-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathAppLoader.tsx
More file actions
191 lines (175 loc) · 7.31 KB
/
AppLoader.tsx
File metadata and controls
191 lines (175 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { useState, useEffect } from "react";
import { AnimatePresence, motion } from "motion/react";
import App from "../../App";
import { AuthTokenModal } from "../AuthTokenModal/AuthTokenModal";
import { ThemeProvider } from "../../contexts/ThemeContext";
import { LoadingScreen } from "../LoadingScreen/LoadingScreen";
import { StartupConnectionError } from "../StartupConnectionError/StartupConnectionError";
import { useReducedMotion } from "../../hooks/useReducedMotion";
import { useWorkspaceStoreRaw, workspaceStore } from "../../stores/WorkspaceStore";
import { useGitStatusStoreRaw } from "../../stores/GitStatusStore";
import { useRuntimeStatusStoreRaw } from "../../stores/RuntimeStatusStore";
import { useBackgroundBashStoreRaw } from "../../stores/BackgroundBashStore";
import { useFlowPromptStoreRaw } from "../../stores/FlowPromptStore";
import { getPRStatusStoreInstance } from "../../stores/PRStatusStore";
import { ProjectProvider, useProjectContext } from "../../contexts/ProjectContext";
import { PolicyProvider, usePolicy } from "@/browser/contexts/PolicyContext";
import { PolicyBlockedScreen } from "@/browser/components/PolicyBlockedScreen/PolicyBlockedScreen";
import { APIProvider, useAPI, type APIClient } from "@/browser/contexts/API";
import { WorkspaceProvider, useWorkspaceContext } from "../../contexts/WorkspaceContext";
import { RouterProvider } from "../../contexts/RouterContext";
import { TelemetryEnabledProvider } from "../../contexts/TelemetryEnabledContext";
import { TerminalRouterProvider } from "../../terminal/TerminalRouterContext";
interface AppLoaderProps {
/** Optional pre-created ORPC api?. If provided, skips internal connection setup. */
client?: APIClient;
}
/**
* AppLoader handles all initialization before rendering the main App:
* 1. Load workspace metadata and projects (via contexts)
* 2. Sync stores with loaded data
* 3. Only render App when everything is ready
*
* WorkspaceContext handles workspace selection restoration from URL.
* RouterProvider must wrap WorkspaceProvider since workspace state is derived from URL.
* WorkspaceProvider must be nested inside ProjectProvider so it can call useProjectContext().
* This ensures App.tsx can assume stores are always synced and removes
* the need for conditional guards in effects.
*/
export function AppLoader(props: AppLoaderProps) {
return (
<ThemeProvider>
<APIProvider client={props.client}>
<PolicyProvider>
<RouterProvider>
<ProjectProvider>
<WorkspaceProvider>
<AppLoaderInner />
</WorkspaceProvider>
</ProjectProvider>
</RouterProvider>
</PolicyProvider>
</APIProvider>
</ThemeProvider>
);
}
/**
* Inner component that has access to both ProjectContext and WorkspaceContext.
* Syncs stores and shows loading screen until ready.
*/
function AppLoaderInner() {
const policyState = usePolicy();
const workspaceContext = useWorkspaceContext();
const projectContext = useProjectContext();
const apiState = useAPI();
const api = apiState.api;
// Get store instances
const workspaceStoreInstance = useWorkspaceStoreRaw();
const gitStatusStore = useGitStatusStoreRaw();
const runtimeStatusStore = useRuntimeStatusStoreRaw();
const backgroundBashStore = useBackgroundBashStoreRaw();
const flowPromptStore = useFlowPromptStoreRaw();
const prefersReducedMotion = useReducedMotion();
// Track whether stores have been synced
const [storesSynced, setStoresSynced] = useState(false);
// Track whether the initial load has completed. After the first successful
// load, we keep rendering the UI during reconnects instead of flashing the
// full-screen LoadingScreen again.
const [initialLoadComplete, setInitialLoadComplete] = useState(false);
// Sync stores when metadata finishes loading
useEffect(() => {
// Keep store clients in sync even during backend restarts (api can be null while reconnecting).
workspaceStoreInstance.setClient(api ?? null);
gitStatusStore.setClient(api ?? null);
runtimeStatusStore.setClient(api ?? null);
backgroundBashStore.setClient(api ?? null);
flowPromptStore.setClient(api ?? null);
getPRStatusStoreInstance().setClient(api ?? null);
if (!workspaceContext.loading) {
workspaceStoreInstance.syncWorkspaces(workspaceContext.workspaceMetadata);
gitStatusStore.syncWorkspaces(workspaceContext.workspaceMetadata);
runtimeStatusStore.syncWorkspaces(workspaceContext.workspaceMetadata);
getPRStatusStoreInstance().syncWorkspaces(workspaceContext.workspaceMetadata);
// Wire up file-modification subscription (idempotent - only subscribes once)
gitStatusStore.subscribeToFileModifications((listener) =>
workspaceStore.subscribeFileModifyingTool(listener)
);
setStoresSynced(true);
} else {
setStoresSynced(false);
}
}, [
workspaceContext.loading,
workspaceContext.workspaceMetadata,
workspaceStoreInstance,
gitStatusStore,
runtimeStatusStore,
backgroundBashStore,
flowPromptStore,
api,
]);
useEffect(() => {
if (initialLoadComplete) {
return;
}
if (!projectContext.loading && !workspaceContext.loading && storesSynced) {
setInitialLoadComplete(true);
}
}, [initialLoadComplete, projectContext.loading, storesSynced, workspaceContext.loading]);
if (policyState.status.state === "blocked") {
return <PolicyBlockedScreen reason={policyState.status.reason} />;
}
// If we're in browser mode and auth is required, show the token prompt before any data loads.
if (apiState.status === "auth_required") {
return (
<AuthTokenModal
isOpen={true}
onSubmit={apiState.authenticate}
onSessionAuthenticated={apiState.retry}
error={apiState.error}
/>
);
}
// AnimatePresence provides a smooth fade-out when the loading screen
// transitions to the main app. mode="wait" ensures the exit animation
// completes before the enter animation starts.
return (
<AnimatePresence mode="wait">
{!initialLoadComplete ? (
<motion.div
key="loading"
initial={{ opacity: 1 }}
exit={prefersReducedMotion ? { opacity: 0 } : { opacity: 0, y: -20 }}
transition={prefersReducedMotion ? { duration: 0 } : { duration: 0.4, ease: "easeInOut" }}
className="bg-surface-primary h-full"
>
{apiState.status === "error" ? (
<StartupConnectionError error={apiState.error} onRetry={apiState.retry} />
) : (
<LoadingScreen
statusText={
apiState.status === "reconnecting"
? `Reconnecting to backend (attempt ${apiState.attempt})...`
: "Loading Mux"
}
/>
)}
</motion.div>
) : (
<motion.div
key="app"
initial={prefersReducedMotion ? { opacity: 1 } : { opacity: 0 }}
animate={{ opacity: 1 }}
transition={prefersReducedMotion ? { duration: 0 } : { duration: 0.3, ease: "easeOut" }}
className="bg-surface-primary h-full"
>
<TelemetryEnabledProvider>
<TerminalRouterProvider>
<App />
</TerminalRouterProvider>
</TelemetryEnabledProvider>
</motion.div>
)}
</AnimatePresence>
);
}