diff --git a/apps/mobile/src/components/EnvironmentMachineSymbol.tsx b/apps/mobile/src/components/EnvironmentMachineSymbol.tsx index 9f4ce3b22a18b..1ce2b02d92975 100644 --- a/apps/mobile/src/components/EnvironmentMachineSymbol.tsx +++ b/apps/mobile/src/components/EnvironmentMachineSymbol.tsx @@ -1,11 +1,15 @@ import { - isEnvironmentMachineKind, + ENVIRONMENT_ICON_LABELS, + isEnvironmentCuratedIconId, + type EnvironmentCuratedIconId, type EnvironmentIcon, - type EnvironmentMachineKind, } from "@t3tools/contracts"; import { SymbolView, type AppSymbolName } from "./AppSymbol"; -export const ENVIRONMENT_MACHINE_SYMBOLS = { +// Every SF name here is already a key of the Android fallback map, so a new +// id adds nothing to the Metro bundle; only iOS needs a check that the glyph +// exists. +const SYMBOL_BY_ID = { server: "server.rack", cloud: "cloud", linux: "terminal", @@ -13,30 +17,27 @@ export const ENVIRONMENT_MACHINE_SYMBOLS = { laptop: "laptopcomputer", "mac-mini": "macmini", "mac-studio": "macstudio", -} as const satisfies Record; - -export const ENVIRONMENT_MACHINE_KIND_LABELS: Record = { - server: "Server", - cloud: "Cloud VM", - linux: "Linux/WSL", - desktop: "Desktop", - laptop: "Laptop", - "mac-mini": "Mini PC", - "mac-studio": "Workstation", -}; + terminal: "terminal", + database: "internaldrive", + container: "cube", + globe: "globe", + home: "house", + gpu: "brain", + network: "point.3.connected.trianglepath.dotted", +} as const satisfies Record; /** - * The machine kind an icon draws as. A name this build cannot draw (picked - * on a newer client) gets the generic server so the row still reads as a + * The curated id an icon draws as. A name this build cannot draw (picked on + * a newer client) gets the generic server so the row still reads as a * machine. */ -function symbolKind(icon: EnvironmentIcon): EnvironmentMachineKind { - return icon.kind === "icon" && isEnvironmentMachineKind(icon.name) ? icon.name : "server"; +function symbolId(icon: EnvironmentIcon): EnvironmentCuratedIconId { + return icon.kind === "icon" && isEnvironmentCuratedIconId(icon.name) ? icon.name : "server"; } /** The symbol name for places that only take one, such as native menu items. */ export function environmentMachineSymbolName(icon: EnvironmentIcon) { - return ENVIRONMENT_MACHINE_SYMBOLS[symbolKind(icon)]; + return SYMBOL_BY_ID[symbolId(icon)]; } /** The glyph an environment wears in lists; SF Symbols on iOS, Tabler on Android. */ @@ -45,11 +46,11 @@ export function EnvironmentMachineSymbol(props: { readonly size: number; readonly tintColorClassName: string; }) { - const kind = symbolKind(props.icon); + const id = symbolId(props.icon); return ( ) { ); } -const ICON_BY_KIND: Record> = { +const ICON_BY_ID: Record> = { server: ServerIcon, cloud: CloudIcon, linux: LinuxIcon, @@ -55,32 +68,32 @@ const ICON_BY_KIND: Record = { - server: "Server", - cloud: "Cloud VM", - linux: "Linux/WSL", - desktop: "Desktop", - laptop: "Laptop", - "mac-mini": "Mini PC", - "mac-studio": "Workstation", + terminal: TerminalIcon, + database: DatabaseIcon, + container: ContainerIcon, + globe: GlobeIcon, + home: HouseIcon, + gpu: BrainIcon, + network: NetworkIcon, }; /** - * The glyph for a named icon. A name this build cannot draw (picked on a - * newer client) gets the generic server so the row still reads as a machine. + * Which curated glyph draws a named icon. A name this build cannot draw + * (picked on a newer client) gets the generic server so the row still reads + * as a machine. */ +function curatedIconId(icon: EnvironmentIcon): EnvironmentCuratedIconId { + return icon.kind === "icon" && isEnvironmentCuratedIconId(icon.name) ? icon.name : "server"; +} + export function environmentMachineIcon(icon: EnvironmentIcon): FunctionComponent { - return icon.kind === "icon" && isEnvironmentMachineKind(icon.name) - ? ICON_BY_KIND[icon.name] - : ICON_BY_KIND.server; + return ICON_BY_ID[curatedIconId(icon)]; } export function EnvironmentMachineIcon({ icon, ...props }: LucideProps & { readonly icon: EnvironmentIcon }) { - const Icon = environmentMachineIcon(icon); + const Icon = ICON_BY_ID[curatedIconId(icon)]; return ; } diff --git a/apps/web/src/components/settings/EnvironmentIconPicker.test.ts b/apps/web/src/components/settings/EnvironmentIconPicker.test.ts index 25fd930e3b6bc..ae9f242e2ae53 100644 --- a/apps/web/src/components/settings/EnvironmentIconPicker.test.ts +++ b/apps/web/src/components/settings/EnvironmentIconPicker.test.ts @@ -1,11 +1,20 @@ -import type { ServerConfig } from "@t3tools/contracts"; +import { environmentIconForMachineKind, type ServerConfig } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { resolveEnvironmentIconPickerLock } from "./EnvironmentIconPicker"; +import { + resolveEnvironmentIconChoiceLock, + resolveEnvironmentIconPickerLock, + resolveEnvironmentIconWrite, +} from "./EnvironmentIconPicker"; -const config = (environmentIcon: boolean | undefined) => +const config = (environmentIcon: boolean | undefined, environmentIconOverride?: boolean) => ({ - environment: { capabilities: environmentIcon === undefined ? {} : { environmentIcon } }, + environment: { + capabilities: { + ...(environmentIcon === undefined ? {} : { environmentIcon }), + ...(environmentIconOverride === undefined ? {} : { environmentIconOverride }), + }, + }, }) as unknown as ServerConfig; describe("resolveEnvironmentIconPickerLock", () => { @@ -39,3 +48,40 @@ describe("resolveEnvironmentIconPickerLock", () => { ).toBeNull(); }); }); + +describe("resolveEnvironmentIconWrite", () => { + it("clears the override when the pick matches detection", () => { + expect(resolveEnvironmentIconWrite({ next: "laptop", detected: "laptop" })).toBeNull(); + }); + + it("writes a machine kind as its shared reference", () => { + expect(resolveEnvironmentIconWrite({ next: "laptop", detected: "server" })).toBe( + environmentIconForMachineKind("laptop"), + ); + }); + + it("writes a role as a named icon", () => { + expect(resolveEnvironmentIconWrite({ next: "database", detected: "server" })).toEqual({ + kind: "icon", + name: "database", + }); + }); +}); + +describe("resolveEnvironmentIconChoiceLock", () => { + it("never locks a machine kind, which every server stores as a string", () => { + expect( + resolveEnvironmentIconChoiceLock({ serverConfig: config(true), id: "laptop" }), + ).toBeNull(); + expect(resolveEnvironmentIconChoiceLock({ serverConfig: null, id: "laptop" })).toBeNull(); + }); + + it("locks a role until the server stores the object form", () => { + expect( + resolveEnvironmentIconChoiceLock({ serverConfig: config(true), id: "database" }), + ).toMatch(/Update/); + expect( + resolveEnvironmentIconChoiceLock({ serverConfig: config(true, true), id: "database" }), + ).toBeNull(); + }); +}); diff --git a/apps/web/src/components/settings/EnvironmentIconPicker.tsx b/apps/web/src/components/settings/EnvironmentIconPicker.tsx index 372fdf4a70f40..5c9e112a3706a 100644 --- a/apps/web/src/components/settings/EnvironmentIconPicker.tsx +++ b/apps/web/src/components/settings/EnvironmentIconPicker.tsx @@ -1,16 +1,23 @@ import { AuthSettingsWriteScope, - ENVIRONMENT_MACHINE_KINDS, + ENVIRONMENT_CURATED_ICON_IDS, + ENVIRONMENT_ICON_LABELS, environmentIconForMachineKind, + isEnvironmentCuratedIconId, isEnvironmentMachineKind, resolveEnvironmentIcon, + type EnvironmentCuratedIconId, + type EnvironmentIcon, type EnvironmentId, + type EnvironmentMachineKind, type ServerConfig, } from "@t3tools/contracts"; +import { Fragment } from "react"; + import { useUpdateEnvironmentSettings } from "../../hooks/useSettings"; import { useEnvironmentScope } from "../../state/session"; -import { ENVIRONMENT_MACHINE_KIND_LABELS, EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; +import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; import { MenuItem, MenuRadioGroup, @@ -21,6 +28,11 @@ import { MenuSubTrigger, } from "../ui/menu"; +// The curated list starts with the machine kinds; roles follow after a rule. +const ENVIRONMENT_MACHINE_KINDS_COUNT = ENVIRONMENT_CURATED_ICON_IDS.findIndex( + (id) => !isEnvironmentMachineKind(id), +); + /** * Why the picker is inert, in the order the user can do something about it. * Null means it can be changed. @@ -42,7 +54,52 @@ export function resolveEnvironmentIconPickerLock(input: { } /** - * "Icon" submenu for an environment's row menu. Lists the machine kinds with + * What to store for a pick. Picking what the server would draw anyway clears + * the override instead of pinning it, so detection keeps working if the + * machine changes; a machine kind gets the shared reference, so renderers + * memoized on the icon do not repaint for the same pick. + */ +export function resolveEnvironmentIconWrite(input: { + readonly next: EnvironmentCuratedIconId; + readonly detected: EnvironmentMachineKind; +}): EnvironmentIcon | null { + if (input.next === input.detected) return null; + // A plain machine kind encodes to the bare string on the wire, so a server + // that predates the object form accepts it unchanged; a role stays an object. + return isEnvironmentMachineKind(input.next) + ? environmentIconForMachineKind(input.next) + : { kind: "icon", name: input.next }; +} + +/** + * Only the seven machine kinds have a string form an older server accepts; + * a role travels as the object, which such a server rejects. Null means the + * id can be picked. + */ +export function resolveEnvironmentIconChoiceLock(input: { + readonly serverConfig: ServerConfig | null; + readonly id: EnvironmentCuratedIconId; +}): string | null { + if (isEnvironmentMachineKind(input.id)) return null; + return input.serverConfig?.environment.capabilities.environmentIconOverride === true + ? null + : "Update this environment's server to pick an icon beyond its machine kind."; +} + +/** + * Why a pick is unavailable, shown as a disabled row so the list still reads. + * Both the whole-submenu lock and the role-section lock render one of these. + */ +function IconLockNotice({ reason }: { readonly reason: string }) { + return ( + + {reason} + + ); +} + +/** + * "Icon" submenu for an environment's row menu. Lists the curated icons with * the server's own detection marked, so the user can tell whether detection * got it right before overriding. Picking the detected kind clears the * override. Locked environments show the reason as a disabled item instead of @@ -64,9 +121,10 @@ export function EnvironmentIconMenu({ // kind clears the override the same way picking the detected kind does. const detected = serverConfig?.environment.platform.machine ?? "server"; const resolved = resolveEnvironmentIcon(serverConfig); - // The radio list only knows the seven kinds; a richer pick checks nothing. - const resolvedKind = - resolved.kind === "icon" && isEnvironmentMachineKind(resolved.name) ? resolved.name : null; + // The radio list only knows the curated ids; a richer pick checks nothing. + const resolvedId = + resolved.kind === "icon" && isEnvironmentCuratedIconId(resolved.name) ? resolved.name : null; + const roleLock = lock ?? resolveEnvironmentIconChoiceLock({ serverConfig, id: "terminal" }); return ( @@ -77,40 +135,51 @@ export function EnvironmentIconMenu({ {lock !== null ? ( <> - - {lock} - + ) : null} { - if (lock !== null || !isEnvironmentMachineKind(next)) return; - // A plain machine kind encodes to the bare string on the wire, so a - // server that predates the object form accepts this unchanged. - updateSettings({ - environmentIcon: next === detected ? null : { kind: "icon", name: next }, - }); + if (!isEnvironmentCuratedIconId(next)) return; + if (resolveEnvironmentIconChoiceLock({ serverConfig, id: next }) !== null) return; + if (lock !== null) return; + updateSettings({ environmentIcon: resolveEnvironmentIconWrite({ next, detected }) }); }} > - {ENVIRONMENT_MACHINE_KINDS.map((kind) => ( - - - - - {ENVIRONMENT_MACHINE_KIND_LABELS[kind]} + {ENVIRONMENT_CURATED_ICON_IDS.map((id, index) => ( + + {index === ENVIRONMENT_MACHINE_KINDS_COUNT ? ( + <> + + {lock === null && roleLock !== null ? : null} + + ) : null} + + + + {ENVIRONMENT_ICON_LABELS[id]} + {id === detected ? ( + + {serverConfig?.environment.platform.machine ? "detected" : "default"} + + ) : null} - {kind === detected ? ( - - {serverConfig?.environment.platform.machine ? "detected" : "default"} - - ) : null} - - + + ))} diff --git a/packages/contracts/src/environment.ts b/packages/contracts/src/environment.ts index 9ffc7131ad12d..902c7747952db 100644 --- a/packages/contracts/src/environment.ts +++ b/packages/contracts/src/environment.ts @@ -70,6 +70,51 @@ export const EnvironmentMachineKind = Schema.Literals(ENVIRONMENT_MACHINE_KINDS) export type EnvironmentMachineKind = typeof EnvironmentMachineKind.Type; export const isEnvironmentMachineKind = Schema.is(EnvironmentMachineKind); +/** + * Named glyphs every client draws: the machine kinds a server can detect, + * plus roles a machine is given but never detected as. Both renderers hold an + * exhaustive map over this list, so a new id fails typecheck until each + * surface can draw it. Only the machine kinds have a bare-string form on the + * wire; a role always travels as the object, which is why picking one needs + * the `environmentIconOverride` capability. + */ +export const ENVIRONMENT_CURATED_ICON_IDS = [ + ...ENVIRONMENT_MACHINE_KINDS, + "terminal", + "database", + "container", + "globe", + "home", + "gpu", + "network", +] as const; +export const EnvironmentCuratedIconId = Schema.Literals(ENVIRONMENT_CURATED_ICON_IDS); +export type EnvironmentCuratedIconId = typeof EnvironmentCuratedIconId.Type; +export const isEnvironmentCuratedIconId = Schema.is(EnvironmentCuratedIconId); + +/** + * What each curated glyph is called in a picker and in an accessibility + * label. Web and mobile draw different glyph sets for the same id, so the + * name is the one thing both surfaces must agree on, and typecheck only + * catches a missing entry rather than two that drifted apart. + */ +export const ENVIRONMENT_ICON_LABELS: Record = { + server: "Server", + cloud: "Cloud VM", + linux: "Linux/WSL", + desktop: "Desktop", + laptop: "Laptop", + "mac-mini": "Mini PC", + "mac-studio": "Workstation", + terminal: "Dev box", + database: "Database", + container: "Container", + globe: "Edge", + home: "Home server", + gpu: "GPU box", + network: "Network", +}; + const EnvironmentNamedIcon = Schema.Struct({ kind: Schema.Literal("icon"), // A curated id or a Lucide id. Renderers try the curated map first, so the