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

SettingsScopeSentence.tsx

334 lines10.7 KB

1import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
2import { useLocation } from "@tanstack/react-router";
3import { CheckIcon, ChevronDownIcon, LayersIcon } from "lucide-react";
4import { Fragment, useState, type ReactNode } from "react";
5​
6import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
7import { useEnvironments, type EnvironmentPresentation } from "../../state/environments";
8import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
9import { ProjectFavicon } from "../ProjectFavicon";
10import { InlineButton } from "../ui/button";
11import {
12 Combobox,
13 ComboboxEmpty,
14 ComboboxItem,
15 ComboboxList,
16 ComboboxPopup,
17 ComboboxSearchInput,
18 ComboboxTrigger,
19} from "../ui/combobox";
20import {
21 Menu,
22 MenuPopup,
23 MenuRadioGroup,
24 MenuRadioItem,
25 MenuRadioItemIndicator,
26 MenuSeparator,
27 MenuTrigger,
28} from "../ui/menu";
29import { useOptionalSettingsScope } from "./SettingsScopeContext";
30import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope";
31import {
32 ALL_ENVIRONMENTS_VALUE,
33 ALL_PROJECTS_VALUE,
34 environmentAxisValue,
35 projectAxisValue,
36 selectEnvironmentAxis,
37 selectProjectAxis,
38 settingsScopeEnvironmentAddress,
39 settingsScopeEnvironmentLabel,
40 settingsScopeEnvironmentLabels,
41 settingsScopeProjectMachines,
42} from "./settingsScopeAxis";
43​
44/** Pages whose every row is saved on this client; they have no scope to pick. */
45export const SETTINGS_DEVICE_ONLY_PATHS: ReadonlySet<string> = new Set([
46 "/settings/appearance",
47 "/settings/snap-shot",
48 "/settings/connections",
49]);
50​
51interface SettingsScopeMenuProps {
52 readonly value: SettingsScopeSearch;
53 readonly groups: readonly SidebarProjectSnapshot[];
54 readonly environments: readonly EnvironmentPresentation[];
55 readonly singleEnvironment: boolean;
56 readonly onChange: (next: SettingsScopeSearch) => void;
58​
59/**
60 * "Applying settings for <project> across <environment>" at the top of a settings
61 * page. The two pickers are the targets a change is written to. A project is
62 * the same project on every environment, so the environment alone decides
63 * where a project override is written.
64 */
65export function SettingsScopeSentence() {
66 const scope = useOptionalSettingsScope();
67 const pathname = useLocation({ select: (location) => location.pathname });
68 const { environments } = useEnvironments();
69 if (scope === null || SETTINGS_DEVICE_ONLY_PATHS.has(pathname)) return null;
70 const props: SettingsScopeMenuProps = {
71 value: scope.search,
72 singleEnvironment: scope.singleEnvironment,
73 groups: scope.groups,
74 environments,
75 onChange: scope.selectScope,
76 };
77 return (
78 <p className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-1 px-3 text-base text-muted-foreground sm:px-4">
79 {/* Each connective stays with its picker so a wrap never strands "on". */}
80 <span className="flex min-w-0 items-center gap-1.5">
81 <span className="shrink-0">Applying settings for</span>
82 <ProjectScopeMenu {...props} />
83 </span>
84 <span className="flex min-w-0 items-center gap-1.5">
85 <span className="shrink-0">
86 {/* A legacy checkout link names one environment without `machine`. */}
87 {scope.search.machine || scope.scope.kind === "checkout" ? "on" : "across"}
88 </span>
89 <EnvironmentScopeMenu {...props} />
90 </span>
91 </p>
92 );
94​
95interface ScopeOption {
96 value: string;
97 label: string;
98 secondary?: string;
99 icon?: ReactNode;
100 offline?: boolean;
102​
103function ScopeOptionLabel({ option }: { option: ScopeOption }) {
104 return (
105 <>
106 {option.icon}
107 <span className="min-w-0 flex-1">
108 <span className="block truncate">{option.label}</span>
109 {option.secondary ? (
110 <span className="block truncate text-xs text-muted-foreground">{option.secondary}</span>
111 ) : null}
112 </span>
113 {option.offline ? (
114 <span className="shrink-0 text-xs text-muted-foreground">Offline</span>
115 ) : null}
116 </>
117 );
119​
120function ScopeMenu({
121 ariaLabel,
122 icon,
123 label,
124 value,
125 options,
126 allOption,
127 searchLabel,
128 onChange,
129}: {
130 ariaLabel: string;
131 icon: ReactNode;
132 label: string;
133 value: string;
134 options: ScopeOption[];
135 allOption: ScopeOption | null;
136 searchLabel: string;
137 onChange: (next: string) => void;
138}) {
139 const [query, setQuery] = useState("");
140 const Trigger = options.length > 6 ? ComboboxTrigger : MenuTrigger;
141 const trigger = (
142 <Trigger
143 aria-label={`${ariaLabel}: ${label}`}
144 render={<InlineButton tone="picker" />}
145 className="min-w-0 max-w-72"
146 >
147 {icon}
148 <span className="min-w-0 truncate">{label}</span>
149 <ChevronDownIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
150 </Trigger>
151 );
152 const choices = allOption ? [allOption, ...options] : options;
153 if (options.length > 6) {
154 const needle = query.trim().toLowerCase();
155 const filteredItems = choices.filter((option) =>
156 `${option.label} ${option.secondary ?? ""} ${option.value}`.toLowerCase().includes(needle),
157 );
158 return (
159 <Combobox
160 items={choices}
161 filteredItems={filteredItems}
162 filter={null}
163 autoHighlight
164 itemToStringLabel={(option) => option.label}
165 isItemEqualToValue={(a, b) => a.value === b.value}
166 value={choices.find((option) => option.value === value) ?? null}
167 onOpenChange={(open) => {
168 if (!open) setQuery("");
169 }}
170 onValueChange={(option) => {
171 if (option) onChange(option.value);
172 }}
173 >
174 {trigger}
175 <ComboboxPopup align="start" className="w-80">
176 <ComboboxSearchInput
177 aria-label={searchLabel}
178 placeholder={searchLabel}
179 value={query}
180 onChange={(event) => setQuery(event.target.value)}
181 />
182 <ComboboxEmpty>No matches found.</ComboboxEmpty>
183 <ComboboxList>
184 {(option: ScopeOption) => (
185 <ComboboxItem key={option.value} value={option}>
186 <ScopeOptionLabel option={option} />
187 {option.value === value ? (
188 <CheckIcon aria-hidden className="size-3.5 shrink-0" />
189 ) : null}
190 </ComboboxItem>
191 )}
192 </ComboboxList>
193 </ComboboxPopup>
194 </Combobox>
195 );
196 }
197 return (
198 <Menu>
199 {trigger}
200 <MenuPopup align="start">
201 <div className="w-72 max-w-full">
202 <MenuRadioGroup
203 value={value}
204 onValueChange={(next) => {
205 if (typeof next === "string") onChange(next);
206 }}
207 >
208 {choices.map((option, index) => (
209 <Fragment key={option.value}>
210 <MenuRadioItem value={option.value} closeOnClick>
211 <span className="flex min-w-0 items-center gap-2">
212 <ScopeOptionLabel option={option} />
213 <MenuRadioItemIndicator />
214 </span>
215 </MenuRadioItem>
216 {index === 0 && allOption ? <MenuSeparator /> : null}
217 </Fragment>
218 ))}
219 </MenuRadioGroup>
220 </div>
221 </MenuPopup>
222 </Menu>
223 );
225​
226function EnvironmentScopeMenu({
227 value,
228 groups,
229 environments,
230 onChange,
231 singleEnvironment,
232}: SettingsScopeMenuProps) {
233 const resolved = resolveSettingsScope(value, groups, environments);
234 const environmentValue = environmentAxisValue(
235 value,
236 resolved.kind === "checkout" ? resolved.environmentId : null,
237 );
238 const selected = environments.find(
239 (environment) => environment.environmentId === environmentValue,
240 );
241 return (
242 <ScopeMenu
243 ariaLabel="Environment scope"
244 icon={
245 selected ? (
246 <EnvironmentMachineIcon
247 aria-hidden
248 kind={resolveEnvironmentMachineKind(selected.serverConfig)}
249 className="size-3.5 shrink-0"
250 />
251 ) : null
252 }
253 label={
254 selected
255 ? settingsScopeEnvironmentLabel(selected, environments)
256 : environmentValue !== ALL_ENVIRONMENTS_VALUE
257 ? "Unavailable environment"
258 : singleEnvironment
259 ? "No environments"
260 : "All environments"
261 }
262 value={environmentValue}
263 onChange={(next) => onChange(selectEnvironmentAxis(value, next))}
264 searchLabel="Search environments"
265 allOption={
266 singleEnvironment
267 ? null
268 : {
269 value: ALL_ENVIRONMENTS_VALUE,
270 label: "All environments",
271 icon: <LayersIcon aria-hidden className="size-3.5 shrink-0" />,
272 }
273 }
274 options={environments.map((environment) => {
275 const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
276 return {
277 value: environment.environmentId,
278 label: primary,
279 secondary:
280 environments.length <= 1
281 ? ""
282 : secondary || settingsScopeEnvironmentAddress(environment),
283 icon: (
284 <EnvironmentMachineIcon
285 aria-hidden
286 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
287 className="size-3.5 shrink-0"
288 />
289 ),
290 offline: environment.connection.phase !== "connected",
291 };
292 })}
293 />
294 );
296​
297function ProjectScopeMenu({ value, groups, environments, onChange }: SettingsScopeMenuProps) {
298 const selected = groups.find((group) => group.projectKey === value.project);
299 const projects = groups.map((group) => ({
300 group,
301 machines: settingsScopeProjectMachines(group, environments, true).join(", "),
302 }));
303 return (
304 <ScopeMenu
305 ariaLabel="Project scope"
306 icon={selected ? <ProjectFavicon project={selected} className="size-3.5 shrink-0" /> : null}
307 label={selected?.displayName ?? (value.project ? "Unavailable project" : "All projects")}
308 value={projectAxisValue(value)}
309 onChange={(next) => onChange(selectProjectAxis(value, next))}
310 searchLabel="Search projects"
311 allOption={{ value: ALL_PROJECTS_VALUE, label: "All projects" }}
312 options={projects.map(({ group, machines }) => {
313 const sameName = projects.filter(
314 (other) =>
315 other.group.projectKey !== group.projectKey &&
316 other.group.displayName === group.displayName,
317 );
318 return {
319 value: group.projectKey,
320 label: group.displayName,
321 secondary:
322 sameName.length === 0
323 ? environments.length > 1
324 ? group.workspaceRoot
325 : ""
326 : sameName.some((other) => other.machines === machines)
327 ? `${machines} · ${group.workspaceRoot}`
328 : machines,
329 icon: <ProjectFavicon project={group} className="size-3.5 shrink-0" />,
330 };
331 })}
332 />
333 );