From 5f2cc9a717a90dbe8d1504f4f682676bfd293a35 Mon Sep 17 00:00:00 2001 From: Dustin <155417613+vgcman16@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:21:15 -0500 Subject: [PATCH] fix: improve persistence handling with global availability flag --- app/entry.client.tsx | 12 ++++++- app/lib/persistence/useChatHistory.ts | 45 ++++++++++++++++++++------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/app/entry.client.tsx b/app/entry.client.tsx index f2847094d..b8a6dd3da 100644 --- a/app/entry.client.tsx +++ b/app/entry.client.tsx @@ -56,8 +56,18 @@ async function initIndexedDB() { } // Initialize IndexedDB before hydrating the app -initIndexedDB().then(() => { +initIndexedDB().then((success) => { + // Set a global flag for persistence status + window.__BOLT_PERSISTENCE_AVAILABLE__ = success; + startTransition(() => { hydrateRoot(document.getElementById('root')!, ); }); }); + +// Add type declaration +declare global { + interface Window { + __BOLT_PERSISTENCE_AVAILABLE__: boolean; + } +} diff --git a/app/lib/persistence/useChatHistory.ts b/app/lib/persistence/useChatHistory.ts index bf0a3244f..b3ac5e6cf 100644 --- a/app/lib/persistence/useChatHistory.ts +++ b/app/lib/persistence/useChatHistory.ts @@ -37,9 +37,9 @@ async function initializeDb() { return undefined; } - // Check if IndexedDB is available - if (!window.indexedDB) { - logger.debug('IndexedDB not available'); + // Check if persistence is available + if (!window.__BOLT_PERSISTENCE_AVAILABLE__) { + logger.debug('Persistence not available'); return undefined; } @@ -68,23 +68,39 @@ export function useChatHistory() { useEffect(() => { const init = async () => { try { + // Always try to initialize the database const database = await initializeDb(); + + // If we have a mixedId but no database, navigate home silently + if (mixedId && !database) { + navigate('/', { replace: true }); + setReady(true); + return; + } + // If we have both mixedId and database, try to load messages if (mixedId && database) { - const storedMessages = await getMessages(database, mixedId); - if (storedMessages && storedMessages.messages.length > 0) { - setInitialMessages(storedMessages.messages); - setUrlId(storedMessages.urlId); - description.set(storedMessages.description); - chatId.set(storedMessages.id); - } else { + try { + const storedMessages = await getMessages(database, mixedId); + if (storedMessages && storedMessages.messages.length > 0) { + setInitialMessages(storedMessages.messages); + setUrlId(storedMessages.urlId); + description.set(storedMessages.description); + chatId.set(storedMessages.id); + } else { + navigate('/', { replace: true }); + } + } catch (error) { + logger.error('Failed to load messages:', error); navigate('/', { replace: true }); } } + + setReady(true); } catch (error) { logger.error('Failed to initialize:', error); + setReady(true); } - setReady(true); }; init(); @@ -133,3 +149,10 @@ function navigateChat(nextId: string) { url.pathname = `/chat/${nextId}`; window.history.replaceState({}, '', url); } + +// Add type declaration +declare global { + interface Window { + __BOLT_PERSISTENCE_AVAILABLE__: boolean; + } +}