File
Blob: src/client/lib/root-workspace-gateway.ts
| 1 | import type { Workspace } from "@/shared/types"; |
| 2 | |
| 3 | type RootWorkspaceDecision = { kind: "redirect"; workspace: Workspace } | { kind: "empty" } | { kind: "unavailable" }; |
| 4 | |
| 5 | interface ResolveRootWorkspaceDecisionInput { |
| 6 | lastVisitedWorkspaceId: string | null; |
| 7 | cachedWorkspaces: Workspace[]; |
| 8 | liveWorkspaces: Workspace[] | null; |
| 9 | } |
| 10 | |
| 11 | function 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 | |
| 19 | export 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 | } |