import { create } from 'zustand' import type { Workspace } from '@/types' import { workspacesApi } from '@/api/workspaces' interface WorkspaceState { workspaces: Workspace[] activeWorkspaceId: string | null loading: boolean sidebarCollapsed: boolean setActiveWorkspace: (id: string) => void fetchWorkspaces: () => Promise getActiveWorkspace: () => Workspace | undefined toggleSidebar: () => void } export const useWorkspaceStore = create((set, get) => ({ workspaces: [], activeWorkspaceId: localStorage.getItem('active-workspace-id'), loading: false, sidebarCollapsed: localStorage.getItem('sidebar-collapsed') === 'true', setActiveWorkspace: (id: string) => { localStorage.setItem('active-workspace-id', id) set({ activeWorkspaceId: id }) }, fetchWorkspaces: async () => { set({ loading: true }) try { const workspaces = await workspacesApi.list() const state = get() let activeId = state.activeWorkspaceId // If no active workspace or active workspace doesn't exist, use default if (!activeId || !workspaces.find(w => w.id === activeId)) { const defaultWs = workspaces.find(w => w.is_default) || workspaces[0] if (defaultWs) { activeId = defaultWs.id localStorage.setItem('active-workspace-id', activeId) } } set({ workspaces, activeWorkspaceId: activeId, loading: false }) } catch { set({ loading: false }) } }, getActiveWorkspace: () => { const { workspaces, activeWorkspaceId } = get() return workspaces.find(w => w.id === activeWorkspaceId) }, toggleSidebar: () => { const next = !get().sidebarCollapsed localStorage.setItem('sidebar-collapsed', String(next)) set({ sidebarCollapsed: next }) }, }))