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 const project = groups.find((group) => group.projectKey === value.project);
234 const memberEnvironmentIds = project
235 ? new Set(project.memberProjects.map((member) => member.environmentId))
236 : null;
237 const options = memberEnvironmentIds
238 ? environments.filter((environment) => memberEnvironmentIds.has(environment.environmentId))
239 : environments;
240 return (
241 <ScopeMenu
242 ariaLabel="Environment scope"
243 icon={
244 selected ? (
245 <EnvironmentMachineIcon
246 aria-hidden
247 kind={resolveEnvironmentMachineKind(selected.serverConfig)}
248 className="size-3.5 shrink-0"
249 />
250 ) : null
251 }
252 label={
253 selected
254 ? settingsScopeEnvironmentLabel(selected, environments)
255 : environmentValue !== ALL_ENVIRONMENTS_VALUE
256 ? "Unavailable environment"
257 : singleEnvironment
258 ? "No environments"
259 : "All environments"
260 }
261 value={environmentValue}
262 onChange={(next) => onChange(selectEnvironmentAxis(value, next))}
263 searchLabel="Search environments"
264 allOption={
265 singleEnvironment
266 ? null
267 : {
268 value: ALL_ENVIRONMENTS_VALUE,
269 label: "All environments",
270 icon: <LayersIcon aria-hidden className="size-3.5 shrink-0" />,
271 }
272 }
273 options={options.map((environment) => {
274 const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
275 return {
276 value: environment.environmentId,
277 label: primary,
278 secondary:
279 environments.length <= 1
280 ? ""
281 : secondary || settingsScopeEnvironmentAddress(environment),
282 icon: (
283 <EnvironmentMachineIcon
284 aria-hidden
285 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
286 className="size-3.5 shrink-0"
287 />
288 ),
289 offline: environment.connection.phase !== "connected",
290 };
291 })}
292 />
293 );
294}
295
296function ProjectScopeMenu({ value, groups, environments, onChange }: SettingsScopeMenuProps) {
297 const selected = groups.find((group) => group.projectKey === value.project);
298 const projects = projectScopeMenuGroups(groups, value).map((group) => ({
299 group,
300 machines: settingsScopeProjectMachines(group, environments, true).join(", "),
301 }));
302 return (
303 <ScopeMenu
304 ariaLabel="Project scope"
305 icon={selected ? <ProjectFavicon project={selected} className="size-3.5 shrink-0" /> : null}
306 label={selected?.displayName ?? (value.project ? "Unavailable project" : "All projects")}
307 value={projectAxisValue(value)}
308 onChange={(next) => onChange(selectProjectAxis(value, next))}
309 searchLabel="Search projects"
310 allOption={{ value: ALL_PROJECTS_VALUE, label: "All projects" }}
311 options={projects.map(({ group, machines }) => {
312 const sameName = projects.filter(
313 (other) =>
314 other.group.projectKey !== group.projectKey &&
315 other.group.displayName === group.displayName,
316 );
317 return {
318 value: group.projectKey,
319 label: group.displayName,
320 secondary:
321 sameName.length === 0
322 ? environments.length > 1
323 ? group.workspaceRoot
324 : ""
325 : sameName.some((other) => other.machines === machines)
326 ? `${machines} · ${group.workspaceRoot}`
327 : machines,
328 icon: <ProjectFavicon project={group} className="size-3.5 shrink-0" />,
329 };
330 })}
331 />
332 );
333}