Spaces:
Runtime error
Runtime error
File size: 1,550 Bytes
effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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 }; |