Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions apps/mobile/src/components/EnvironmentMachineSymbol.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
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",
desktop: "desktopcomputer",
laptop: "laptopcomputer",
"mac-mini": "macmini",
"mac-studio": "macstudio",
} as const satisfies Record<EnvironmentMachineKind, AppSymbolName>;

export const ENVIRONMENT_MACHINE_KIND_LABELS: Record<EnvironmentMachineKind, string> = {
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<EnvironmentCuratedIconId, AppSymbolName>;

/**
* 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. */
Expand All @@ -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 (
<SymbolView
accessibilityLabel={ENVIRONMENT_MACHINE_KIND_LABELS[kind]}
name={ENVIRONMENT_MACHINE_SYMBOLS[kind]}
accessibilityLabel={ENVIRONMENT_ICON_LABELS[id]}
name={SYMBOL_BY_ID[id]}
size={props.size}
tintColorClassName={props.tintColorClassName}
type="monochrome"
Expand Down
53 changes: 33 additions & 20 deletions apps/web/src/components/EnvironmentMachineIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import {
isEnvironmentMachineKind,
isEnvironmentCuratedIconId,
type EnvironmentCuratedIconId,
type EnvironmentIcon,
type EnvironmentMachineKind,
} from "@t3tools/contracts";
import { CloudIcon, LaptopIcon, MonitorIcon, ServerIcon, type LucideProps } from "lucide-react";
import {
BrainIcon,
CloudIcon,
ContainerIcon,
DatabaseIcon,
GlobeIcon,
HouseIcon,
LaptopIcon,
MonitorIcon,
NetworkIcon,
ServerIcon,
TerminalIcon,
type LucideProps,
} from "lucide-react";
import type { FunctionComponent, SVGProps } from "react";
import { LinuxIcon } from "./Icons";

Expand Down Expand Up @@ -47,40 +60,40 @@ function MacStudioIcon(props: SVGProps<SVGSVGElement>) {
);
}

const ICON_BY_KIND: Record<EnvironmentMachineKind, FunctionComponent<LucideProps>> = {
const ICON_BY_ID: Record<EnvironmentCuratedIconId, FunctionComponent<LucideProps>> = {
server: ServerIcon,
cloud: CloudIcon,
linux: LinuxIcon,
desktop: MonitorIcon,
laptop: LaptopIcon,
"mac-mini": MacMiniIcon,
"mac-studio": MacStudioIcon,
};

export const ENVIRONMENT_MACHINE_KIND_LABELS: Record<EnvironmentMachineKind, string> = {
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<LucideProps> {
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 <Icon {...props} />;
}
54 changes: 50 additions & 4 deletions apps/web/src/components/settings/EnvironmentIconPicker.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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();
});
});
Loading
Loading