-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathRouterContext.tsx
More file actions
475 lines (418 loc) · 15.7 KB
/
RouterContext.tsx
File metadata and controls
475 lines (418 loc) · 15.7 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
type ReactNode,
} from "react";
import { MemoryRouter, useLocation, useNavigate, useSearchParams } from "react-router-dom";
import { readPersistedState, updatePersistedState } from "@/browser/hooks/usePersistedState";
import {
prependInitialAppProxyBasePath,
stripInitialAppProxyBasePathFromPathname,
} from "@/browser/utils/frontendBasePath";
import {
LAST_VISITED_ROUTE_KEY,
LAUNCH_BEHAVIOR_KEY,
SELECTED_WORKSPACE_KEY,
type LaunchBehavior,
} from "@/common/constants/storage";
import type { WorkspaceSelection } from "@/browser/components/ProjectSidebar/ProjectSidebar";
import { getProjectRouteId } from "@/common/utils/projectRouteId";
export interface RouterContext {
navigateToWorkspace: (workspaceId: string) => void;
navigateToProject: (
projectPath: string,
sectionId?: string,
draftId?: string,
options?: { replace?: boolean }
) => void;
navigateToHome: () => void;
navigateToSettings: (section?: string) => void;
navigateFromSettings: () => void;
navigateToAnalytics: () => void;
navigateFromAnalytics: () => void;
currentWorkspaceId: string | null;
/** Settings section from URL (null when not on settings page). */
currentSettingsSection: string | null;
/** Project identifier from URL (does not include full filesystem path). */
currentProjectId: string | null;
/** Optional project path carried via in-memory navigation state (not persisted on refresh). */
currentProjectPathFromState: string | null;
/** Section ID for pending workspace creation (from URL) */
pendingSectionId: string | null;
/** Draft ID for UI-only workspace creation drafts (from URL) */
pendingDraftId: string | null;
/** True when the analytics dashboard route is active. */
isAnalyticsOpen: boolean;
}
const RouterContext = createContext<RouterContext | undefined>(undefined);
export function useRouter(): RouterContext {
const ctx = useContext(RouterContext);
if (!ctx) {
throw new Error("useRouter must be used within RouterProvider");
}
return ctx;
}
type StartupNavigationType = "navigate" | "reload" | "back_forward" | "prerender" | null;
function isStandalonePwa(): boolean {
return (
typeof window.matchMedia === "function" &&
window.matchMedia("(display-mode: standalone)").matches
);
}
function getStartupNavigationType(): StartupNavigationType {
const entries = window.performance?.getEntriesByType?.("navigation");
const firstEntry = entries?.[0];
const entryType =
firstEntry && typeof firstEntry === "object" && "type" in firstEntry ? firstEntry.type : null;
if (
entryType === "navigate" ||
entryType === "reload" ||
entryType === "back_forward" ||
entryType === "prerender"
) {
return entryType;
}
const legacyType = window.performance?.navigation?.type;
if (legacyType === 1) {
return "reload";
}
if (legacyType === 2) {
return "back_forward";
}
if (legacyType === 0) {
return "navigate";
}
return null;
}
function isRouteRestoringNavigationType(type: StartupNavigationType): boolean {
return type === "reload" || type === "back_forward";
}
function shouldRestoreWorkspaceUrlOnStartup(options: {
isStandalone: boolean;
launchBehavior: LaunchBehavior | null;
navigationType: StartupNavigationType;
}): boolean {
if (options.isStandalone) {
return isRouteRestoringNavigationType(options.navigationType);
}
return (
options.launchBehavior === "last-workspace" ||
isRouteRestoringNavigationType(options.navigationType)
);
}
function hasValidEncodedPathSegment(encodedValue: string): boolean {
if (encodedValue.length === 0) {
return false;
}
try {
decodeURIComponent(encodedValue);
return true;
} catch {
return false;
}
}
function hasValidRestorableWorkspaceRoute(route: string): boolean {
if (!route.startsWith("/workspace/")) {
return false;
}
const workspaceId = route.slice("/workspace/".length).split(/[?#]/, 1)[0] ?? "";
return hasValidEncodedPathSegment(workspaceId);
}
function hasValidRestorableSettingsRoute(route: string): boolean {
if (!route.startsWith("/settings/")) {
return false;
}
const sectionId = route.slice("/settings/".length).split(/[?#]/, 1)[0] ?? "";
return hasValidEncodedPathSegment(sectionId);
}
function matchesRouteBoundary(route: string, basePath: string): boolean {
return route === basePath || route.startsWith(`${basePath}?`) || route.startsWith(`${basePath}#`);
}
function decodePathSegment(segment: string): string | null {
try {
return decodeURIComponent(segment);
} catch {
return null;
}
}
function isRestorableRoute(route: unknown): route is string {
if (typeof route !== "string" || route.length === 0) {
return false;
}
return (
hasValidRestorableWorkspaceRoute(route) ||
matchesRouteBoundary(route, "/project") ||
hasValidRestorableSettingsRoute(route) ||
matchesRouteBoundary(route, "/analytics")
);
}
/** Get the initial route, falling back to the compatibility root entrypoint when needed. */
function getInitialRoute(): string {
const routePathname = stripInitialAppProxyBasePathFromPathname(window.location.pathname);
const isStorybook = routePathname.endsWith("iframe.html");
const isStandalone = isStandalonePwa();
const navigationType = getStartupNavigationType();
const launchBehavior = !isStandalone
? readPersistedState<LaunchBehavior>(LAUNCH_BEHAVIOR_KEY, "dashboard")
: null;
if (window.location.protocol === "file:") {
const persistedRoute = readPersistedState<string | null>(LAST_VISITED_ROUTE_KEY, null);
if (isRestorableRoute(persistedRoute)) {
return persistedRoute;
}
}
// In browser mode (not Storybook), read route directly from the current URL. Workspace
// routes are special: fresh launches may ignore them, but explicit restore-style navigations
// such as hard reload/back-forward should reopen the same chat.
if (window.location.protocol !== "file:" && !isStorybook) {
const url = routePathname + window.location.search;
// Only use URL if it's a valid route (starts with /, not just "/" or empty)
if (url.startsWith("/") && url !== "/") {
if (!url.startsWith("/workspace/")) {
return url;
}
if (
shouldRestoreWorkspaceUrlOnStartup({
isStandalone,
launchBehavior,
navigationType,
})
) {
return url;
}
}
}
// In Storybook, stories seed localStorage via selectWorkspace() during setup.
// Read that selection so stories start at the correct workspace view.
if (isStorybook) {
const savedWorkspace = readPersistedState<WorkspaceSelection | null>(
SELECTED_WORKSPACE_KEY,
null
);
if (savedWorkspace?.workspaceId) {
return `/workspace/${encodeURIComponent(savedWorkspace.workspaceId)}`;
}
}
if (!isStandalone && launchBehavior === "last-workspace") {
const savedWorkspace = readPersistedState<WorkspaceSelection | null>(
SELECTED_WORKSPACE_KEY,
null
);
if (savedWorkspace?.workspaceId) {
return `/workspace/${encodeURIComponent(savedWorkspace.workspaceId)}`;
}
}
// "dashboard" (legacy storage value) and "new-chat" both enter through "/".
// WorkspaceContext immediately resolves that compatibility root route to a real page.
return "/";
}
/** Sync router state to browser URL (dev server) and persist the desktop route. */
function useUrlSync(): void {
const location = useLocation();
useEffect(() => {
const url = location.pathname + location.search + location.hash;
// The dedicated Mux home page is gone. Keep "/" as a transient compatibility
// entrypoint, but only persist real restorable routes so desktop relaunches reopen
// the last meaningful page instead of getting stuck on root.
if (isRestorableRoute(url)) {
updatePersistedState(LAST_VISITED_ROUTE_KEY, url);
}
const currentRoutePathname = stripInitialAppProxyBasePathFromPathname(window.location.pathname);
// Skip in Storybook (conflicts with story navigation)
if (currentRoutePathname.endsWith("iframe.html")) return;
// Skip in Electron (file:// reloads always boot through index.html; we restore via localStorage above)
if (window.location.protocol === "file:") return;
const browserUrl = prependInitialAppProxyBasePath(url);
if (browserUrl !== window.location.pathname + window.location.search + window.location.hash) {
window.history.replaceState(null, "", browserUrl);
}
}, [location.pathname, location.search, location.hash]);
}
function RouterContextInner(props: { children: ReactNode }) {
function getProjectPathFromLocationState(state: unknown): string | null {
if (!state || typeof state !== "object") return null;
if (!("projectPath" in state)) return null;
const projectPath = (state as { projectPath?: unknown }).projectPath;
return typeof projectPath === "string" ? projectPath : null;
}
const navigate = useNavigate();
const navigateRef = useRef(navigate);
useEffect(() => {
navigateRef.current = navigate;
}, [navigate]);
const location = useLocation();
const [searchParams] = useSearchParams();
useUrlSync();
const workspaceMatch = /^\/workspace\/(.+)$/.exec(location.pathname);
const currentWorkspaceId = workspaceMatch ? decodePathSegment(workspaceMatch[1]) : null;
const currentProjectId =
location.pathname === "/project"
? (searchParams.get("project") ?? searchParams.get("path"))
: null;
const currentProjectPathFromState =
location.pathname === "/project" ? getProjectPathFromLocationState(location.state) : null;
const settingsMatch = /^\/settings\/([^/]+)$/.exec(location.pathname);
const currentSettingsSection = settingsMatch ? decodePathSegment(settingsMatch[1]) : null;
const isAnalyticsOpen = location.pathname === "/analytics";
interface NonSettingsLocationSnapshot {
url: string;
state: unknown;
}
// When leaving settings, we need to restore the *full* previous location including
// any in-memory navigation state (e.g. /project relies on { projectPath } state, and
// the legacy ?path= deep link rewrite stores that path in location.state).
// Include /analytics so Settings opened from Analytics can close back to Analytics.
const lastNonSettingsLocationRef = useRef<NonSettingsLocationSnapshot>({
url: getInitialRoute(),
state: null,
});
// Keep a separate "close analytics" snapshot that intentionally excludes /analytics so
// closing analytics still returns to the last non-analytics route.
const lastNonAnalyticsLocationRef = useRef<NonSettingsLocationSnapshot>({
url: getInitialRoute(),
state: null,
});
useEffect(() => {
if (!location.pathname.startsWith("/settings")) {
const locationSnapshot: NonSettingsLocationSnapshot = {
url: location.pathname + location.search,
state: location.state,
};
lastNonSettingsLocationRef.current = locationSnapshot;
if (location.pathname !== "/analytics") {
lastNonAnalyticsLocationRef.current = locationSnapshot;
}
}
}, [location.pathname, location.search, location.state]);
// Back-compat: if we ever land on a legacy deep link (/project?path=<full path>),
// immediately replace it with the non-path project id URL.
useEffect(() => {
if (location.pathname !== "/project") return;
const params = new URLSearchParams(location.search);
const legacyPath = params.get("path");
const projectParam = params.get("project");
if (!projectParam && legacyPath) {
const section = params.get("section");
const draft = params.get("draft");
const projectId = getProjectRouteId(legacyPath);
const nextParams = new URLSearchParams();
nextParams.set("project", projectId);
if (section) {
nextParams.set("section", section);
}
if (draft) {
nextParams.set("draft", draft);
}
const url = `/project?${nextParams.toString()}`;
void navigateRef.current(url, { replace: true, state: { projectPath: legacyPath } });
}
}, [location.pathname, location.search]);
const pendingSectionId = location.pathname === "/project" ? searchParams.get("section") : null;
const pendingDraftId = location.pathname === "/project" ? searchParams.get("draft") : null;
// Navigation defaults to push so back/forward keeps working as expected.
// Callers can opt into replace for compatibility-root redirects that should not
// add a disposable "/" history entry.
const navigateToWorkspace = useCallback((id: string) => {
void navigateRef.current(`/workspace/${encodeURIComponent(id)}`);
}, []);
const navigateToProject = useCallback(
(path: string, sectionId?: string, draftId?: string, options?: { replace?: boolean }) => {
const projectId = getProjectRouteId(path);
const params = new URLSearchParams();
params.set("project", projectId);
if (sectionId) {
params.set("section", sectionId);
}
if (draftId) {
params.set("draft", draftId);
}
const url = `/project?${params.toString()}`;
void navigateRef.current(url, {
replace: options?.replace === true,
state: { projectPath: path },
});
},
[]
);
const navigateToHome = useCallback(() => {
void navigateRef.current("/");
}, []);
const navigateToSettings = useCallback((section?: string) => {
const nextSection = section ?? "general";
void navigateRef.current(`/settings/${encodeURIComponent(nextSection)}`);
}, []);
const navigateFromSettings = useCallback(() => {
const lastLocation = lastNonSettingsLocationRef.current;
if (!lastLocation.url || lastLocation.url.startsWith("/settings")) {
void navigateRef.current("/");
return;
}
void navigateRef.current(lastLocation.url, { state: lastLocation.state });
}, []);
const navigateToAnalytics = useCallback(() => {
void navigateRef.current("/analytics");
}, []);
const navigateFromAnalytics = useCallback(() => {
const lastLocation = lastNonAnalyticsLocationRef.current;
if (
!lastLocation.url ||
lastLocation.url.startsWith("/settings") ||
lastLocation.url === "/analytics"
) {
void navigateRef.current("/");
return;
}
void navigateRef.current(lastLocation.url, { state: lastLocation.state });
}, []);
const value = useMemo<RouterContext>(
() => ({
navigateToWorkspace,
navigateToProject,
navigateToHome,
navigateToSettings,
navigateFromSettings,
navigateToAnalytics,
navigateFromAnalytics,
currentWorkspaceId,
currentSettingsSection,
currentProjectId,
currentProjectPathFromState,
pendingSectionId,
pendingDraftId,
isAnalyticsOpen,
}),
[
navigateToHome,
navigateToProject,
navigateToSettings,
navigateFromSettings,
navigateToAnalytics,
navigateFromAnalytics,
navigateToWorkspace,
currentWorkspaceId,
currentSettingsSection,
currentProjectId,
currentProjectPathFromState,
pendingSectionId,
pendingDraftId,
isAnalyticsOpen,
]
);
return <RouterContext.Provider value={value}>{props.children}</RouterContext.Provider>;
}
// Disable startTransition wrapping for navigation state updates so they
// batch with other normal-priority React state updates in the same tick.
// Without this, React processes navigation at transition (lower) priority,
// causing a flash of stale UI between normal-priority updates (e.g.
// setIsSending(false)) and the deferred route change.
export function RouterProvider(props: { children: ReactNode }) {
return (
<MemoryRouter initialEntries={[getInitialRoute()]} unstable_useTransitions={false}>
<RouterContextInner>{props.children}</RouterContextInner>
</MemoryRouter>
);
}