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";
32
33export const SidebarChromeHeader = memo(function SidebarChromeHeader({
34 isElectron,
35}: {
36 isElectron: boolean;
37}) {
38 const stageLabel = useEnvironmentStageLabel();
39 const environmentIdentificationMode = useEnvironmentIdentificationMode();
40 const backdropVariant = resolveSidebarStageBackdropVariant(
41 stageLabel,
42 environmentIdentificationMode === "artwork",
43 );
44 const pillLabel =
45 environmentIdentificationMode === "pill"
46 ? resolveEnvironmentIdentificationPillLabel(stageLabel)
47 : null;
48
49 return (
50 // The titlebar row, not a padded SidebarHeader: it aligns to the window controls.
51 <div
52 className={cn(
53 "relative flex h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center gap-2 px-3 md:pl-0",
54 isElectron && "drag-region",
55 )}
56 >
57 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
58 <SidebarTrigger
59 // Over the stage artwork: the media viewer's control-on-imagery treatment.
60 variant={backdropVariant ? "media-navigation" : "ghost"}
61 className="relative top-auto z-10 translate-y-0 md:hidden"
62 />
63 {/* One visible line: the pill wraps onto the clipped second line once it no longer fits.
64 The padding keeps the brand's focus ring inside the clip. */}
65 <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">
66 <SidebarBrand onBackdrop={backdropVariant !== null} />
67 {pillLabel ? (
68 <div className="ml-1 flex h-7 items-center">
69 <Badge data-environment-identification="pill" size="sm" variant="secondary">
70 {pillLabel}
71 </Badge>
72 </div>
73 ) : null}
74 </div>
75 </div>
76 );
77});
78
79// Measures the brand at its titlebar inset, plus the header's right padding and the
80// sidebar border, so the sidebar minimum follows font size, zoom and macOS window controls.
81export function SidebarBrandWidthProbe({
82 onWidthChange,
83}: {
84 onWidthChange: (width: number) => void;
85}) {
86 const observeWidth = useCallback(
87 (probe: HTMLDivElement) => {
88 const observer = new ResizeObserver(([entry]) => {
89 if (entry) onWidthChange(entry.borderBoxSize[0]?.inlineSize ?? probe.offsetWidth);
90 });
91 observer.observe(probe);
92 return () => observer.disconnect();
93 },
94 [onWidthChange],
95 );
96
97 return (
98 <div
99 aria-hidden="true"
100 className="pointer-events-none invisible fixed top-0 left-0 flex w-max border-r border-transparent pr-3"
101 ref={observeWidth}
102 >
103 <div className="ml-[var(--workspace-titlebar-content-left)] flex">
104 <SidebarBrandMark onBackdrop={false} />
105 </div>
106 </div>
107 );
108}
109
110function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
111 return (
112 <Link
113 aria-label="Go to threads"
114 className={cn(
115 "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 md:flex",
116 onBackdrop ? "text-white" : "text-foreground",
117 )}
118 to="/"
119 >
120 <SidebarBrandMark onBackdrop={onBackdrop} />
121 </Link>
122 );
123}
124
125function SidebarBrandMark({ onBackdrop }: { onBackdrop: boolean }) {
126 return (
127 // Center the visible capitals, without the font's ascender/descender space.
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]",
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});