MCPcopy Create free account
hub / github.com/TraderAlice/OpenAlice / WorkspacesProvider

Function WorkspacesProvider

ui/src/contexts/WorkspacesContext.tsx:59–377  ·  view source on GitHub ↗
({ children }: { children: ReactNode })

Source from the content-addressed store, hash-verified

57const LIST_POLL_MS = 3000
58
59export function WorkspacesProvider({ children }: { children: ReactNode }) {
60 const [workspaces, setWorkspaces] = useState<Workspace[]>([])
61 const [templates, setTemplates] = useState<TemplateInfo[]>([])
62 const [templatesLoaded, setTemplatesLoaded] = useState(false)
63 const [agents, setAgents] = useState<AgentInfo[]>([])
64 const [defaultAgent, setDefaultAgentState] = useState<string | null>(null)
65 const [issueDefaultAgent, setIssueDefaultAgentState] = useState<string | null>(null)
66 const [listError, setListError] = useState<string | null>(null)
67 // Don't reconcile orphan tabs until we've successfully fetched the
68 // workspaces list at least once — otherwise the initial `[]` looks like
69 // "every workspace was just deleted" and a deep-linked workspace URL
70 // gets its freshly-opened tab closed before the first poll lands.
71 const [hasLoaded, setHasLoaded] = useState(false)
72 // AI-provider config modal target. Lifted to context so the sidebar
73 // gear button (no workspace tab needed) and the WorkspacePage header
74 // button share one modal instance — and the modal survives activity
75 // switches (rendered here, not inside an activity-scoped component).
76 const [configuringAgentTarget, setConfiguringAgentTarget] = useState<{ wsId: string; agent?: AgentId } | null>(null)
77 const [pendingSessionDelete, setPendingSessionDelete] = useState<{ wsId: string; sessionId: string } | null>(null)
78 const { t } = useTranslation()
79 const terminalTheme = useResolvedTerminalThemeVariant()
80
81 const openOrFocus = useWorkspace((s) => s.openOrFocus)
82 const closeTab = useWorkspace((s) => s.closeTab)
83
84 const refresh = useCallback(async (): Promise<void> => {
85 try {
86 const list = await listWorkspaces()
87 setWorkspaces(list)
88 setHasLoaded(true)
89 setListError(null)
90 } catch (err) {
91 setListError((err as Error).message)
92 }
93 }, [])
94
95 useEffect(() => {
96 void refresh()
97 const id = setInterval(() => void refresh(), LIST_POLL_MS)
98 return () => clearInterval(id)
99 }, [refresh])
100
101 useEffect(() => {
102 void listTemplates()
103 .then(setTemplates)
104 .catch(() => setTemplates([]))
105 .finally(() => setTemplatesLoaded(true))
106 void listAgents().then(setAgents).catch(() => setAgents([]))
107 void getWorkspaceDefaultAgent().then(setDefaultAgentState).catch(() => setDefaultAgentState(null))
108 void getIssueDefaultAgent().then(setIssueDefaultAgentState).catch(() => setIssueDefaultAgentState(null))
109 }, [])
110
111 // Reconcile tabs against the workspaces list. If a workspace or session
112 // disappeared (deleted on disk / on the server), close any tabs that
113 // referenced it so they don't dangle as 404s.
114 useEffect(() => {
115 if (!hasLoaded) return
116 const validW = new Set<string>()

Callers

nothing calls this directly

Calls 15

listWorkspacesFunction · 0.90
listTemplatesFunction · 0.90
listAgentsFunction · 0.90
getWorkspaceDefaultAgentFunction · 0.90
getIssueDefaultAgentFunction · 0.90
spawnSessionFunction · 0.90
updateWorkspaceMetadataFunction · 0.90
deleteSessionFunction · 0.90
closeTabFunction · 0.85
openOrFocusFunction · 0.85
withRecordFunction · 0.85

Tested by

no test coverage detected