diff --git a/apps/studio/lib/auth.tsx b/apps/studio/lib/auth.tsx index 18ef96866c105..92663680c914b 100644 --- a/apps/studio/lib/auth.tsx +++ b/apps/studio/lib/auth.tsx @@ -8,6 +8,7 @@ import { import { PropsWithChildren, useCallback, useEffect } from 'react' import { toast } from 'sonner' +import { useAiAssistantStateSnapshot } from 'state/ai-assistant-state' import { GOTRUE_ERRORS, IS_PLATFORM } from './constants' const AuthErrorToaster = ({ children }: PropsWithChildren) => { @@ -42,12 +43,15 @@ export { useAuth, useIsLoggedIn, useSession, useUser } from 'common' export function useSignOut() { const queryClient = useQueryClient() + const { clearStorage: clearAssistantStorage } = useAiAssistantStateSnapshot() return useCallback(async () => { const result = await gotrueClient.signOut() clearLocalStorage() + // Clear Assistant IndexedDB + await clearAssistantStorage() await queryClient.clear() return result - }, [queryClient]) + }, [queryClient, clearAssistantStorage]) } diff --git a/apps/studio/state/ai-assistant-state.tsx b/apps/studio/state/ai-assistant-state.tsx index c6047f3e8bea7..b33feb9704718 100644 --- a/apps/studio/state/ai-assistant-state.tsx +++ b/apps/studio/state/ai-assistant-state.tsx @@ -95,6 +95,15 @@ async function saveAiState(state: StoredAiAssistantState): Promise { } } +async function clearStorage(): Promise { + try { + const db = await openAiDb() + await db.clear(STORE_NAME) + } catch (error) { + console.error('Failed to clear AI state from IndexedDB:', error) + } +} + // Helper function to load state from IndexedDB async function loadFromIndexedDB(projectRef: string): Promise { try { @@ -387,6 +396,10 @@ export const createAiAssistantState = (): AiAssistantState => { } } }, + + clearStorage: async () => { + await clearStorage() + }, }) return state @@ -413,6 +426,7 @@ export type AiAssistantState = AiAssistantData & { clearSqlSnippets: () => void getCachedSQLResults: (args: { messageId: string; snippetId?: string }) => any[] | undefined loadPersistedState: (persistedState: StoredAiAssistantState) => void + clearStorage: () => Promise } export const AiAssistantStateContext = createContext(createAiAssistantState())