Files
sim/stores/index.ts
2025-02-18 20:17:38 -08:00

81 lines
2.6 KiB
TypeScript

import { useChatStore } from './chat/store'
import { useConsoleStore } from './console/store'
import { useExecutionStore } from './execution/store'
import { useNotificationStore } from './notifications/store'
import { useEnvironmentStore } from './settings/environment/store'
import { useGeneralStore } from './settings/general/store'
import { addDeletedWorkflow, initializeSyncManager } from './sync-manager'
import { useWorkflowRegistry } from './workflow/registry/store'
import { useWorkflowStore } from './workflow/store'
// Initialize sync manager when the store is first imported
if (typeof window !== 'undefined') {
initializeSyncManager()
}
// Reset all application stores to their initial state
export const resetAllStores = () => {
// Track all workflow IDs for deletion before clearing
if (typeof window !== 'undefined') {
const workflowRegistry = useWorkflowRegistry.getState().workflows
Object.keys(workflowRegistry).forEach((id) => {
addDeletedWorkflow(id)
})
// Selectively clear localStorage items
const keysToKeep = ['next-favicon']
const keysToRemove = Object.keys(localStorage).filter((key) => !keysToKeep.includes(key))
keysToRemove.forEach((key) => localStorage.removeItem(key))
}
// Force immediate state reset for all stores
// This ensures in-memory state is also cleared
useWorkflowStore.getState().clear()
useWorkflowRegistry.setState({
workflows: {},
activeWorkflowId: null,
isLoading: false,
error: null,
})
useNotificationStore.setState({ notifications: [] })
useEnvironmentStore.setState({ variables: {} })
useExecutionStore.getState().reset()
useConsoleStore.setState({ entries: [], isOpen: false })
useGeneralStore.setState({ isAutoConnectEnabled: true, isDebugModeEnabled: false })
useChatStore.setState({ messages: [], isProcessing: false, error: null })
}
// Log the current state of all stores
export const logAllStores = () => {
const state = {
workflow: useWorkflowStore.getState(),
workflowRegistry: useWorkflowRegistry.getState(),
notifications: useNotificationStore.getState(),
environment: useEnvironmentStore.getState(),
execution: useExecutionStore.getState(),
console: useConsoleStore.getState(),
chat: useChatStore.getState(),
}
console.group('Application State')
Object.entries(state).forEach(([storeName, storeState]) => {
console.group(storeName)
console.log(storeState)
console.groupEnd()
})
console.groupEnd()
return state
}
// Export all stores for convenience
export {
useWorkflowStore,
useWorkflowRegistry,
useNotificationStore,
useEnvironmentStore,
useExecutionStore,
useConsoleStore,
useChatStore,
}