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