Open History to see the last commit
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 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;
57}
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-base 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 );
85}
86
87interface ScopeOption {
88 value: string;
89 label: string;
90 secondary?: string;
91 icon?: ReactNode;
92 offline?: boolean;
93}
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 );
110}
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={<InlineButton tone="picker" />}
137 className="min-w-0 max-w-72"
138 >
139 {icon}
140 <span className="min-w-0 truncate">{label}</span>
141 <ChevronDownIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
142 </Trigger>
143 );
144 const choices = allOption ? [allOption, ...options] : options;
145 if (options.length > 6) {
146 const needle = query.trim().toLowerCase();
147 const filteredItems = choices.filter((option) =>
148 `${option.label} ${option.secondary ?? ""} ${option.value}`.toLowerCase().includes(needle),
149 );
150 return (
151 <Combobox
152 items={choices}
153 filteredItems={filteredItems}
154 filter={null}
155 autoHighlight
156 itemToStringLabel={(option) => option.label}
157 isItemEqualToValue={(a, b) => a.value === b.value}
158 value={choices.find((option) => option.value === value) ?? null}
159 onOpenChange={(open) => {
160 if (!open) setQuery("");
161 }}
162 onValueChange={(option) => {
163 if (option) onChange(option.value);
164 }}
165 >
166 {trigger}
167 <ComboboxPopup align="start" className="w-80">
168 <ComboboxSearchInput
169 aria-label={searchLabel}
170 placeholder={searchLabel}
171 value={query}
172 onChange={(event) => setQuery(event.target.value)}
173 />
174 <ComboboxEmpty>No matches found.</ComboboxEmpty>
175 <ComboboxList>
176 {(option: ScopeOption) => (
177 <ComboboxItem key={option.value} value={option}>
178 <ScopeOptionLabel option={option} />
179 {option.value === value ? (
180 <CheckIcon aria-hidden className="size-3.5 shrink-0" />
181 ) : null}
182 </ComboboxItem>
183 )}
184 </ComboboxList>
185 </ComboboxPopup>
186 </Combobox>
187 );
188 }
189 return (
190 <Menu>
191 {trigger}
192 <MenuPopup align="start">
193 <div className="w-72 max-w-full">
194 <MenuRadioGroup
195 value={value}
196 onValueChange={(next) => {
197 if (typeof next === "string") onChange(next);
198 }}
199 >
200 {choices.map((option, index) => (
201 <Fragment key={option.value}>
202 <MenuRadioItem value={option.value} closeOnClick>
203 <span className="flex min-w-0 items-center gap-2">
204 <ScopeOptionLabel option={option} />
205 <MenuRadioItemIndicator />
206 </span>
207 </MenuRadioItem>
208 {index === 0 && allOption ? <MenuSeparator /> : null}
209 </Fragment>
210 ))}
211 </MenuRadioGroup>
212 </div>
213 </MenuPopup>
214 </Menu>
215 );
216}
217
218function EnvironmentScopeMenu({
219 value,
220 groups,
221 environments,
222 onChange,
223 singleEnvironment,
224}: SettingsScopeMenuProps) {
225 const resolved = resolveSettingsScope(value, groups, environments);
226 const environmentValue = environmentAxisValue(
227 value,
228 resolved.kind === "checkout" ? resolved.environmentId : null,
229 );
230 const selected = environments.find(
231 (environment) => environment.environmentId === environmentValue,
232 );
233 return (
234 <ScopeMenu
235 ariaLabel="Environment scope"
236 icon={
237 selected ? (
238 <EnvironmentMachineIcon
239 aria-hidden
240 kind={resolveEnvironmentMachineKind(selected.serverConfig)}
241 className="size-3.5 shrink-0"
242 />
243 ) : null
244 }
245 label={
246 selected
247 ? settingsScopeEnvironmentLabel(selected, environments)
248 : environmentValue !== ALL_ENVIRONMENTS_VALUE
249 ? "Unavailable environment"
250 : singleEnvironment
251 ? "No environments"
252 : "All environments"
253 }
254 value={environmentValue}
255 onChange={(next) => onChange(selectEnvironmentAxis(value, next))}
256 searchLabel="Search environments"
257 allOption={
258 singleEnvironment
259 ? null
260 : {
261 value: ALL_ENVIRONMENTS_VALUE,
262 label: "All environments",
263 icon: <LayersIcon aria-hidden className="size-3.5 shrink-0" />,
264 }
265 }
266 options={environments.map((environment) => {
267 const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
268 return {
269 value: environment.environmentId,
270 label: primary,
271 secondary:
272 environments.length <= 1
273 ? ""
274 : secondary || settingsScopeEnvironmentAddress(environment),
275 icon: (
276 <EnvironmentMachineIcon
277 aria-hidden
278 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
279 className="size-3.5 shrink-0"
280 />
281 ),
282 offline: environment.connection.phase !== "connected",
283 };
284 })}
285 />
286 );
287}
288
289function ProjectScopeMenu({ value, groups, environments, onChange }: SettingsScopeMenuProps) {
290 const selected = groups.find((group) => group.projectKey === value.project);
291 const projects = projectScopeMenuGroups(groups, value).map((group) => ({
292 group,
293 machines: settingsScopeProjectMachines(group, environments, true).join(", "),
294 }));
295 return (
296 <ScopeMenu
297 ariaLabel="Project scope"
298 icon={selected ? <ProjectFavicon project={selected} className="size-3.5 shrink-0" /> : null}
299 label={selected?.displayName ?? (value.project ? "Unavailable project" : "All projects")}
300 value={projectAxisValue(value)}
301 onChange={(next) => onChange(selectProjectAxis(value, next))}
302 searchLabel="Search projects"
303 allOption={{ value: ALL_PROJECTS_VALUE, label: "All projects" }}
304 options={projects.map(({ group, machines }) => {
305 const sameName = projects.filter(
306 (other) =>
307 other.group.projectKey !== group.projectKey &&
308 other.group.displayName === group.displayName,
309 );
310 return {
311 value: group.projectKey,
312 label: group.displayName,
313 secondary:
314 sameName.length === 0
315 ? environments.length > 1
316 ? group.workspaceRoot
317 : ""
318 : sameName.some((other) => other.machines === machines)
319 ? `${machines} · ${group.workspaceRoot}`
320 : machines,
321 icon: <ProjectFavicon project={group} className="size-3.5 shrink-0" />,
322 };
323 })}
324 />
325 );
326}