Open History to see the last commit
1import { ArrowLeftIcon, ChartNoAxesColumnIcon, SettingsIcon } from "lucide-react";
2import type { ReactNode } from "react";
3import { memo, useCallback } from "react";
4import { Link, useLocation, useNavigate } from "@tanstack/react-router";
5
6import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
7import { cn } from "../../lib/utils";
8import { usePullRequestsSupported } from "../../state/environments";
9import { T3Wordmark } from "../T3Wordmark";
10import {
11 resolveEnvironmentIdentificationPillLabel,
12 resolveSidebarStageBackdropVariant,
13 SidebarStageBackdrop,
14 useEnvironmentStageLabel,
15} from "../SidebarStageBackdrop";
16import { Badge } from "../ui/badge";
17import {
18 SidebarFooter,
19 SidebarMenu,
20 SidebarMenuButton,
21 SidebarMenuItem,
22 SidebarTrigger,
23 useSidebar,
24} from "../ui/sidebar";
25import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
26import { readPullRequestListPreferences } from "../pullRequest/pullRequestListPreferences";
27import { isSidebarUtilityPage, useNavigateToMainApp } from "./mainAppLocation";
28import { SidebarThreadUndoNotice } from "./SidebarThreadUndoNotice";
29import { SidebarProviderUpdatePill } from "./SidebarProviderUpdatePill";
30import { SidebarUpdateArchitectureWarning, SidebarUpdatePill } from "./SidebarUpdatePill";
31import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
32import { observeResize } from "~/lib/observeResize";
33
34export const SidebarChromeHeader = memo(function SidebarChromeHeader({
35 isElectron,
36}: {
37 isElectron: boolean;
38}) {
39 const stageLabel = useEnvironmentStageLabel();
40 const environmentIdentificationMode = useEnvironmentIdentificationMode();
41 const backdropVariant = resolveSidebarStageBackdropVariant(
42 stageLabel,
43 environmentIdentificationMode === "artwork",
44 );
45 const pillLabel =
46 environmentIdentificationMode === "pill"
47 ? resolveEnvironmentIdentificationPillLabel(stageLabel)
48 : null;
49
50 return (
51 // The titlebar row, not a padded SidebarHeader: it aligns to the window controls.
52 <div
53 className={cn(
54 "relative flex h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center gap-2 pr-3",
55 isElectron && "drag-region",
56 )}
57 >
58 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
59 {/* Match the fixed control's inset and border compensation inside the sheet's safe area. */}
60 <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">
61 <SidebarTrigger
62 // Over the stage artwork: the media viewer's control-on-imagery treatment.
63 variant={backdropVariant ? "media-navigation" : "ghost"}
64 className="relative top-auto translate-y-0"
65 aria-label="Toggle main sidebar"
66 />
67 </div>
68 {/* One visible line: the pill wraps onto the clipped second line once it no longer fits.
69 The padding keeps the brand's focus ring inside the clip. */}
70 <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">
71 <SidebarBrand onBackdrop={backdropVariant !== null} />
72 {pillLabel ? (
73 <div className="ml-1 flex h-7 items-center">
74 <Badge data-environment-identification="pill" size="sm" variant="secondary">
75 {pillLabel}
76 </Badge>
77 </div>
78 ) : null}
79 </div>
80 </div>
81 );
82});
83
84// Measures the brand at its titlebar inset, plus the header's right padding and the
85// sidebar border, so the sidebar minimum follows font size, zoom and macOS window controls.
86export function SidebarBrandWidthProbe({
87 onWidthChange,
88}: {
89 onWidthChange: (width: number) => void;
90}) {
91 const observeWidth = useCallback(
92 (probe: HTMLDivElement) => {
93 return observeResize(probe, ([entry]) => {
94 if (entry) onWidthChange(entry.borderBoxSize[0]?.inlineSize ?? probe.offsetWidth);
95 });
96 },
97 [onWidthChange],
98 );
99
100 return (
101 <div
102 aria-hidden="true"
103 className="pointer-events-none invisible fixed top-0 left-0 flex w-max border-r border-transparent pr-3"
104 ref={observeWidth}
105 >
106 <div className="ml-[var(--workspace-titlebar-content-left)] flex">
107 <SidebarBrandMark onBackdrop={false} />
108 </div>
109 </div>
110 );
111}
112
113function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
114 return (
115 <Link
116 aria-label="Go to threads"
117 className={cn(
118 "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",
119 onBackdrop ? "text-white" : "text-foreground",
120 )}
121 to="/"
122 >
123 <SidebarBrandMark onBackdrop={onBackdrop} />
124 </Link>
125 );
126}
127
128function SidebarBrandMark({ onBackdrop }: { onBackdrop: boolean }) {
129 return (
130 // Center the visible capitals, without the font's ascender/descender space.
131 // Padding keeps ascenders and round-letter overshoot inside the truncation clip.
132 <span className="inline-flex min-w-0 items-baseline gap-1 text-sm font-medium tracking-tight">
133 <T3Wordmark aria-label="T3" className="h-[1cap] w-auto shrink-0" />
134 <span
135 className={cn(
136 "truncate [text-box:trim-both_cap_alphabetic] supports-[text-box:trim-both_cap_alphabetic]:py-1",
137 onBackdrop ? "text-white/70" : "text-muted-foreground",
138 )}
139 >
140 Code
141 </span>
142 </span>
143 );
144}
145
146function SidebarUtilityItem({
147 icon,
148 label,
149 onClick,
150}: {
151 icon: ReactNode;
152 label: string;
153 onClick: () => void;
154}) {
155 return (
156 <SidebarMenuItem className="shrink-0">
157 <Tooltip>
158 <TooltipTrigger
159 render={
160 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
161 {icon}
162 </SidebarMenuButton>
163 }
164 />
165 <TooltipPopup side="top">{label}</TooltipPopup>
166 </Tooltip>
167 </SidebarMenuItem>
168 );
169}
170
171export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
172 const navigate = useNavigate();
173 const navigateToMainApp = useNavigateToMainApp();
174 const { isMobile, setOpenMobile } = useSidebar();
175 const isOnUtilityPage = useLocation({
176 select: (location) => isSidebarUtilityPage(location.pathname),
177 });
178 const pullRequestsSupported = usePullRequestsSupported();
179 const closeMobileSidebar = useCallback(() => {
180 if (isMobile) {
181 setOpenMobile(false);
182 }
183 }, [isMobile, setOpenMobile]);
184 const handlePullRequestsClick = useCallback(() => {
185 closeMobileSidebar();
186 void navigate({
187 to: "/pull-requests",
188 search: readPullRequestListPreferences(),
189 });
190 }, [closeMobileSidebar, navigate]);
191 const handleSettingsClick = useCallback(() => {
192 closeMobileSidebar();
193 void navigate({ to: "/settings" });
194 }, [closeMobileSidebar, navigate]);
195
196 const handleUsageClick = useCallback(() => {
197 if (isMobile) {
198 setOpenMobile(false);
199 }
200 void navigate({ to: "/usage" });
201 }, [isMobile, navigate, setOpenMobile]);
202
203 const handleBackClick = useCallback(() => {
204 closeMobileSidebar();
205 void navigateToMainApp();
206 }, [closeMobileSidebar, navigateToMainApp]);
207
208 return (
209 <SidebarMenu className="flex-row items-center">
210 {isOnUtilityPage ? (
211 <SidebarMenuItem className="min-w-0 flex-1">
212 <SidebarMenuButton onClick={handleBackClick}>
213 <ArrowLeftIcon />
214 <span>Back</span>
215 </SidebarMenuButton>
216 </SidebarMenuItem>
217 ) : (
218 <>
219 <SidebarUtilityItem
220 icon={<SettingsIcon />}
221 label="Settings"
222 onClick={handleSettingsClick}
223 />
224 {pullRequestsSupported ? (
225 <SidebarUtilityItem
226 icon={<PullRequestGlyph.pullRequest />}
227 label="Pull Requests"
228 onClick={handlePullRequestsClick}
229 />
230 ) : null}
231 <SidebarUtilityItem
232 icon={<ChartNoAxesColumnIcon />}
233 label="Usage"
234 onClick={handleUsageClick}
235 />
236 </>
237 )}
238 <SidebarUpdatePill />
239 </SidebarMenu>
240 );
241});
242
243export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
244 return (
245 <SidebarFooter>
246 <SidebarThreadUndoNotice />
247 <SidebarProviderUpdatePill />
248 <SidebarUpdateArchitectureWarning />
249 <SidebarUtilityMenu />
250 </SidebarFooter>
251 );
252});