Open History to see the last commit
1import { resolveEnvironmentMachineKind, type EnvironmentId } from "@t3tools/contracts";
2import { useLocation, useNavigate } from "@tanstack/react-router";
3import { ChevronRightIcon, LayersIcon } from "lucide-react";
4import type { ReactNode } from "react";
5
6import { cn } from "../../lib/utils";
7import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
8import { useEnvironments, type EnvironmentPresentation } from "../../state/environments";
9import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
10import { ProjectFavicon } from "../ProjectFavicon";
11import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
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 settingsScopeEnvironmentAddress,
19 settingsScopeEnvironmentLabels,
20 settingsScopeProjectMachines,
21} from "./settingsScopeAxis";
22
23interface ScopeChoice {
24 label: string;
25 secondary?: string;
26 icon: ReactNode;
27 search: SettingsScopeSearch;
28}
29
30/** Offer an explicit target change when a category has no settings at this scope. */
31export function SettingsScopeNotice({
32 children,
33 target,
34 targetId,
35 eligibleEnvironmentIds,
36}: {
37 children: string;
38 target: "environment" | "all" | "project" | "checkout";
39 targetId?: string;
40 eligibleEnvironmentIds?: readonly EnvironmentId[];
41}) {
42 const { selectScope, search, groups } = useSettingsScope();
43 const navigate = useNavigate({ from: "/settings" });
44 const pathname = useLocation({ select: (location) => location.pathname });
45 const { environments } = useEnvironments();
46 const environmentIcon = (environment: EnvironmentPresentation | undefined) =>
47 environment ? (
48 <EnvironmentMachineIcon
49 aria-hidden
50 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
51 className="size-4 shrink-0 text-muted-foreground"
52 />
53 ) : null;
54 const choiceGroups =
55 target === "checkout"
56 ? groups.filter((group) => !search.project || group.projectKey === search.project)
57 : [null];
58 const renderChoices = (group: SidebarProjectSnapshot | null) => {
59 const choices: ScopeChoice[] = group
60 ? group.memberProjects.map((member) => {
61 const environment = environments.find(
62 (entry) => entry.environmentId === member.environmentId,
63 );
64 const { primary, secondary } = environment
65 ? settingsScopeEnvironmentLabels(environment, environments)
66 : {
67 primary: member.environmentLabel ?? "Environment",
68 secondary: member.environmentId,
69 };
70 return {
71 label: primary,
72 secondary: [secondary, member.workspaceRoot].filter(Boolean).join(" · "),
73 icon: environmentIcon(environment),
74 search: {
75 project: group.projectKey,
76 machine: member.environmentId,
77 checkout: member.physicalProjectKey,
78 },
79 };
80 })
81 : target === "project"
82 ? groups.map((group) => ({
83 label: group.displayName,
84 secondary: [
85 ...(groups.some(
86 (other) =>
87 other.projectKey !== group.projectKey && other.displayName === group.displayName,
88 )
89 ? settingsScopeProjectMachines(group, environments)
90 : []),
91 group.workspaceRoot,
92 ].join(" · "),
93 icon: <ProjectFavicon project={group} className="size-4 shrink-0" />,
94 search: { project: group.projectKey },
95 }))
96 : target === "environment"
97 ? environments
98 .filter(
99 (entry) =>
100 eligibleEnvironmentIds === undefined ||
101 eligibleEnvironmentIds.includes(entry.environmentId),
102 )
103 .map((entry) => {
104 const { primary, secondary } = settingsScopeEnvironmentLabels(entry, environments);
105 return {
106 label: primary,
107 secondary: secondary || settingsScopeEnvironmentAddress(entry),
108 icon: environmentIcon(entry),
109 search: { machine: entry.environmentId },
110 };
111 })
112 : [
113 {
114 label: "Open all environments",
115 icon: <LayersIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" />,
116 search: {},
117 },
118 ];
119 return choices.map((choice) => (
120 <button
121 key={JSON.stringify(choice.search)}
122 type="button"
123 className={cn(
124 ITEM_ROW_CLASSNAME,
125 "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",
126 )}
127 onClick={() => {
128 if (targetId)
129 void navigate({ to: pathname, search: () => choice.search, hash: targetId });
130 else selectScope(choice.search);
131 }}
132 >
133 {choice.icon}
134 <span className="min-w-0 flex-1">
135 <span className="block truncate text-sm font-medium">{choice.label}</span>
136 {choice.secondary ? (
137 <Tooltip>
138 <TooltipTrigger
139 render={<span className="block truncate text-xs text-muted-foreground" />}
140 >
141 {choice.secondary}
142 </TooltipTrigger>
143 <TooltipPopup>{choice.secondary}</TooltipPopup>
144 </Tooltip>
145 ) : null}
146 </span>
147 <ChevronRightIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" />
148 </button>
149 ));
150 };
151 return (
152 <SettingsPageContainer>
153 <div className="space-y-3">
154 <p role="status" className="px-3 text-sm text-muted-foreground sm:px-4">
155 {children}
156 </p>
157 <div className="space-y-4">
158 {choiceGroups.map((group) =>
159 group?.displayName ? (
160 <SettingsSection key={group.projectKey} title={group.displayName}>
161 {renderChoices(group)}
162 </SettingsSection>
163 ) : (
164 <SettingsGroup key={group?.projectKey ?? target}>
165 {renderChoices(group)}
166 </SettingsGroup>
167 ),
168 )}
169 </div>
170 </div>
171 </SettingsPageContainer>
172 );
173}