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

SettingsScopeSentence.tsx

448 lines15.0 KB

1import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
2import { fileDisplayName } from "@t3tools/shared/path";
3import { CheckIcon, ChevronDownIcon, FolderIcon, LayersIcon } from "lucide-react";
4import { Fragment, useState, type ReactNode } from "react";
5import { T, useGT, useMessages, Var } from "gt-react";
6​
7import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
8import { useEnvironments, type EnvironmentPresentation } from "../../state/environments";
9import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
10import { ProjectFavicon } from "../ProjectFavicon";
11import { Button } from "../ui/button";
12import {
13 Combobox,
14 ComboboxEmpty,
15 ComboboxItem,
16 ComboboxList,
17 ComboboxPopup,
18 ComboboxSearchInput,
19 ComboboxTrigger,
20} from "../ui/combobox";
21import {
22 Menu,
23 MenuPopup,
24 MenuRadioGroup,
25 MenuRadioItem,
26 MenuRadioItemIndicator,
27 MenuSeparator,
28 MenuTrigger,
29} from "../ui/menu";
30import { PickerRowContent } from "../ui/picker-row-content";
31import { useOptionalSettingsScope } from "./SettingsScopeContext";
32import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope";
33import {
34 ALL_CHECKOUTS_VALUE,
35 ALL_ENVIRONMENTS_VALUE,
36 ALL_PROJECTS_VALUE,
37 environmentAxisValue,
38 projectAxisValue,
39 projectScopeMenuGroups,
40 selectCheckoutAxis,
41 selectEnvironmentAxis,
42 selectProjectAxis,
43 settingsScopeCheckoutChoices,
44 settingsScopeCheckoutLabel,
45 settingsScopeEnvironmentAddress,
46 settingsScopeEnvironmentLabel,
47 settingsScopeEnvironmentLabels,
48 settingsScopeProjectMachines,
49} from "./settingsScopeAxis";
50import { useRenderedSettingsPathname } from "./useRenderedSettingsPathname";
51​
52export const SETTINGS_DEVICE_ONLY_PATHS: ReadonlySet<string> = new Set([
53 "/settings/general",
54 "/settings/appearance",
55 "/settings/chat",
56 "/settings/diffs",
57 "/settings/snap-shot",
58 "/settings/connections",
59 // Access picks its environment on the page itself.
60 "/settings/access",
61 "/settings/about",
62]);
63​
64interface SettingsScopeMenuProps {
65 readonly value: SettingsScopeSearch;
66 readonly groups: readonly SidebarProjectSnapshot[];
67 readonly environments: readonly EnvironmentPresentation[];
68 readonly singleEnvironment: boolean;
69 readonly onChange: (next: SettingsScopeSearch) => void;
71​
72export function SettingsScopeSentence() {
73 const scope = useOptionalSettingsScope();
74 const pathname = useRenderedSettingsPathname();
75 const { environments } = useEnvironments();
76 if (scope === null || SETTINGS_DEVICE_ONLY_PATHS.has(pathname)) return null;
77 const props: SettingsScopeMenuProps = {
78 value: scope.search,
79 singleEnvironment: scope.singleEnvironment,
80 groups: scope.groups,
81 environments,
82 onChange: scope.selectScope,
83 };
84 const group = scope.groups.find((candidate) => candidate.projectKey === scope.search.project);
85 const hasCheckouts = group ? settingsScopeCheckoutChoices(group, scope.search).length > 0 : false;
86 const project = <ProjectScopeMenu {...props} />;
87 const environment = <EnvironmentScopeMenu {...props} />;
88 const checkout = <CheckoutScopeMenu {...props} />;
89 const onEnvironment = scope.search.machine || scope.scope.kind === "checkout";
90 return (
91 <p className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-1 px-3 text-sm text-muted-foreground sm:px-4">
92 {onEnvironment && hasCheckouts ? (
93 <T context="settings scope sentence; each variable is a menu">
94 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
95 <span className="shrink-0">Applying settings for</span>
96 <Var>{project}</Var>
97 </span>
98 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
99 <span className="shrink-0">on</span>
100 <Var>{environment}</Var>
101 </span>
102 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
103 <span className="shrink-0">in</span>
104 <Var>{checkout}</Var>
105 </span>
106 </T>
107 ) : onEnvironment ? (
108 <T context="settings scope sentence; each variable is a menu">
109 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
110 <span className="shrink-0">Applying settings for</span>
111 <Var>{project}</Var>
112 </span>
113 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
114 <span className="shrink-0">on</span>
115 <Var>{environment}</Var>
116 </span>
117 </T>
118 ) : hasCheckouts ? (
119 <T context="settings scope sentence; each variable is a menu">
120 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
121 <span className="shrink-0">Applying settings for</span>
122 <Var>{project}</Var>
123 </span>
124 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
125 <span className="shrink-0">across</span>
126 <Var>{environment}</Var>
127 </span>
128 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
129 <span className="shrink-0">in</span>
130 <Var>{checkout}</Var>
131 </span>
132 </T>
133 ) : (
134 <T context="settings scope sentence; each variable is a menu">
135 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
136 <span className="shrink-0">Applying settings for</span>
137 <Var>{project}</Var>
138 </span>
139 <span className="flex min-w-0 flex-wrap items-center gap-1.5">
140 <span className="shrink-0">across</span>
141 <Var>{environment}</Var>
142 </span>
143 </T>
144 )}
145 </p>
146 );
148​
149interface ScopeOption {
150 value: string;
151 label: string;
152 secondary?: string;
153 icon?: ReactNode;
154 offline?: boolean;
156​
157function ScopeOptionLabel({ option }: { option: ScopeOption }) {
158 return (
159 <>
160 <PickerRowContent
161 icon={option.icon}
162 title={<span className="truncate">{option.label}</span>}
163 description={
164 option.secondary ? (
165 <span className="truncate text-xs text-muted-foreground">{option.secondary}</span>
166 ) : null
167 }
168 />
169 {option.offline ? (
170 <span className="shrink-0 text-xs text-muted-foreground">
171 <T>Offline</T>
172 </span>
173 ) : null}
174 </>
175 );
177​
178function ScopeMenu({
179 ariaLabel,
180 icon,
181 label,
182 value,
183 options,
184 allOption,
185 searchLabel,
186 onChange,
187}: {
188 ariaLabel: string;
189 icon: ReactNode;
190 label: string;
191 value: string;
192 options: ScopeOption[];
193 allOption: ScopeOption | null;
194 searchLabel: string;
195 onChange: (next: string) => void;
196}) {
197 const [query, setQuery] = useState("");
198 const Trigger = options.length > 6 ? ComboboxTrigger : MenuTrigger;
199 const trigger = (
200 <Trigger
201 aria-label={`${ariaLabel}: ${label}`}
202 render={<Button variant="outline" size="sm" />}
203 className="min-w-0 max-w-[min(100%,18rem)]"
204 >
205 {icon}
206 <span className="min-w-0 truncate">{label}</span>
207 <ChevronDownIcon
208 aria-hidden
209 strokeWidth={1.75}
210 className="-me-0.5 size-3.5 shrink-0 text-muted-foreground opacity-70"
211 />
212 </Trigger>
213 );
214 const choices = allOption ? [allOption, ...options] : options;
215 if (options.length > 6) {
216 const needle = query.trim().toLowerCase();
217 const filteredItems = choices.filter((option) =>
218 `${option.label} ${option.secondary ?? ""} ${option.value}`.toLowerCase().includes(needle),
219 );
220 return (
221 <Combobox
222 items={choices}
223 filteredItems={filteredItems}
224 filter={null}
225 autoHighlight
226 itemToStringLabel={(option) => option.label}
227 isItemEqualToValue={(a, b) => a.value === b.value}
228 value={choices.find((option) => option.value === value) ?? null}
229 onOpenChange={(open) => {
230 if (!open) setQuery("");
231 }}
232 onValueChange={(option) => {
233 if (option) onChange(option.value);
234 }}
235 >
236 {trigger}
237 <ComboboxPopup align="start" className="w-80">
238 <ComboboxSearchInput
239 aria-label={searchLabel}
240 placeholder={searchLabel}
241 value={query}
242 onChange={(event) => setQuery(event.target.value)}
243 />
244 <ComboboxEmpty>
245 <T>No matches found.</T>
246 </ComboboxEmpty>
247 <ComboboxList>
248 {(option: ScopeOption) => (
249 <ComboboxItem key={option.value} value={option}>
250 <ScopeOptionLabel option={option} />
251 {option.value === value ? (
252 <CheckIcon aria-hidden className="size-3.5 shrink-0" />
253 ) : null}
254 </ComboboxItem>
255 )}
256 </ComboboxList>
257 </ComboboxPopup>
258 </Combobox>
259 );
260 }
261 return (
262 <Menu>
263 {trigger}
264 <MenuPopup align="start">
265 <div className="w-72 max-w-full">
266 <MenuRadioGroup
267 value={value}
268 onValueChange={(next) => {
269 if (typeof next === "string") onChange(next);
270 }}
271 >
272 {choices.map((option, index) => (
273 <Fragment key={option.value}>
274 <MenuRadioItem value={option.value} closeOnClick>
275 <span className="flex min-w-0 items-center gap-2">
276 <ScopeOptionLabel option={option} />
277 <MenuRadioItemIndicator />
278 </span>
279 </MenuRadioItem>
280 {index === 0 && allOption ? <MenuSeparator /> : null}
281 </Fragment>
282 ))}
283 </MenuRadioGroup>
284 </div>
285 </MenuPopup>
286 </Menu>
287 );
289​
290function EnvironmentScopeMenu({
291 value,
292 groups,
293 environments,
294 onChange,
295 singleEnvironment,
296}: SettingsScopeMenuProps) {
297 const messages = useMessages();
298 const gt = useGT();
299 const resolved = resolveSettingsScope(value, groups, environments, messages);
300 const environmentValue = environmentAxisValue(
301 value,
302 resolved.kind === "checkout" ? resolved.environmentId : null,
303 );
304 const selected = environments.find(
305 (environment) => environment.environmentId === environmentValue,
306 );
307 const project = groups.find((group) => group.projectKey === value.project);
308 const memberEnvironmentIds = project
309 ? new Set(project.memberProjects.map((member) => member.environmentId))
310 : null;
311 const options = memberEnvironmentIds
312 ? environments.filter((environment) => memberEnvironmentIds.has(environment.environmentId))
313 : environments;
314 return (
315 <ScopeMenu
316 ariaLabel={gt`Environment scope`}
317 icon={
318 selected ? (
319 <EnvironmentMachineIcon
320 aria-hidden
321 kind={resolveEnvironmentMachineKind(selected.serverConfig)}
322 className="size-3.5 shrink-0"
323 />
324 ) : null
325 }
326 label={
327 selected
328 ? settingsScopeEnvironmentLabel(selected, environments)
329 : environmentValue !== ALL_ENVIRONMENTS_VALUE
330 ? gt`Unavailable environment`
331 : singleEnvironment
332 ? gt`No environments`
333 : gt`All environments`
334 }
335 value={environmentValue}
336 onChange={(next) => onChange(selectEnvironmentAxis(value, next))}
337 searchLabel={gt`Search environments`}
338 allOption={
339 singleEnvironment
340 ? null
341 : {
342 value: ALL_ENVIRONMENTS_VALUE,
343 label: gt`All environments`,
344 icon: <LayersIcon aria-hidden className="size-3.5 shrink-0" />,
345 }
346 }
347 options={options.map((environment) => {
348 const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
349 return {
350 value: environment.environmentId,
351 label: primary,
352 secondary:
353 environments.length <= 1
354 ? ""
355 : secondary || settingsScopeEnvironmentAddress(environment),
356 icon: (
357 <EnvironmentMachineIcon
358 aria-hidden
359 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
360 className="size-3.5 shrink-0"
361 />
362 ),
363 offline: environment.connection.phase !== "connected",
364 };
365 })}
366 />
367 );
369​
370function CheckoutScopeMenu({ value, groups, onChange }: SettingsScopeMenuProps) {
371 const gt = useGT();
372 const group = groups.find((candidate) => candidate.projectKey === value.project);
373 const checkouts = group ? settingsScopeCheckoutChoices(group, value) : [];
374 if (checkouts.length === 0) return null;
375 const selected = checkouts.find((member) => member.physicalProjectKey === value.checkout);
376 return (
377 <ScopeMenu
378 ariaLabel={gt`Checkout scope`}
379 icon={<FolderIcon aria-hidden className="size-3.5 shrink-0" />}
380 label={
381 selected
382 ? settingsScopeCheckoutLabel(selected, checkouts)
383 : value.checkout
384 ? gt`Unavailable checkout`
385 : gt`All checkouts`
386 }
387 value={value.checkout ?? ALL_CHECKOUTS_VALUE}
388 onChange={(next) =>
389 onChange(
390 selectCheckoutAxis(
391 value,
392 checkouts.find((member) => member.physicalProjectKey === next) ?? null,
393 ),
394 )
395 }
396 searchLabel={gt`Search checkouts`}
397 allOption={{ value: ALL_CHECKOUTS_VALUE, label: gt`All checkouts` }}
398 options={checkouts.map((member) => ({
399 value: member.physicalProjectKey,
400 label: fileDisplayName(member.workspaceRoot),
401 secondary: member.workspaceRoot,
402 }))}
403 />
404 );
406​
407function ProjectScopeMenu({ value, groups, environments, onChange }: SettingsScopeMenuProps) {
408 const gt = useGT();
409 const selected = groups.find((group) => group.projectKey === value.project);
410 const projects = projectScopeMenuGroups(groups, value).map((group) => ({
411 group,
412 machines: settingsScopeProjectMachines(group, environments, true).join(", "),
413 }));
414 return (
415 <ScopeMenu
416 ariaLabel={gt`Project scope`}
417 icon={selected ? <ProjectFavicon project={selected} className="size-3.5 shrink-0" /> : null}
418 label={selected?.displayName ?? (value.project ? gt`Unavailable project` : gt`All projects`)}
419 value={projectAxisValue(value)}
420 onChange={(next) => onChange(selectProjectAxis(value, next))}
421 searchLabel={gt`Search projects`}
422 allOption={{ value: ALL_PROJECTS_VALUE, label: gt`All projects` }}
423 options={projects.map(({ group, machines }) => {
424 const sameName = projects.filter(
425 (other) =>
426 other.group.projectKey !== group.projectKey &&
427 other.group.displayName === group.displayName,
428 );
429 const location =
430 sameName.length === 0
431 ? environments.length > 1
432 ? group.workspaceRoot
433 : ""
434 : sameName.some((other) => other.machines === machines)
435 ? `${machines} · ${group.workspaceRoot}`
436 : machines;
437 return {
438 value: group.projectKey,
439 label: group.displayName,
440 secondary: group.archived
441 ? [gt({ context: "project state" })`Archived`, location].filter(Boolean).join(" · ")
442 : location,
443 icon: <ProjectFavicon project={group} className="size-3.5 shrink-0" />,
444 };
445 })}
446 />
447 );