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