diff --git a/www/app/Chat.tsx b/www/app/Chat.tsx index 28e9c7c..1aa034d 100644 --- a/www/app/Chat.tsx +++ b/www/app/Chat.tsx @@ -150,30 +150,20 @@ export default function Chat({ return getConversations(); }; - const { data: conversations, mutate: mutateConversations } = useSWR( - userId, - conversationsFetcher, - { - fallbackData: initialConversations, - onSuccess: async (conversations) => { - if (conversations.length) { - if ( - !conversationId || - !conversations.find((c) => c.conversationId === conversationId) - ) { - setConversationId(conversations[0].conversationId); - } - setCanSend(true); - } else { - const newConvo = await createConversation(); - setConversationId(newConvo?.conversationId); - await mutateConversations(); - } - }, - provider: cacheProvider, - revalidateOnFocus: false, - } - ); + const conversationsKey = useMemo(() => userId, [userId]); + +const { data: conversations, mutate: mutateConversations } = useSWR( + conversationsKey, + conversationsFetcher, + { + fallbackData: initialConversations, + provider: cacheProvider, + revalidateOnFocus: false, + dedupingInterval: 60000, + revalidateIfStale: false, + revalidateOnMount: false, + } +); const messagesFetcher = async (conversationId: string) => { if (!userId) return Promise.resolve([]); @@ -183,12 +173,17 @@ export default function Chat({ return getMessages(conversationId); }; + const messagesKey = useMemo(() => + conversationId ? ['messages', conversationId] : null, + [conversationId] + ); + const { data: messages, mutate: mutateMessages, isLoading: messagesLoading, } = useSWR( - conversationId ? ['messages', conversationId] : null, + messagesKey, () => messagesFetcher(conversationId!), { fallbackData: initialMessages, @@ -200,7 +195,7 @@ export default function Chat({ if (conversationId?.startsWith('temp-')) { mutateMessages([], false); } - }, + } } ); diff --git a/www/utils/swrCache.ts b/www/utils/swrCache.ts index be02e5a..289002d 100644 --- a/www/utils/swrCache.ts +++ b/www/utils/swrCache.ts @@ -1,45 +1,12 @@ -import { createStore, get, set, del, clear, } from 'idb-keyval'; import type { Cache } from 'swr'; -const store = typeof window !== 'undefined' - ? createStore('bloom-db', 'messages') - : null; - -// In-memory cache for synchronous operations const memoryCache = new Map(); -export const clearSWRCache = async () => { - memoryCache.clear(); - if (store && typeof window !== 'undefined') { - try { - await clear(store); - } catch (error) { - console.error('Error clearing cache:', error); - } - } -}; +export const cacheProvider = (cache: Readonly>) => ({ + get: (key: string) => memoryCache.get(key), + set: (key: string, value: any) => memoryCache.set(key, value), + delete: (key: string) => memoryCache.delete(key), + keys: () => Array.from(memoryCache.keys())[Symbol.iterator]() +}); -export const cacheProvider = (cache: Readonly>) => { - return { - get: (key: string) => { - return memoryCache.get(key); - }, - set: (key: string, value: any) => { - memoryCache.set(key, value); - // Persist to IndexedDB in background - if (store) { - set(key, value, store).catch(console.error); - } - }, - delete: (key: string) => { - memoryCache.delete(key); - // Delete from IndexedDB in background - if (store) { - del(key, store).catch(console.error); - } - }, - keys: () => { - return memoryCache.keys(); - } - } -} \ No newline at end of file +export const clearSWRCache = () => memoryCache.clear(); \ No newline at end of file