Open History to see the last commit
1import { resolveEnvironmentMachineKind, type EnvironmentId } from "@t3tools/contracts";
2import { useNavigate } from "@tanstack/react-router";
3import { useGT } from "gt-react";
4import { ChevronRightIcon, LayersIcon } from "lucide-react";
5import type { ReactNode } from "react";
6
7import { cn } from "../../lib/utils";
8import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
9import { useEnvironments, type EnvironmentPresentation } from "../../state/environments";
10import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
11import { ProjectFavicon } from "../ProjectFavicon";
12import { ITEM_ROW_CLASSNAME } from "./itemRows";
13import { SettingsGroup } from "./SettingsGroup";
14import { SettingsPageContainer, SettingsSection } from "./settingsLayout";
15import { useSettingsScope } from "./SettingsScopeContext";
16import type { SettingsScopeSearch } from "./settingsScope";
17import {
18 projectScopeMenuGroups,
19 selectProjectAxis,
20 settingsScopeEnvironmentAddress,
21 settingsScopeEnvironmentLabels,
22 settingsScopeProjectMachines,
23} from "./settingsScopeAxis";
24import { useRenderedSettingsPathname } from "./useRenderedSettingsPathname";
25import { SharedTooltipTrigger } from "../ui/shared-tooltip";
26
27interface ScopeChoice {
28 label: string;
29 secondary?: string;
30 icon: ReactNode;
31 search: SettingsScopeSearch;
32}
33
34export function SettingsScopeNotice({
35 children,
36 target,
37 targetId,
38 eligibleEnvironmentIds,
39 inline = false,
40}: {
41 children: string;
42 /** Renders inside a section instead of as its own page. */
43 inline?: boolean;
44 target: "environment" | "all" | "project" | "checkout";
45 targetId?: string;
46 eligibleEnvironmentIds?: readonly EnvironmentId[];
47}) {
48 const gt = useGT();
49 const { selectScope, search, groups } = useSettingsScope();
50 const navigate = useNavigate({ from: "/settings" });
51 const pathname = useRenderedSettingsPathname();
52 const { environments } = useEnvironments();
53 const environmentIcon = (environment: EnvironmentPresentation | undefined) =>
54 environment ? (
55 <EnvironmentMachineIcon
56 aria-hidden
57 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
58 className="size-4 shrink-0 text-muted-foreground"
59 />
60 ) : null;
61 const choiceGroups =
62 target === "checkout"
63 ? groups.filter((group) => !search.project || group.projectKey === search.project)
64 : [null];
65 const projectGroups = projectScopeMenuGroups(groups, search);
66 const renderChoices = (group: SidebarProjectSnapshot | null) => {
67 const choices: ScopeChoice[] = group
68 ? group.memberProjects.map((member) => {
69 const environment = environments.find(
70 (entry) => entry.environmentId === member.environmentId,
71 );
72 const { primary, secondary } = environment
73 ? settingsScopeEnvironmentLabels(environment, environments)
74 : {
75 primary: member.environmentLabel ?? gt("Environment"),
76 secondary: member.environmentId,
77 };
78 return {
79 label: primary,
80 secondary: [secondary, member.workspaceRoot].filter(Boolean).join(" · "),
81 icon: environmentIcon(environment),
82 search: {
83 project: group.projectKey,
84 machine: member.environmentId,
85 checkout: member.physicalProjectKey,
86 },
87 };
88 })
89 : target === "project"
90 ? projectGroups.map((group) => ({
91 label: group.displayName,
92 secondary: [
93 ...(group.archived ? [gt("Archived", { $context: "project state" })] : []),
94 ...(projectGroups.some(
95 (other) =>
96 other.projectKey !== group.projectKey && other.displayName === group.displayName,
97 )
98 ? settingsScopeProjectMachines(group, environments)
99 : []),
100 group.workspaceRoot,
101 ].join(" · "),
102 icon: <ProjectFavicon project={group} className="size-4 shrink-0" />,
103 search: selectProjectAxis(search, group.projectKey),
104 }))
105 : target === "environment"
106 ? environments
107 .filter(
108 (entry) =>
109 eligibleEnvironmentIds === undefined ||
110 eligibleEnvironmentIds.includes(entry.environmentId),
111 )
112 .map((entry) => {
113 const { primary, secondary } = settingsScopeEnvironmentLabels(entry, environments);
114 return {
115 label: primary,
116 secondary: secondary || settingsScopeEnvironmentAddress(entry),
117 icon: environmentIcon(entry),
118 search: { machine: entry.environmentId },
119 };
120 })
121 : [
122 {
123 label: gt("Open all environments"),
124 icon: <LayersIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" />,
125 search: {},
126 },
127 ];
128 return choices.map((choice) => (
129 <button
130 key={JSON.stringify(choice.search)}
131 type="button"
132 className={cn(
133 ITEM_ROW_CLASSNAME,
134 "flex w-full min-w-0 cursor-pointer items-center gap-3 text-left outline-none hover:bg-accent/50 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
135 )}
136 onClick={() => {
137 if (targetId)
138 void navigate({
139 to: pathname,
140 search: () => ({
141 project: undefined,
142 machine: undefined,
143 checkout: undefined,
144 ...choice.search,
145 }),
146 hash: targetId,
147 });
148 else selectScope(choice.search);
149 }}
150 >
151 {choice.icon}
152 <span className="min-w-0 flex-1">
153 <span className="block truncate text-sm font-medium">{choice.label}</span>
154 {choice.secondary ? (
155 <SharedTooltipTrigger
156 render={<span className="block truncate text-xs text-muted-foreground" />}
157 label={choice.secondary}
158 >
159 {choice.secondary}
160 </SharedTooltipTrigger>
161 ) : null}
162 </span>
163 <ChevronRightIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" />
164 </button>
165 ));
166 };
167 const notice = (
168 <div className="space-y-3">
169 <p role="status" className="px-3 text-sm text-muted-foreground sm:px-4">
170 {children}
171 </p>
172 <div className="space-y-4">
173 {choiceGroups.map((group) =>
174 group?.displayName ? (
175 <SettingsSection key={group.projectKey} title={group.displayName}>
176 {renderChoices(group)}
177 </SettingsSection>
178 ) : (
179 <SettingsGroup key={group?.projectKey ?? target}>{renderChoices(group)}</SettingsGroup>
180 ),
181 )}
182 </div>
183 </div>
184 );
185 return inline ? notice : <SettingsPageContainer>{notice}</SettingsPageContainer>;
186}