diff --git a/app/components/sidebar/Menu.client.tsx b/app/components/sidebar/Menu.client.tsx index 83990264..b79ca849 100644 --- a/app/components/sidebar/Menu.client.tsx +++ b/app/components/sidebar/Menu.client.tsx @@ -41,18 +41,19 @@ const skipConfirmDeleteCookieName = 'skipConfirmDelete'; export const Menu = () => { const menuRef = useRef(null); - const [list, setList] = useState([]); + const [list, setList] = useState(null); const [open, setOpen] = useState(false); const [dialogContent, setDialogContent] = useState(null); const [isSettingsOpen, setIsSettingsOpen] = useState(false); const [skipConfirmDeleteChecked, setSkipConfirmDeleteChecked] = useState(false); const { filteredItems: filteredList, handleSearchChange } = useSearchFilter({ - items: list, + items: list ?? [], searchFields: ['title'], }); const loadEntries = useCallback(() => { + setList(null); database .getAllChats() .then(setList) @@ -64,7 +65,7 @@ export const Menu = () => { event.preventDefault(); // Optimistically remove the item from the list while we update the database. - setList(list.filter((chat) => chat.id !== item.id)); + setList((list ?? []).filter((chat) => chat.id !== item.id)); database .deleteChat(item.id) @@ -168,7 +169,7 @@ export const Menu = () => {
{filteredList.length === 0 && (
- {list.length === 0 ? 'No previous conversations' : 'No matches found'} + {list ? (list.length === 0 ? 'No previous conversations' : 'No matches found') : 'Loading...'}
)} diff --git a/app/lib/persistence/chats.ts b/app/lib/persistence/chats.ts index 8720c767..1e2c30e3 100644 --- a/app/lib/persistence/chats.ts +++ b/app/lib/persistence/chats.ts @@ -68,26 +68,13 @@ const deletedChats = new Set(); async function getAllChats(): Promise { const userId = await getCurrentUserId(); - - if (!userId) { - return getLocalChats(); - } - - const { data, error } = await getSupabase().from('chats').select(CHAT_SUMMARY_COLUMNS).eq('deleted', false); - - if (error) { - throw error; - } - - const chats = data.map(databaseRowToChatSummary); - return chats.filter((chat) => !deletedChats.has(chat.id)); -} - -async function syncLocalChats(): Promise { - const userId = await getCurrentUserId(); const localChats = getLocalChats(); - if (userId && localChats.length) { + if (!userId) { + return localChats; + } + + if (localChats.length) { try { for (const chat of localChats) { if (chat.title) { @@ -99,6 +86,15 @@ async function syncLocalChats(): Promise { console.error('Error syncing local chats', error); } } + + const { data, error } = await getSupabase().from('chats').select(CHAT_SUMMARY_COLUMNS).eq('deleted', false); + + if (error) { + throw error; + } + + const chats = data.map(databaseRowToChatSummary); + return chats.filter((chat) => !deletedChats.has(chat.id)); } async function setChatContents(chat: ChatContents) { @@ -254,7 +250,6 @@ async function updateChatLastMessage( export const database = { getAllChats, - syncLocalChats, setChatContents, getChatPublicData, getChatContents, diff --git a/app/lib/stores/auth.ts b/app/lib/stores/auth.ts index be86e0c4..d9652d06 100644 --- a/app/lib/stores/auth.ts +++ b/app/lib/stores/auth.ts @@ -4,7 +4,6 @@ import { type User, type Session, AuthError } from '@supabase/supabase-js'; import { logStore } from './logs'; import { useEffect, useState } from 'react'; import { isAuthenticated } from '~/lib/supabase/client'; -import { database } from '~/lib/persistence/chats'; import { pingTelemetry } from '~/lib/hooks/pingTelemetry'; export const userStore = atom(null); @@ -84,8 +83,6 @@ export async function initializeAuth() { sessionStore.set(null); logStore.logSystem('User signed out'); } - - await database.syncLocalChats(); }); return () => {