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

SettingsScopeSentence.tsx

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