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(''); const chatsStore = writable(initialData); const selectedChatId = writable(intitalChatId); localforage.config({ name: 'Pix2PixChat', storeName: 'chatsStore' }); localforage.getItem('chatsStore').then((value) => { if (value) { chatsStore.set(value); } else { chatsStore.set(initialData); } }); chatsStore.subscribe((value) => localforage.setItem('chatsStore', value)); localforage.getItem('selectedChatId').then((value) => { if (value) { selectedChatId.set(value); } else { selectedChatId.set(intitalChatId); } }); selectedChatId.subscribe((value) => localforage.setItem('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(''); // export const chatsStore = writable( // browser ? JSON.parse(localStorage['chatsStore'] || JSON.stringify(initialData)) : initialData // ); // chatsStore.subscribe((value) => { // if (browser) { // return (localStorage['chatsStore'] = JSON.stringify(value)); // } // }); // export const selectedChatId = writable( // browser ? JSON.parse(localStorage['selectedChatId'] || JSON.stringify(intitalChatId)) : intitalChatId // ); // selectedChatId.subscribe((value) => { // if (browser) { // return (localStorage['selectedChatId'] = JSON.stringify(value)); // } // });