Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SettingsScopeNotice.tsx

172 lines6.4 KB

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