Skip to content
File

Blob: src/client/lib/root-workspace-gateway.ts

typescript40 lines
1import type { Workspace } from "@/shared/types";
2 
3type RootWorkspaceDecision = { kind: "redirect"; workspace: Workspace } | { kind: "empty" } | { kind: "unavailable" };
4 
5interface ResolveRootWorkspaceDecisionInput {
6 lastVisitedWorkspaceId: string | null;
7 cachedWorkspaces: Workspace[];
8 liveWorkspaces: Workspace[] | null;
9}
10 
11function findPreferredWorkspace(workspaces: Workspace[], lastVisitedId: string | null): Workspace | null {
12 if (lastVisitedId) {
13 const match = workspaces.find((w) => w.id === lastVisitedId);
14 if (match) return match;
15 }
16 return workspaces[0] ?? null;
17}
18 
19export function resolveRootWorkspaceDecision({
20 lastVisitedWorkspaceId,
21 cachedWorkspaces,
22 liveWorkspaces,
23}: ResolveRootWorkspaceDecisionInput): RootWorkspaceDecision {
24 if (liveWorkspaces !== null) {
25 const liveTarget = findPreferredWorkspace(liveWorkspaces, lastVisitedWorkspaceId);
26 if (liveTarget) {
27 return { kind: "redirect", workspace: liveTarget };
28 }
29 
30 return { kind: "empty" };
31 }
32 
33 const cachedTarget = findPreferredWorkspace(cachedWorkspaces, lastVisitedWorkspaceId);
34 if (cachedTarget) {
35 return { kind: "redirect", workspace: cachedTarget };
36 }
37 
38 return { kind: "unavailable" };
39}