clear-all-caches.ts3.3 KBView on GitHub /**
* Utility to clear all application caches on logout
* This includes React Query cache, IndexedDB, and all Zustand stores
*/
import { useAgentExecutionsStore } from '@/modules/aop/store/agentExecutionsSlice';
import { resetCedarStore } from '@/modules/cedar-os/src/store/CedarStore';
import { clearQueryCache } from '@/providers/query-provider';
import { clearAllPersistedQueries } from '@/lib/query-persistence';
import type { QueryClient } from '@tanstack/react-query';
import { useCedarStore } from '@/modules/store';
/**
* Clear all application caches.
*
* @param queryClient - The React Query client instance
* @param connectionId - The connection ID to clear cache for
* @param options.preserveUserLocalState - When true, skips clearing user-specific persisted
* state (e.g. calendar visible IDs). Use this for admin view transitions where we only
* want to flush query/data caches, not the authenticated user's own preferences.
* Full logout should always leave this false (the default).
*/
export async function clearAllCaches(
queryClient: QueryClient,
connectionId: string | null,
options: { preserveUserLocalState?: boolean } = {},
) {
const { preserveUserLocalState = false } = options;
try {
// 1. Clear React Query cache (both in-memory and persisted in IndexedDB)
queryClient.clear();
await clearQueryCache(connectionId);
// Also clear ALL persisted queries on logout (not just this connection)
await clearAllPersistedQueries();
// 3. Reset all Zustand stores
// Mail store - has multiple reset methods for different slices
const mailStore = useCedarStore.getState();
mailStore.resetMailState?.();
mailStore.resetThreadSlice?.();
mailStore.resetSearchState?.();
mailStore.resetNavigationState?.();
mailStore.resetCRMState?.();
if (!preserveUserLocalState) {
// Calendar visible IDs belong to the authenticated user and must survive admin view
// switches. Only wipe them on full logout so we never accidentally persist another
// user's calendar IDs into the real user's localStorage.
mailStore.resetCalendarState?.();
}
mailStore.clear?.(); // Clear conversations slice
// Agent Executions store - reset to initial state
useAgentExecutionsStore.setState({
executions: {},
threadToExecutions: {},
conversationToExecutions: {},
loading: new Set(),
});
// Cedar OS store - Clear persisted data and reset state
// Note: Some Cedar stores use persist middleware for messages
if (typeof window !== 'undefined' && window.localStorage) {
// Clear any persisted Cedar store data
const keysToRemove = Object.keys(window.localStorage).filter(
(key) =>
key.startsWith('cedar-store') ||
key.startsWith('cedar-mail') ||
key === 'mail-active-filters' ||
(!preserveUserLocalState && key === 'calendar-visible-ids'),
);
keysToRemove.forEach((key) => window.localStorage.removeItem(key));
}
// Reset Cedar store using the dedicated reset function
resetCedarStore();
console.log('✅ All caches cleared successfully');
} catch (error) {
console.error('❌ Error clearing caches:', error);
// Don't throw - we still want logout to proceed even if cache clearing fails
}
}