radames's picture
first
18cd77b
import localforage from 'localforage';
import { writable } from 'svelte/store';
import { MessageType, Sender, type ChatData, type Message } from './types';
import { nanoid } from 'nanoid';
import { initImage } from './initImage';
const intitalChatId = nanoid();
const initMessage: Message = {
id: nanoid(),
content: initImage,
sender: Sender.USER,
type: MessageType.IMAGE,
timestamp: new Date().getTime()
}
const initialData: ChatData = [{
id: intitalChatId,
messages: [initMessage],
blurb: `New Chat - ${intitalChatId}`,
timestamp: new Date().getTime()
}];
const lastBase64Image = writable<string | null>(initMessage.content);
const loadingState = writable<string>('');
const chatsStore = writable<ChatData>(initialData);
const selectedChatId = writable<string>(intitalChatId);
localforage.config({
name: 'Pix2PixChat',
storeName: 'chatsStore'
});
localforage.getItem<ChatData>('chatsStore').then((value) => {
if (value) {
chatsStore.set(value);
} else {
chatsStore.set(initialData);
}
});
chatsStore.subscribe((value) => {
localforage.setItem<ChatData>('chatsStore', value)
return value;
});
localforage.getItem<string>('selectedChatId').then((value) => {
if (value) {
selectedChatId.set(value);
} else {
selectedChatId.set(intitalChatId);
}
});
selectedChatId.subscribe((value) => {
localforage.setItem<string>('selectedChatId', value)
return value;
});
export { loadingState, chatsStore, selectedChatId, lastBase64Image };