返回 CodeWhale
surface-states.ts
根目录 / web / lib / surface-states.ts
1 import { DESKTOP_RELEASE, desktopDownload, type DesktopReleaseFact } from "./desktop-release";
2 import { FOUNDER_SWITCH_VALUES, type FounderSwitchValues } from "./founder-switches";
3 import { getChrome } from "./i18n/dictionaries";
4 import type { ChromeDict } from "./i18n/dictionaries/types";
5
6 export const SURFACE_STATES = ["released", "available", "beta", "dev", "open", "invite_only", "not_open", "not_public", "in_source"] as const;
7 export type SurfaceState = (typeof SURFACE_STATES)[number];
8
9 export const SURFACE_IDS = [
10 "terminal",
11 "local-browser",
12 "vscode",
13 "desktop",
14 "hosted-web",
15 "cloud-computers",
16 "accounts",
17 "phone",
18 "computer-use",
19 ] as const;
20 export type SurfaceId = (typeof SURFACE_IDS)[number];
21
22 export type SurfaceTone = "ready" | "attention" | "idle";
23
24 export const STATE_CHROME_KEY = {
25 released: "stateReleased",
26 available: "stateAvailable",
27 beta: "stateBeta",
28 dev: "stateDev",
29 open: "stateOpen",
30 invite_only: "stateInviteOnly",
31 not_open: "stateNotOpen",
32 not_public: "stateNotPublic",
33 in_source: "stateInSource",
34 } as const satisfies Record<SurfaceState, keyof ChromeDict>;
35
36 export const STATE_TONE: Record<SurfaceState, SurfaceTone> = {
37 released: "ready",
38 available: "ready",
39 open: "ready",
40 beta: "attention",
41 dev: "attention",
42 invite_only: "idle",
43 not_open: "idle",
44 not_public: "idle",
45 in_source: "idle",
46 };
47
48 const COMMITTED_STATE: Record<Exclude<SurfaceId, "desktop">, SurfaceState> = {
49 terminal: "released",
50 "local-browser": "released",
51 vscode: "available",
52 "hosted-web": "not_open",
53 "cloud-computers": "invite_only",
54 accounts: "open",
55 phone: "not_public",
56 "computer-use": "beta",
57 };
58
59 export interface SurfaceContext {
60 desktopRelease?: DesktopReleaseFact | null;
61 switches?: FounderSwitchValues;
62 }
63
64 export function surfaceState(id: SurfaceId, context: SurfaceContext = {}): SurfaceState {
65 if (id === "desktop") {
66 return desktopDownload(context.desktopRelease === undefined ? DESKTOP_RELEASE : context.desktopRelease) ? "released" : "dev";
67 }
68 return COMMITTED_STATE[id];
69 }
70
71 export function stateLabel(state: SurfaceState, locale: string): string {
72 return getChrome(locale)[STATE_CHROME_KEY[state]];
73 }
74
75 export interface SurfaceCapabilities {
76 link: boolean;
77 download: boolean;
78 requestAccess: boolean;
79 }
80
81 export function surfaceCapabilities(id: SurfaceId, context: SurfaceContext = {}): SurfaceCapabilities {
82 const state = surfaceState(id, context);
83 const switches = context.switches ?? FOUNDER_SWITCH_VALUES;
84 return {
85 link: state !== "not_open" && state !== "not_public",
86 download: id === "desktop" && state === "released",
87 requestAccess: id === "cloud-computers" && state === "invite_only" && switches.cloudInvitationsOpen,
88 };
89 }
90
90 lines TYPESCRIPT