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 <div
52 className={cn(
53 "relative flex h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center gap-2 pr-3",
54 isElectron && "drag-region",
55 )}
56 >
57 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
58 <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">
59 <SidebarTrigger
60 variant={backdropVariant ? "media-navigation" : "ghost"}
61 className="relative top-auto translate-y-0"
62 aria-label="Toggle main sidebar"
63 />
64 </div>
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
79export function SidebarBrandWidthProbe({
80 onWidthChange,
81}: {
82 onWidthChange: (width: number) => void;
83}) {
84 const observeWidth = useCallback(
85 (probe: HTMLDivElement) => {
86 return observeResize(probe, ([entry]) => {
87 if (entry) onWidthChange(entry.borderBoxSize[0]?.inlineSize ?? probe.offsetWidth);
88 });
89 },
90 [onWidthChange],
91 );
92
93 return (
94 <div
95 aria-hidden="true"
96 className="pointer-events-none invisible fixed top-0 left-0 flex w-max border-r border-transparent pr-3"
97 ref={observeWidth}
98 >
99 <div className="ml-[var(--workspace-titlebar-content-left)] flex">
100 <SidebarBrandMark onBackdrop={false} />
101 </div>
102 </div>
103 );
104}
105
106function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
107 return (
108 <Link
109 aria-label="Go to threads"
110 className={cn(
111 "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",
112 onBackdrop ? "text-white" : "text-foreground",
113 )}
114 to="/"
115 >
116 <SidebarBrandMark onBackdrop={onBackdrop} />
117 </Link>
118 );
119}
120
121function SidebarBrandMark({ onBackdrop }: { onBackdrop: boolean }) {
122 return (
123 <span className="inline-flex min-w-0 items-baseline gap-1 text-sm font-medium tracking-tight">
124 <T3Wordmark aria-label="T3" className="h-[1cap] w-auto shrink-0" />
125 <span
126 className={cn(
127 "truncate [text-box:trim-both_cap_alphabetic] supports-[text-box:trim-both_cap_alphabetic]:py-1",
128 onBackdrop ? "text-white/70" : "text-muted-foreground",
129 )}
130 >
131 Code
132 </span>
133 </span>
134 );
135}
136
137function SidebarUtilityItem({
138 icon,
139 label,
140 onClick,
141}: {
142 icon: ReactNode;
143 label: string;
144 onClick: () => void;
145}) {
146 return (
147 <SidebarMenuItem className="shrink-0">
148 <Tooltip>
149 <TooltipTrigger
150 render={
151 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
152 {icon}
153 </SidebarMenuButton>
154 }
155 />
156 <TooltipPopup side="top">{label}</TooltipPopup>
157 </Tooltip>
158 </SidebarMenuItem>
159 );
160}
161
162export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
163 const navigate = useNavigate();
164 const navigateToMainApp = useNavigateToMainApp();
165 const { isMobile, setOpenMobile } = useSidebar();
166 const isOnUtilityPage = useLocation({
167 select: (location) => isSidebarUtilityPage(location.pathname),
168 });
169 const pullRequestsSupported = usePullRequestsSupported();
170 const closeMobileSidebar = useCallback(() => {
171 if (isMobile) {
172 setOpenMobile(false);
173 }
174 }, [isMobile, setOpenMobile]);
175 const handlePullRequestsClick = useCallback(() => {
176 closeMobileSidebar();
177 void navigate({
178 to: "/pull-requests",
179 search: readPullRequestListPreferences(),
180 });
181 }, [closeMobileSidebar, navigate]);
182 const handleSettingsClick = useCallback(() => {
183 closeMobileSidebar();
184 void navigate({ to: "/settings" });
185 }, [closeMobileSidebar, navigate]);
186
187 const handleUsageClick = useCallback(() => {
188 if (isMobile) {
189 setOpenMobile(false);
190 }
191 void navigate({ to: "/usage" });
192 }, [isMobile, navigate, setOpenMobile]);
193
194 const handleBackClick = useCallback(() => {
195 closeMobileSidebar();
196 void navigateToMainApp();
197 }, [closeMobileSidebar, navigateToMainApp]);
198
199 return (
200 <SidebarMenu className="flex-row items-center">
201 {isOnUtilityPage ? (
202 <SidebarMenuItem className="min-w-0 flex-1">
203 <SidebarMenuButton onClick={handleBackClick}>
204 <ArrowLeftIcon />
205 <span>Back</span>
206 </SidebarMenuButton>
207 </SidebarMenuItem>
208 ) : (
209 <>
210 <SidebarUtilityItem
211 icon={<SettingsIcon />}
212 label="Settings"
213 onClick={handleSettingsClick}
214 />
215 {pullRequestsSupported ? (
216 <SidebarUtilityItem
217 icon={<PullRequestGlyph.pullRequest />}
218 label="Pull Requests"
219 onClick={handlePullRequestsClick}
220 />
221 ) : null}
222 <SidebarUtilityItem
223 icon={<ChartNoAxesColumnIcon />}
224 label="Usage"
225 onClick={handleUsageClick}
226 />
227 </>
228 )}
229 <SidebarUpdatePill />
230 </SidebarMenu>
231 );
232});
233
234export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
235 return (
236 <SidebarFooter>
237 <SidebarThreadUndoNotice />
238 <SidebarProviderUpdatePill />
239 <SidebarUpdateArchitectureWarning />
240 <SidebarUtilityMenu />
241 </SidebarFooter>
242 );
243});