Skip to content
File

Blob: src/client/lib/page-load-target.ts

typescript42 lines
1import { SESSION_MODES, type SessionMode } from "@/client/lib/constants";
2import type { ActivePageSurface } from "@/client/lib/active-page-model";
3import { isWorkspaceReady, type WorkspaceRouteState } from "@/client/lib/workspace-route-model";
4import type { Page } from "@/shared/types";
5 
6export type PageLoadTarget = "live" | "cached-page" | "offline-unavailable" | "cache-unavailable";
7 
8interface PageLoadTargetInput {
9 surface: ActivePageSurface;
10 workspaceId: string | null;
11 online: boolean;
12 sessionMode: SessionMode;
13 cachedPage: Page | null;
14 docCached: boolean;
15 route: WorkspaceRouteState | null;
16}
17 
18/**
19 * Decide what to render for the active page. Returns `null` to signal
20 * "wait" — workspace identity is still resolving; no load attempt should
21 * fire yet.
22 */
23export function getPageLoadTarget(input: PageLoadTargetInput): PageLoadTarget | null {
24 if (input.surface === "shared") {
25 return input.workspaceId ? "live" : null;
26 }
27 if (!input.online || input.sessionMode !== SESSION_MODES.AUTHENTICATED) {
28 if (input.cachedPage && input.docCached) return "cached-page";
29 return "offline-unavailable";
30 }
31 const route = input.route;
32 if (!route) return null;
33 if (route.phase === "degraded") {
34 if (input.cachedPage && input.docCached) return "cached-page";
35 return "cache-unavailable";
36 }
37 if (!input.workspaceId || !isWorkspaceReady(route)) {
38 return null;
39 }
40 return "live";
41}