Open History to see the last commit
1import {
2 ArrowLeftIcon,
3 ChartNoAxesColumnIcon,
4 GitPullRequestIcon,
5 SettingsIcon,
6} from "lucide-react";
7import type { ReactNode } from "react";
8import { memo, useCallback } from "react";
9import { Link, useCanGoBack, useLocation, useNavigate } from "@tanstack/react-router";
10
11import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
12import { cn } from "../../lib/utils";
13import { useEnvironments } from "../../state/environments";
14import {
15 resolveEnvironmentIdentificationPillLabel,
16 resolveSidebarStageBackdropVariant,
17 resolveSidebarStageFocusRingOffsetClass,
18 SidebarStageBackdrop,
19 useEnvironmentStageLabel,
20} from "../SidebarStageBackdrop";
21import { Badge } from "../ui/badge";
22import {
23 SidebarFooter,
24 SidebarHeader,
25 SidebarMenu,
26 SidebarMenuButton,
27 SidebarMenuItem,
28 SidebarTrigger,
29 useSidebar,
30} from "../ui/sidebar";
31import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
32import { SidebarProviderUpdatePill } from "./SidebarProviderUpdatePill";
33import { SidebarUpdateArchitectureWarning, SidebarUpdatePill } from "./SidebarUpdatePill";
34
35export const SidebarChromeHeader = memo(function SidebarChromeHeader({
36 isElectron,
37}: {
38 isElectron: boolean;
39}) {
40 const stageLabel = useEnvironmentStageLabel();
41 const environmentIdentificationMode = useEnvironmentIdentificationMode();
42 const backdropVariant = resolveSidebarStageBackdropVariant(
43 stageLabel,
44 environmentIdentificationMode === "artwork",
45 );
46 const pillLabel =
47 environmentIdentificationMode === "pill"
48 ? resolveEnvironmentIdentificationPillLabel(stageLabel)
49 : null;
50
51 return (
52 <SidebarHeader
53 className={cn(
54 "@container/sidebar-header relative h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center px-3 py-0 md:px-0",
55 isElectron && "drag-region",
56 )}
57 >
58 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
59 <SidebarTrigger
60 className={cn(
61 "relative z-10 md:hidden",
62 backdropVariant &&
63 "focus-visible:ring-white/90 [&_svg]:stroke-white/90! [&_svg]:opacity-100! [&_svg]:hover:stroke-white! [:hover,[data-pressed]]:bg-white/15",
64 backdropVariant && resolveSidebarStageFocusRingOffsetClass(backdropVariant),
65 )}
66 />
67 <SidebarBrand onBackdrop={backdropVariant !== null} />
68 {pillLabel ? (
69 <Badge
70 className="relative z-10 ml-1 rounded-full px-1.5 text-muted-foreground"
71 data-environment-identification="pill"
72 size="sm"
73 variant="secondary"
74 >
75 {pillLabel}
76 </Badge>
77 ) : null}
78 </SidebarHeader>
79 );
80});
81
82function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
83 return (
84 <Link
85 aria-label="Go to threads"
86 className={cn(
87 "relative z-10 ml-[var(--workspace-titlebar-content-left)] hidden h-7 w-fit min-w-0 shrink-0 items-center gap-1 overflow-hidden rounded-md outline-hidden ring-ring focus-visible:ring-2 md:flex",
88 onBackdrop ? "text-white" : "text-foreground",
89 )}
90 to="/"
91 >
92 <T3Wordmark />
93 <span
94 className={cn(
95 "-translate-y-px truncate text-sm font-medium tracking-tight",
96 onBackdrop ? "text-white/70" : "text-muted-foreground",
97 )}
98 >
99 Code
100 </span>
101 </Link>
102 );
103}
104
105function T3Wordmark() {
106 return (
107 <svg
108 aria-label="T3"
109 className="h-2.5 w-auto shrink-0"
110 viewBox="15.5309 37 94.3941 56.96"
111 xmlns="http://www.w3.org/2000/svg"
112 >
113 <path
114 d="M33.4509 93V47.56H15.5309V37H64.3309V47.56H46.4109V93H33.4509ZM86.7253 93.96C82.832 93.96 78.9653 93.4533 75.1253 92.44C71.2853 91.3733 68.032 89.88 65.3653 87.96L70.4053 78.04C72.5386 79.5867 75.0186 80.8133 77.8453 81.72C80.672 82.6267 83.5253 83.08 86.4053 83.08C89.6586 83.08 92.2186 82.44 94.0853 81.16C95.952 79.88 96.8853 78.12 96.8853 75.88C96.8853 73.7467 96.0586 72.0667 94.4053 70.84C92.752 69.6133 90.0853 69 86.4053 69H80.4853V60.44L96.0853 42.76L97.5253 47.4H68.1653V37H107.365V45.4L91.8453 63.08L85.2853 59.32H89.0453C95.9253 59.32 101.125 60.8667 104.645 63.96C108.165 67.0533 109.925 71.0267 109.925 75.88C109.925 79.0267 109.099 81.9867 107.445 84.76C105.792 87.48 103.259 89.6933 99.8453 91.4C96.432 93.1067 92.0586 93.96 86.7253 93.96Z"
115 fill="currentColor"
116 />
117 </svg>
118 );
119}
120
121function SidebarUtilityItem({
122 icon,
123 label,
124 onClick,
125}: {
126 icon: ReactNode;
127 label: string;
128 onClick: () => void;
129}) {
130 return (
131 <SidebarMenuItem className="shrink-0">
132 <Tooltip>
133 <TooltipTrigger
134 render={
135 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
136 {icon}
137 </SidebarMenuButton>
138 }
139 />
140 <TooltipPopup side="top">{label}</TooltipPopup>
141 </Tooltip>
142 </SidebarMenuItem>
143 );
144}
145
146export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
147 const navigate = useNavigate();
148 const canGoBack = useCanGoBack();
149 const { isMobile, setOpenMobile } = useSidebar();
150 const currentFooterPage = useLocation({
151 select: (location) =>
152 /^\/settings(?:\/|$)/.test(location.pathname)
153 ? "settings"
154 : location.pathname === "/usage"
155 ? "usage"
156 : location.pathname === "/pull-requests"
157 ? "pull-requests"
158 : null,
159 });
160 const { environments } = useEnvironments();
161 // The page reads every connected server, so one of them offering pull requests is enough for
162 // the link to lead somewhere.
163 const pullRequestsSupported = environments.some(
164 (environment) => environment.serverConfig?.environment.capabilities.pullRequests === true,
165 );
166 const closeMobileSidebar = useCallback(() => {
167 if (isMobile) {
168 setOpenMobile(false);
169 }
170 }, [isMobile, setOpenMobile]);
171 const handlePullRequestsClick = useCallback(() => {
172 closeMobileSidebar();
173 void navigate({ to: "/pull-requests", search: { involvement: "all", state: "open" } });
174 }, [closeMobileSidebar, navigate]);
175 const handleSettingsClick = useCallback(() => {
176 closeMobileSidebar();
177 void navigate({ to: "/settings" });
178 }, [closeMobileSidebar, navigate]);
179
180 const handleUsageClick = useCallback(() => {
181 if (isMobile) {
182 setOpenMobile(false);
183 }
184 void navigate({ to: "/usage" });
185 }, [isMobile, navigate, setOpenMobile]);
186
187 const handleBackClick = useCallback(() => {
188 closeMobileSidebar();
189 if (canGoBack) {
190 window.history.back();
191 return;
192 }
193 void navigate({ to: "/" });
194 }, [canGoBack, closeMobileSidebar, navigate]);
195
196 return (
197 <SidebarMenu className="flex-row items-center">
198 {currentFooterPage ? (
199 <SidebarMenuItem className="min-w-0 flex-1">
200 <SidebarMenuButton onClick={handleBackClick}>
201 <ArrowLeftIcon />
202 <span>Back</span>
203 </SidebarMenuButton>
204 </SidebarMenuItem>
205 ) : (
206 <>
207 <SidebarUtilityItem
208 icon={<SettingsIcon />}
209 label="Settings"
210 onClick={handleSettingsClick}
211 />
212 {pullRequestsSupported ? (
213 <SidebarUtilityItem
214 icon={<GitPullRequestIcon />}
215 label="Pull Requests"
216 onClick={handlePullRequestsClick}
217 />
218 ) : null}
219 <SidebarUtilityItem
220 icon={<ChartNoAxesColumnIcon />}
221 label="Usage"
222 onClick={handleUsageClick}
223 />
224 </>
225 )}
226 <SidebarUpdatePill />
227 </SidebarMenu>
228 );
229});
230
231export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
232 return (
233 <SidebarFooter className="p-[var(--sidebar-content-inset)]">
234 <SidebarProviderUpdatePill />
235 <SidebarUpdateArchitectureWarning />
236 <SidebarUtilityMenu />
237 </SidebarFooter>
238 );
239});