Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

EnvironmentIconPicker.tsx

137 lines4.9 KB

1import {
2 ENVIRONMENT_MACHINE_KINDS,
3 isEnvironmentMachineKind,
4 resolveEnvironmentMachineKind,
5 type EnvironmentId,
6 type ServerConfig,
7} from "@t3tools/contracts";
8​
9import { isElectron } from "../../env";
10import { usePrimarySessionState } from "../../environments/primary";
11import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
12import { usePrimaryEnvironmentId } from "../../state/environments";
13import { useEnvironmentSessionState } from "../../state/session";
14import { ENVIRONMENT_MACHINE_KIND_LABELS, EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
15import {
16 MenuItem,
17 MenuRadioGroup,
18 MenuRadioItem,
19 MenuSeparator,
20 MenuSub,
21 MenuSubPopup,
22 MenuSubTrigger,
23} from "../ui/menu";
24import {
25 resolvePrimaryOperateAccess,
26 resolveRemoteOperateAccess,
27} from "./ProviderSettingsPanel.logic";
28​
29/**
30 * Why the picker is inert, in the order the user can do something about it.
31 * Null means it can be changed.
32 */
33export function resolveEnvironmentIconPickerLock(input: {
34 readonly serverConfig: ServerConfig | null;
35 readonly operateAccess: "granted" | "denied" | "pending";
36}): string | null {
37 if (input.serverConfig === null) {
38 return "Connect to this environment to change its icon.";
39 }
40 if (input.serverConfig.environment.capabilities.environmentIcon !== true) {
41 return "This environment's server is too old to keep an icon. Update it to choose one.";
42 }
43 if (input.operateAccess === "denied") {
44 return "Your session on this environment cannot change its settings.";
45 }
46 return null;
48​
49// Same split the provider settings use: the desktop app owns its primary
50// server outright, a browser session on the primary checks its cookie
51// session's scopes, and a remote checks the scopes its own server reports.
52function useEnvironmentOperateAccess(environmentId: EnvironmentId) {
53 const isPrimary = usePrimaryEnvironmentId() === environmentId;
54 const primarySession = usePrimarySessionState();
55 const remoteSession = useEnvironmentSessionState(environmentId);
56 if (isPrimary) {
57 return isElectron
58 ? "granted"
59 : resolvePrimaryOperateAccess({
60 isPrimary: true,
61 hasDesktopBridge: false,
62 session: primarySession.data,
63 isPending: primarySession.isPending,
64 hasError: primarySession.error !== null,
65 });
66 }
67 return resolveRemoteOperateAccess({
68 session: remoteSession.data,
69 isPending: remoteSession.isPending,
70 hasError: remoteSession.hasError,
71 });
73​
74/**
75 * "Icon" submenu for an environment's row menu. Lists the machine kinds with
76 * the server's own detection marked, so the user can tell whether detection
77 * got it right before overriding. Picking the detected kind clears the
78 * override. Locked environments show the reason as a disabled item instead of
79 * hiding the submenu, so the current icon still reads.
80 */
81export function EnvironmentIconMenu({
82 environmentId,
83 serverConfig,
84}: {
85 readonly environmentId: EnvironmentId;
86 readonly serverConfig: ServerConfig | null;
87}) {
88 const updateSettings = useUpdateEnvironmentSettings(environmentId);
89 const operateAccess = useEnvironmentOperateAccess(environmentId);
90 const lock = resolveEnvironmentIconPickerLock({ serverConfig, operateAccess });
91 // With no detection the server falls back to "server", so picking that
92 // kind clears the override the same way picking the detected kind does.
93 const detected = serverConfig?.environment.platform.machine ?? "server";
94 const resolved = resolveEnvironmentMachineKind(serverConfig);
95​
96 return (
97 <MenuSub>
98 <MenuSubTrigger>
99 <EnvironmentMachineIcon kind={resolved} />
100 Icon
101 </MenuSubTrigger>
102 <MenuSubPopup className="min-w-44">
103 {lock !== null ? (
104 <>
105 <MenuItem disabled className="whitespace-normal text-xs">
106 {lock}
107 </MenuItem>
108 <MenuSeparator />
109 </>
110 ) : null}
111 <MenuRadioGroup
112 value={resolved}
113 onValueChange={(next) => {
114 if (lock !== null || !isEnvironmentMachineKind(next)) return;
115 updateSettings({ environmentIcon: next === detected ? null : next });
116 }}
117 >
118 {ENVIRONMENT_MACHINE_KINDS.map((kind) => (
119 <MenuRadioItem key={kind} value={kind} disabled={lock !== null}>
120 <span className="flex min-w-0 items-center gap-2">
121 <EnvironmentMachineIcon kind={kind} className="size-3.5 shrink-0" />
122 <span className="min-w-0 flex-1 truncate">
123 {ENVIRONMENT_MACHINE_KIND_LABELS[kind]}
124 </span>
125 {kind === detected ? (
126 <span className="shrink-0 text-xs text-muted-foreground">
127 {serverConfig?.environment.platform.machine ? "detected" : "default"}
128 </span>
129 ) : null}
130 </span>
131 </MenuRadioItem>
132 ))}
133 </MenuRadioGroup>
134 </MenuSubPopup>
135 </MenuSub>
136 );