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

SettingsScopeNotice.tsx

173 lines6.5 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 ...(group.archived ? ["Archived"] : []),
88 ...(groups.some(
89 (other) =>
90 other.projectKey !== group.projectKey && other.displayName === group.displayName,
91 )
92 ? settingsScopeProjectMachines(group, environments)
93 : []),
94 group.workspaceRoot,
95 ].join(" · "),
96 icon: <ProjectFavicon project={group} className="size-4 shrink-0" />,
97 search: { project: group.projectKey },
98 }))
99 : target === "environment"
100 ? environments
101 .filter(
102 (entry) =>
103 eligibleEnvironmentIds === undefined ||
104 eligibleEnvironmentIds.includes(entry.environmentId),
105 )
106 .map((entry) => {
107 const { primary, secondary } = settingsScopeEnvironmentLabels(entry, environments);
108 return {
109 label: primary,
110 secondary: secondary || settingsScopeEnvironmentAddress(entry),
111 icon: environmentIcon(entry),
112 search: { machine: entry.environmentId },
113 };
114 })
115 : [
116 {
117 label: "Open all environments",
118 icon: <LayersIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" />,
119 search: {},
120 },
121 ];
122 return choices.map((choice) => (
123 <button
124 key={JSON.stringify(choice.search)}
125 type="button"
126 className={cn(
127 ITEM_ROW_CLASSNAME,
128 "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",
129 )}
130 onClick={() => {
131 if (targetId)
132 void navigate({ to: pathname, search: () => choice.search, hash: targetId });
133 else selectScope(choice.search);
134 }}
135 >
136 {choice.icon}
137 <span className="min-w-0 flex-1">
138 <span className="block truncate text-sm font-medium">{choice.label}</span>
139 {choice.secondary ? (
140 <Tooltip>
141 <TooltipTrigger
142 render={<span className="block truncate text-xs text-muted-foreground" />}
143 >
144 {choice.secondary}
145 </TooltipTrigger>
146 <TooltipPopup>{choice.secondary}</TooltipPopup>
147 </Tooltip>
148 ) : null}
149 </span>
150 <ChevronRightIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" />
151 </button>
152 ));
153 };
154 const notice = (
155 <div className="space-y-3">
156 <p role="status" className="px-3 text-sm text-muted-foreground sm:px-4">
157 {children}
158 </p>
159 <div className="space-y-4">
160 {choiceGroups.map((group) =>
161 group?.displayName ? (
162 <SettingsSection key={group.projectKey} title={group.displayName}>
163 {renderChoices(group)}
164 </SettingsSection>
165 ) : (
166 <SettingsGroup key={group?.projectKey ?? target}>{renderChoices(group)}</SettingsGroup>
167 ),
168 )}
169 </div>
170 </div>
171 );
172 return inline ? notice : <SettingsPageContainer>{notice}</SettingsPageContainer>;