radames's picture
first
effb90e
raw
history blame
2.19 kB
import localforage from 'localforage';
import { writable } from 'svelte/store';
import type { ChatData } from './types';
import { nanoid } from 'nanoid';
const intitalChatId = nanoid();
const initialData: ChatData = [{
id: intitalChatId,
messages: [],
blurb: `New Chat - ${intitalChatId}`,
timestamp: new Date().getTime()
}];
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));
localforage.getItem<string>('selectedChatId').then((value) => {
if (value) {
selectedChatId.set(value);
} else {
selectedChatId.set(intitalChatId);
}
});
selectedChatId.subscribe((value) => localforage.setItem<string>('selectedChatId', value));
export { loadingState, chatsStore, selectedChatId };
// import { writable } from 'svelte/store';
// import { browser } from '$app/environment';
// import type { ChatData } from './types';
// import { nanoid } from 'nanoid';
// const intitalChatId = nanoid();
// const initialData: ChatData = [{
// id: intitalChatId,
// messages: [],
// blurb: `New Chat - ${new Date().getTime()}`
// }
// ]
// export const loadingState = writable<string>('');
// export const chatsStore = writable<ChatData>(
// browser ? JSON.parse(localStorage['chatsStore'] || JSON.stringify(initialData)) : initialData
// );
// chatsStore.subscribe((value) => {
// if (browser) {
// return (localStorage['chatsStore'] = JSON.stringify(value));
// }
// });
// export const selectedChatId = writable<string>(
// browser ? JSON.parse(localStorage['selectedChatId'] || JSON.stringify(intitalChatId)) : intitalChatId
// );
// selectedChatId.subscribe((value) => {
// if (browser) {
// return (localStorage['selectedChatId'] = JSON.stringify(value));
// }
// });