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

SettingsScopeSentence.tsx

344 lines10.9 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 { Button } 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 projectScopeMenuGroups,
37 selectEnvironmentAxis,
38 selectProjectAxis,
39 settingsScopeEnvironmentAddress,
40 settingsScopeEnvironmentLabel,
41 settingsScopeEnvironmentLabels,
42 settingsScopeProjectMachines,
43} from "./settingsScopeAxis";
44​
45export const SETTINGS_DEVICE_ONLY_PATHS: ReadonlySet<string> = new Set([
46 "/settings/general",
47 "/settings/appearance",
48 "/settings/chat",
49 "/settings/diffs",
50 "/settings/snap-shot",
51 "/settings/connections",
52 // Network access and authorized clients belong to the environment serving this app.
53 "/settings/access",
54 "/settings/about",
55]);
56​
57interface SettingsScopeMenuProps {
58 readonly value: SettingsScopeSearch;
59 readonly groups: readonly SidebarProjectSnapshot[];
60 readonly environments: readonly EnvironmentPresentation[];
61 readonly singleEnvironment: boolean;
62 readonly onChange: (next: SettingsScopeSearch) => void;
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-sm text-muted-foreground sm:px-4">
79 <span className="flex min-w-0 items-center gap-1.5">
80 <span className="shrink-0">Applying settings for</span>
81 <ProjectScopeMenu {...props} />
82 </span>
83 <span className="flex min-w-0 items-center gap-1.5">
84 <span className="shrink-0">
85 {scope.search.machine || scope.scope.kind === "checkout" ? "on" : "across"}
86 </span>
87 <EnvironmentScopeMenu {...props} />
88 </span>
89 </p>
90 );
92​
93interface ScopeOption {
94 value: string;
95 label: string;
96 secondary?: string;
97 icon?: ReactNode;
98 offline?: boolean;
100​
101function ScopeOptionLabel({ option }: { option: ScopeOption }) {
102 return (
103 <>
104 {option.icon}
105 <span className="min-w-0 flex-1">
106 <span className="block truncate">{option.label}</span>
107 {option.secondary ? (
108 <span className="block truncate text-xs text-muted-foreground">{option.secondary}</span>
109 ) : null}
110 </span>
111 {option.offline ? (
112 <span className="shrink-0 text-xs text-muted-foreground">Offline</span>
113 ) : null}
114 </>
115 );
117​
118function ScopeMenu({
119 ariaLabel,
120 icon,
121 label,
122 value,
123 options,
124 allOption,
125 searchLabel,
126 onChange,
127}: {
128 ariaLabel: string;
129 icon: ReactNode;
130 label: string;
131 value: string;
132 options: ScopeOption[];
133 allOption: ScopeOption | null;
134 searchLabel: string;
135 onChange: (next: string) => void;
136}) {
137 const [query, setQuery] = useState("");
138 const Trigger = options.length > 6 ? ComboboxTrigger : MenuTrigger;
139 const trigger = (
140 <Trigger
141 aria-label={`${ariaLabel}: ${label}`}
142 render={<Button variant="outline" size="sm" />}
143 className="min-w-0 max-w-72"
144 >
145 {icon}
146 <span className="min-w-0 truncate">{label}</span>
147 <ChevronDownIcon
148 aria-hidden
149 strokeWidth={1.75}
150 className="-me-0.5 size-3.5 shrink-0 text-muted-foreground opacity-70"
151 />
152 </Trigger>
153 );
154 const choices = allOption ? [allOption, ...options] : options;
155 if (options.length > 6) {
156 const needle = query.trim().toLowerCase();
157 const filteredItems = choices.filter((option) =>
158 `${option.label} ${option.secondary ?? ""} ${option.value}`.toLowerCase().includes(needle),
159 );
160 return (
161 <Combobox
162 items={choices}
163 filteredItems={filteredItems}
164 filter={null}
165 autoHighlight
166 itemToStringLabel={(option) => option.label}
167 isItemEqualToValue={(a, b) => a.value === b.value}
168 value={choices.find((option) => option.value === value) ?? null}
169 onOpenChange={(open) => {
170 if (!open) setQuery("");
171 }}
172 onValueChange={(option) => {
173 if (option) onChange(option.value);
174 }}
175 >
176 {trigger}
177 <ComboboxPopup align="start" className="w-80">
178 <ComboboxSearchInput
179 aria-label={searchLabel}
180 placeholder={searchLabel}
181 value={query}
182 onChange={(event) => setQuery(event.target.value)}
183 />
184 <ComboboxEmpty>No matches found.</ComboboxEmpty>
185 <ComboboxList>
186 {(option: ScopeOption) => (
187 <ComboboxItem key={option.value} value={option}>
188 <ScopeOptionLabel option={option} />
189 {option.value === value ? (
190 <CheckIcon aria-hidden className="size-3.5 shrink-0" />
191 ) : null}
192 </ComboboxItem>
193 )}
194 </ComboboxList>
195 </ComboboxPopup>
196 </Combobox>
197 );
198 }
199 return (
200 <Menu>
201 {trigger}
202 <MenuPopup align="start">
203 <div className="w-72 max-w-full">
204 <MenuRadioGroup
205 value={value}
206 onValueChange={(next) => {
207 if (typeof next === "string") onChange(next);
208 }}
209 >
210 {choices.map((option, index) => (
211 <Fragment key={option.value}>
212 <MenuRadioItem value={option.value} closeOnClick>
213 <span className="flex min-w-0 items-center gap-2">
214 <ScopeOptionLabel option={option} />
215 <MenuRadioItemIndicator />
216 </span>
217 </MenuRadioItem>
218 {index === 0 && allOption ? <MenuSeparator /> : null}
219 </Fragment>
220 ))}
221 </MenuRadioGroup>
222 </div>
223 </MenuPopup>
224 </Menu>
225 );
227​
228function EnvironmentScopeMenu({
229 value,
230 groups,
231 environments,
232 onChange,
233 singleEnvironment,
234}: SettingsScopeMenuProps) {
235 const resolved = resolveSettingsScope(value, groups, environments);
236 const environmentValue = environmentAxisValue(
237 value,
238 resolved.kind === "checkout" ? resolved.environmentId : null,
239 );
240 const selected = environments.find(
241 (environment) => environment.environmentId === environmentValue,
242 );
243 const project = groups.find((group) => group.projectKey === value.project);
244 const memberEnvironmentIds = project
245 ? new Set(project.memberProjects.map((member) => member.environmentId))
246 : null;
247 const options = memberEnvironmentIds
248 ? environments.filter((environment) => memberEnvironmentIds.has(environment.environmentId))
249 : environments;
250 return (
251 <ScopeMenu
252 ariaLabel="Environment scope"
253 icon={
254 selected ? (
255 <EnvironmentMachineIcon
256 aria-hidden
257 kind={resolveEnvironmentMachineKind(selected.serverConfig)}
258 className="size-3.5 shrink-0"
259 />
260 ) : null
261 }
262 label={
263 selected
264 ? settingsScopeEnvironmentLabel(selected, environments)
265 : environmentValue !== ALL_ENVIRONMENTS_VALUE
266 ? "Unavailable environment"
267 : singleEnvironment
268 ? "No environments"
269 : "All environments"
270 }
271 value={environmentValue}
272 onChange={(next) => onChange(selectEnvironmentAxis(value, next))}
273 searchLabel="Search environments"
274 allOption={
275 singleEnvironment
276 ? null
277 : {
278 value: ALL_ENVIRONMENTS_VALUE,
279 label: "All environments",
280 icon: <LayersIcon aria-hidden className="size-3.5 shrink-0" />,
281 }
282 }
283 options={options.map((environment) => {
284 const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
285 return {
286 value: environment.environmentId,
287 label: primary,
288 secondary:
289 environments.length <= 1
290 ? ""
291 : secondary || settingsScopeEnvironmentAddress(environment),
292 icon: (
293 <EnvironmentMachineIcon
294 aria-hidden
295 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
296 className="size-3.5 shrink-0"
297 />
298 ),
299 offline: environment.connection.phase !== "connected",
300 };
301 })}
302 />
303 );
305​
306function ProjectScopeMenu({ value, groups, environments, onChange }: SettingsScopeMenuProps) {
307 const selected = groups.find((group) => group.projectKey === value.project);
308 const projects = projectScopeMenuGroups(groups, value).map((group) => ({
309 group,
310 machines: settingsScopeProjectMachines(group, environments, true).join(", "),
311 }));
312 return (
313 <ScopeMenu
314 ariaLabel="Project scope"
315 icon={selected ? <ProjectFavicon project={selected} className="size-3.5 shrink-0" /> : null}
316 label={selected?.displayName ?? (value.project ? "Unavailable project" : "All projects")}
317 value={projectAxisValue(value)}
318 onChange={(next) => onChange(selectProjectAxis(value, next))}
319 searchLabel="Search projects"
320 allOption={{ value: ALL_PROJECTS_VALUE, label: "All projects" }}
321 options={projects.map(({ group, machines }) => {
322 const sameName = projects.filter(
323 (other) =>
324 other.group.projectKey !== group.projectKey &&
325 other.group.displayName === group.displayName,
326 );
327 const location =
328 sameName.length === 0
329 ? environments.length > 1
330 ? group.workspaceRoot
331 : ""
332 : sameName.some((other) => other.machines === machines)
333 ? `${machines} · ${group.workspaceRoot}`
334 : machines;
335 return {
336 value: group.projectKey,
337 label: group.displayName,
338 secondary: group.archived ? ["Archived", location].filter(Boolean).join(" · ") : location,
339 icon: <ProjectFavicon project={group} className="size-3.5 shrink-0" />,
340 };
341 })}
342 />
343 );