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

SidebarChrome.tsx

323 lines10.1 KB

1import { ArrowLeftIcon, ArrowRightIcon, ChartNoAxesColumnIcon, SettingsIcon } from "lucide-react";
2import type { ReactNode } from "react";
3import { T, useGT } from "gt-react";
4import { memo } from "react";
5import { Link, useLocation, useNavigate } from "@tanstack/react-router";
6​
7import { useAtomValue } from "@effect/atom-react";
8import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
9import { shortcutLabelForCommand } from "../../keybindings";
10import {
11 navigateHistory,
12 useCanNavigateHistory,
13 type HistoryDirection,
14} from "../../navigationHistory";
15import { primaryServerKeybindingsAtom } from "../../state/server";
16import { cn } from "../../lib/utils";
17import { useIssuesSupported, usePullRequestsSupported } from "../../state/environments";
18import { T3Wordmark } from "../T3Wordmark";
19import {
20 resolveEnvironmentIdentificationPillLabel,
21 resolveSidebarStageBackdropVariant,
22 SidebarStageBackdrop,
23 useEnvironmentStageLabel,
24} from "../SidebarStageBackdrop";
25import { Badge } from "../ui/badge";
26import { Button } from "../ui/button";
27import {
28 SidebarFooter,
29 SidebarMenu,
30 SidebarMenuButton,
31 SidebarMenuItem,
32 SidebarTrigger,
33 useSidebar,
34} from "../ui/sidebar";
35import { readPullRequestListPreferences } from "../pullRequest/pullRequestListPreferences";
36import { readIssueListPreferences } from "../issue/issueListPreferences";
37import { IssueGlyph } from "../issue/issuePresentation";
38import { isSidebarUtilityPage, useNavigateToMainApp } from "./mainAppLocation";
39import { SidebarThreadUndoNotice } from "./SidebarThreadUndoNotice";
40import { SidebarProviderUpdatePill } from "./SidebarProviderUpdatePill";
41import { SidebarUpdateArchitectureWarning, SidebarUpdatePill } from "./SidebarUpdatePill";
42import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
43import { observeResize } from "~/lib/observeResize";
44import { SharedTooltipTrigger } from "../ui/shared-tooltip";
45​
46export const SidebarChromeHeader = memo(function SidebarChromeHeader({
47 isElectron,
48}: {
49 isElectron: boolean;
50}) {
51 const stageLabel = useEnvironmentStageLabel();
52 const environmentIdentificationMode = useEnvironmentIdentificationMode();
53 const gt = useGT();
54 const backdropVariant = resolveSidebarStageBackdropVariant(
55 stageLabel,
56 environmentIdentificationMode,
57 );
58 const pillLabel =
59 environmentIdentificationMode === "pill"
60 ? resolveEnvironmentIdentificationPillLabel(stageLabel)
61 : null;
62​
63 return (
64 <div
65 className={cn(
66 "relative flex h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center gap-2 pr-3",
67 isElectron && "drag-region",
68 )}
69 >
70 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
71 <div className="relative top-[calc(var(--workspace-controls-top)-env(safe-area-inset-top))] z-10 ml-[calc(var(--workspace-controls-left)+1px-env(safe-area-inset-left))] flex shrink-0 items-center md:hidden">
72 <SidebarTrigger
73 variant={backdropVariant ? "media-navigation" : "ghost"}
74 className="relative top-auto translate-y-0"
75 aria-label={gt("Toggle main sidebar")}
76 />
77 </div>
78 <div className="relative z-10 flex h-8 min-w-0 flex-1 flex-wrap content-start items-center gap-x-2 overflow-hidden py-0.5">
79 <div className="flex shrink-0 items-center gap-1">
80 <SidebarBrand onBackdrop={backdropVariant !== null} />
81 {isElectron ? (
82 <div className="hidden items-center md:flex">
83 <HistoryNavigationButton direction="back" onBackdrop={backdropVariant !== null} />
84 <HistoryNavigationButton direction="forward" onBackdrop={backdropVariant !== null} />
85 </div>
86 ) : null}
87 </div>
88 {pillLabel ? (
89 <div className="ml-1 flex h-7 items-center">
90 <Badge data-environment-identification="pill" size="sm" variant="secondary">
91 {pillLabel}
92 </Badge>
93 </div>
94 ) : null}
95 </div>
96 </div>
97 );
98});
99​
100export function SidebarBrandWidthProbe({
101 isElectron,
102 onWidthChange,
103}: {
104 isElectron: boolean;
105 onWidthChange: (width: number) => void;
106}) {
107 const observeWidth = (probe: HTMLDivElement) => {
108 return observeResize(probe, ([entry]) => {
109 if (entry) onWidthChange(entry.borderBoxSize[0]?.inlineSize ?? probe.offsetWidth);
110 });
111 };
112​
113 return (
114 <div
115 aria-hidden="true"
116 className="pointer-events-none invisible fixed top-0 left-0 flex w-max border-r border-transparent pr-3"
117 ref={observeWidth}
118 >
119 <div className="ml-[var(--workspace-titlebar-content-left)] flex gap-1">
120 <SidebarBrandMark onBackdrop={false} />
121 {isElectron ? <div className="w-[calc(2*var(--workspace-titlebar-control-size))]" /> : null}
122 </div>
123 </div>
124 );
126​
127function HistoryNavigationButton({
128 direction,
129 onBackdrop,
130}: {
131 direction: HistoryDirection;
132 onBackdrop: boolean;
133}) {
134 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
135 const enabled = useCanNavigateHistory(direction);
136 const gt = useGT();
137 const label = direction === "back" ? gt("Go back") : gt("Go forward");
138 const shortcutLabel = shortcutLabelForCommand(
139 keybindings,
140 direction === "back" ? "navigation.back" : "navigation.forward",
141 );
142 const Icon = direction === "back" ? ArrowLeftIcon : ArrowRightIcon;
143 return (
144 <SharedTooltipTrigger
145 render={
146 <Button
147 aria-label={label}
148 className={cn(
149 "[-webkit-app-region:no-drag]",
150 onBackdrop && "relative top-auto translate-y-0",
151 )}
152 disabled={!enabled}
153 onClick={() => navigateHistory(direction)}
154 size="icon-sm"
155 variant={onBackdrop ? "media-navigation" : "ghost"}
156 />
157 }
158 label={
159 <>
160 {label}
161 {shortcutLabel ? ` (${shortcutLabel})` : ""}
162 </>
163 }
164 side="bottom"
165 >
166 <Icon className="size-3.5" />
167 </SharedTooltipTrigger>
168 );
170​
171function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
172 const gt = useGT();
173 return (
174 <Link
175 aria-label={gt("T3 Code home")}
176 className={cn(
177 "relative z-10 ml-[var(--workspace-titlebar-content-left)] hidden h-7 w-fit min-w-0 shrink-0 items-center overflow-hidden rounded-md outline-hidden ring-ring focus-visible:ring-2 focus-visible:ring-inset md:flex",
178 onBackdrop ? "text-white" : "text-foreground",
179 )}
180 to="/"
181 >
182 <SidebarBrandMark onBackdrop={onBackdrop} />
183 </Link>
184 );
186​
187function SidebarBrandMark({ onBackdrop }: { onBackdrop: boolean }) {
188 return (
189 <span className="inline-flex min-w-0 items-baseline gap-1 text-sm font-medium tracking-tight">
190 <T3Wordmark aria-label="T3" className="h-[1cap] w-auto shrink-0" />
191 <span
192 className={cn(
193 "truncate [text-box:trim-both_cap_alphabetic] supports-[text-box:trim-both_cap_alphabetic]:py-1",
194 onBackdrop ? "text-white/70" : "text-muted-foreground",
195 )}
196 >
197 {/* oxlint-disable-next-line t3code/no-untranslated-copy -- product wordmark */}
198 {"Code"}
199 </span>
200 </span>
201 );
203​
204function SidebarUtilityItem({
205 icon,
206 label,
207 onClick,
208}: {
209 icon: ReactNode;
210 label: string;
211 onClick: () => void;
212}) {
213 return (
214 <SidebarMenuItem className="shrink-0">
215 <SharedTooltipTrigger
216 render={
217 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
218 {icon}
219 </SidebarMenuButton>
220 }
221 label={label}
222 />
223 </SidebarMenuItem>
224 );
226​
227const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
228 const navigate = useNavigate();
229 const navigateToMainApp = useNavigateToMainApp();
230 const gt = useGT();
231 const { isMobile, setOpenMobile } = useSidebar();
232 const isOnUtilityPage = useLocation({
233 select: (location) => isSidebarUtilityPage(location.pathname),
234 });
235 const pullRequestsSupported = usePullRequestsSupported();
236 const issuesSupported = useIssuesSupported();
237 const closeMobileSidebar = () => {
238 if (isMobile) {
239 setOpenMobile(false);
240 }
241 };
242 const handlePullRequestsClick = () => {
243 closeMobileSidebar();
244 void navigate({
245 to: "/pull-requests",
246 search: readPullRequestListPreferences(),
247 });
248 };
249 const handleIssuesClick = () => {
250 closeMobileSidebar();
251 void navigate({ to: "/issues", search: readIssueListPreferences() });
252 };
253 const handleSettingsClick = () => {
254 closeMobileSidebar();
255 void navigate({ to: "/settings" });
256 };
257​
258 const handleUsageClick = () => {
259 if (isMobile) {
260 setOpenMobile(false);
261 }
262 void navigate({ to: "/usage" });
263 };
264​
265 const handleBackClick = () => {
266 closeMobileSidebar();
267 void navigateToMainApp();
268 };
269​
270 return (
271 <SidebarMenu className="flex-row items-center">
272 {isOnUtilityPage ? (
273 <SidebarMenuItem className="min-w-0 flex-1">
274 <SidebarMenuButton onClick={handleBackClick}>
275 <ArrowLeftIcon />
276 <span>
277 <T>Back</T>
278 </span>
279 </SidebarMenuButton>
280 </SidebarMenuItem>
281 ) : (
282 <>
283 <SidebarUtilityItem
284 icon={<SettingsIcon />}
285 label={gt("Settings")}
286 onClick={handleSettingsClick}
287 />
288 {pullRequestsSupported ? (
289 <SidebarUtilityItem
290 icon={<PullRequestGlyph.pullRequest />}
291 label={gt("Pull requests")}
292 onClick={handlePullRequestsClick}
293 />
294 ) : null}
295 {issuesSupported ? (
296 <SidebarUtilityItem
297 icon={<IssueGlyph.issue />}
298 label={gt("Issues", { $context: "sidebar link to the list of issues" })}
299 onClick={handleIssuesClick}
300 />
301 ) : null}
302 <SidebarUtilityItem
303 icon={<ChartNoAxesColumnIcon />}
304 label={gt("Usage")}
305 onClick={handleUsageClick}
306 />
307 </>
308 )}
309 <SidebarThreadUndoNotice />
310 <SidebarUpdatePill />
311 </SidebarMenu>
312 );
313});
314​
315export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
316 return (
317 <SidebarFooter>
318 <SidebarProviderUpdatePill />
319 <SidebarUpdateArchitectureWarning />
320 <SidebarUtilityMenu />
321 </SidebarFooter>
322 );
323});