Open History to see the last commit
1import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
2import { useLocation } from "@tanstack/react-router";
3import { ChevronDownIcon, LayersIcon } from "lucide-react";
4import 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 Menu,
13 MenuPopup,
14 MenuRadioGroup,
15 MenuRadioItem,
16 MenuRadioItemIndicator,
17 MenuSeparator,
18 MenuTrigger,
19} from "../ui/menu";
20import { useOptionalSettingsScope } from "./SettingsScopeContext";
21import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope";
22import {
23 ALL_ENVIRONMENTS_VALUE,
24 ALL_PROJECTS_VALUE,
25 environmentAxisValue,
26 projectAxisValue,
27 selectEnvironmentAxis,
28 selectProjectAxis,
29 settingsScopeEnvironmentLabel,
30} from "./settingsScopeAxis";
31
32/** Pages whose every row is saved on this client; they have no scope to pick. */
33export const SETTINGS_DEVICE_ONLY_PATHS: ReadonlySet<string> = new Set([
34 "/settings/appearance",
35 "/settings/snap-shot",
36 "/settings/connections",
37]);
38
39interface SettingsScopeMenuProps {
40 readonly value: SettingsScopeSearch;
41 readonly groups: readonly SidebarProjectSnapshot[];
42 readonly environments: readonly EnvironmentPresentation[];
43 readonly onChange: (next: SettingsScopeSearch) => void;
44}
45
46/**
47 * "Applying settings for <project> across <environment>" at the top of a settings
48 * page. The two pickers are the targets a change is written to. A project is
49 * the same project on every environment, so the environment alone decides
50 * where a project override is written.
51 */
52export function SettingsScopeSentence() {
53 const scope = useOptionalSettingsScope();
54 const pathname = useLocation({ select: (location) => location.pathname });
55 const { environments } = useEnvironments();
56 if (scope === null || SETTINGS_DEVICE_ONLY_PATHS.has(pathname)) return null;
57 const props: SettingsScopeMenuProps = {
58 value: scope.search,
59 groups: scope.groups,
60 environments,
61 onChange: scope.selectScope,
62 };
63 return (
64 <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">
65 {/* Each connective stays with its picker so a wrap never strands "on". */}
66 <span className="flex min-w-0 items-center gap-1.5">
67 <span className="shrink-0">Applying settings for</span>
68 <ProjectScopeMenu {...props} />
69 </span>
70 <span className="flex min-w-0 items-center gap-1.5">
71 <span className="shrink-0">
72 {/* A legacy checkout link names one environment without `machine`. */}
73 {scope.search.machine || scope.scope.kind === "checkout" ? "on" : "across"}
74 </span>
75 <EnvironmentScopeMenu {...props} />
76 </span>
77 </p>
78 );
79}
80
81function ScopeMenu({
82 ariaLabel,
83 icon,
84 label,
85 children,
86}: {
87 ariaLabel: string;
88 icon: ReactNode;
89 label: string;
90 children: ReactNode;
91}) {
92 return (
93 <Menu>
94 <MenuTrigger
95 aria-label={`${ariaLabel}: ${label}`}
96 render={<InlineButton tone="picker" />}
97 className="min-w-0 max-w-72"
98 >
99 {icon}
100 <span className="min-w-0 truncate">{label}</span>
101 <ChevronDownIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
102 </MenuTrigger>
103 <MenuPopup align="start">{children}</MenuPopup>
104 </Menu>
105 );
106}
107
108function EnvironmentScopeMenu({ value, groups, environments, onChange }: SettingsScopeMenuProps) {
109 const resolved = resolveSettingsScope(value, groups, environments);
110 const environmentValue = environmentAxisValue(
111 value,
112 resolved.kind === "checkout" ? resolved.environmentId : null,
113 );
114 const selected = environments.find(
115 (environment) => environment.environmentId === environmentValue,
116 );
117 return (
118 <ScopeMenu
119 ariaLabel="Environment scope"
120 icon={
121 selected ? (
122 <EnvironmentMachineIcon
123 aria-hidden
124 kind={resolveEnvironmentMachineKind(selected.serverConfig)}
125 className="size-3.5 shrink-0"
126 />
127 ) : null
128 }
129 label={
130 selected
131 ? settingsScopeEnvironmentLabel(selected, environments)
132 : environmentValue !== ALL_ENVIRONMENTS_VALUE
133 ? "Unavailable environment"
134 : "All environments"
135 }
136 >
137 <MenuRadioGroup
138 value={environmentValue}
139 onValueChange={(next) => {
140 if (typeof next === "string") onChange(selectEnvironmentAxis(value, next));
141 }}
142 >
143 <MenuRadioItem value={ALL_ENVIRONMENTS_VALUE}>
144 <span className="flex min-w-0 items-center gap-2">
145 <LayersIcon aria-hidden className="size-3.5" />
146 <span className="min-w-0 flex-1 truncate">All environments</span>
147 <MenuRadioItemIndicator />
148 </span>
149 </MenuRadioItem>
150 <MenuSeparator />
151 {environments.map((environment) => (
152 <MenuRadioItem key={environment.environmentId} value={environment.environmentId}>
153 <span className="flex min-w-0 items-center gap-2">
154 <EnvironmentMachineIcon
155 aria-hidden
156 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
157 className="size-3.5"
158 />
159 <span className="min-w-0 flex-1 truncate">
160 {settingsScopeEnvironmentLabel(environment, environments)}
161 </span>
162 {environment.connection.phase === "connected" ? null : (
163 <span className="shrink-0 text-xs text-muted-foreground">Offline</span>
164 )}
165 <MenuRadioItemIndicator />
166 </span>
167 </MenuRadioItem>
168 ))}
169 </MenuRadioGroup>
170 </ScopeMenu>
171 );
172}
173
174function ProjectScopeMenu({ value, groups, onChange }: SettingsScopeMenuProps) {
175 const selected = groups.find((group) => group.projectKey === value.project);
176 return (
177 <ScopeMenu
178 ariaLabel="Project scope"
179 icon={selected ? <ProjectFavicon project={selected} className="size-3.5 shrink-0" /> : null}
180 label={selected?.displayName ?? (value.project ? "Unavailable project" : "All projects")}
181 >
182 <MenuRadioGroup
183 value={projectAxisValue(value)}
184 onValueChange={(next) => {
185 if (typeof next === "string") onChange(selectProjectAxis(value, next));
186 }}
187 >
188 <MenuRadioItem value={ALL_PROJECTS_VALUE}>
189 <span className="flex min-w-0 items-center gap-2">
190 <span className="min-w-0 flex-1 truncate">All projects</span>
191 <MenuRadioItemIndicator />
192 </span>
193 </MenuRadioItem>
194 <MenuSeparator />
195 {groups.map((group) => (
196 <MenuRadioItem key={group.projectKey} value={group.projectKey}>
197 <span className="flex min-w-0 items-center gap-2">
198 <ProjectFavicon project={group} className="size-3.5" />
199 <span className="min-w-0 flex-1 truncate">{group.displayName}</span>
200 <MenuRadioItemIndicator />
201 </span>
202 </MenuRadioItem>
203 ))}
204 </MenuRadioGroup>
205 </ScopeMenu>
206 );
207}