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