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