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, useCanGoBack, useLocation, useNavigate } from "@tanstack/react-router";
5
6import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
7import { cn } from "../../lib/utils";
8import { useEnvironments } from "../../state/environments";
9import { T3Wordmark } from "../T3Wordmark";
10import {
11 resolveEnvironmentIdentificationPillLabel,
12 resolveSidebarStageBackdropVariant,
13 resolveSidebarStageFocusRingOffsetClass,
14 SidebarStageBackdrop,
15 useEnvironmentStageLabel,
16} from "../SidebarStageBackdrop";
17import { Badge } from "../ui/badge";
18import {
19 SidebarFooter,
20 SidebarHeader,
21 SidebarMenu,
22 SidebarMenuButton,
23 SidebarMenuItem,
24 SidebarTrigger,
25 useSidebar,
26} from "../ui/sidebar";
27import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
28import { readPullRequestListPreferences } from "../pullRequest/pullRequestListPreferences";
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 <SidebarHeader
51 className={cn(
52 "@container/sidebar-header relative h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center px-3 py-0 md:px-0",
53 isElectron && "drag-region",
54 )}
55 >
56 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
57 <SidebarTrigger
58 className={cn(
59 "relative z-10 md:hidden",
60 backdropVariant &&
61 "focus-visible:ring-white/90 [&_svg]:stroke-white/90! [&_svg]:opacity-100! [&_svg]:hover:stroke-white! [:hover,[data-pressed]]:bg-white/15",
62 backdropVariant && resolveSidebarStageFocusRingOffsetClass(backdropVariant),
63 )}
64 />
65 <SidebarBrand onBackdrop={backdropVariant !== null} />
66 {pillLabel ? (
67 <Badge
68 className="relative z-10 ml-1 hidden rounded-full px-1.5 text-muted-foreground @[15rem]/sidebar-header:inline-flex"
69 data-environment-identification="pill"
70 size="sm"
71 variant="secondary"
72 >
73 {pillLabel}
74 </Badge>
75 ) : null}
76 </SidebarHeader>
77 );
78});
79
80function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
81 return (
82 <Link
83 aria-label="Go to threads"
84 className={cn(
85 "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",
86 onBackdrop ? "text-white" : "text-foreground",
87 )}
88 to="/"
89 >
90 {/* Center the visible capitals, without the font's ascender/descender space. */}
91 <span className="inline-flex min-w-0 items-baseline gap-1 text-sm font-medium tracking-tight">
92 <T3Wordmark aria-label="T3" className="h-[1cap] w-auto shrink-0" />
93 <span
94 className={cn(
95 "truncate [text-box:trim-both_cap_alphabetic]",
96 onBackdrop ? "text-white/70" : "text-muted-foreground",
97 )}
98 >
99 Code
100 </span>
101 </span>
102 </Link>
103 );
104}
105
106function SidebarUtilityItem({
107 icon,
108 label,
109 onClick,
110}: {
111 icon: ReactNode;
112 label: string;
113 onClick: () => void;
114}) {
115 return (
116 <SidebarMenuItem className="shrink-0">
117 <Tooltip>
118 <TooltipTrigger
119 render={
120 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
121 {icon}
122 </SidebarMenuButton>
123 }
124 />
125 <TooltipPopup side="top">{label}</TooltipPopup>
126 </Tooltip>
127 </SidebarMenuItem>
128 );
129}
130
131export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
132 const navigate = useNavigate();
133 const canGoBack = useCanGoBack();
134 const { isMobile, setOpenMobile } = useSidebar();
135 const currentFooterPage = useLocation({
136 select: (location) =>
137 /^\/settings(?:\/|$)/.test(location.pathname)
138 ? "settings"
139 : /^\/projects\/[^/]+\/?$/.test(location.pathname)
140 ? "project-settings"
141 : location.pathname === "/usage"
142 ? "usage"
143 : location.pathname === "/pull-requests"
144 ? "pull-requests"
145 : null,
146 });
147 const { environments } = useEnvironments();
148 // The page reads every connected server, so one of them offering pull requests is enough for
149 // the link to lead somewhere.
150 const pullRequestsSupported = environments.some(
151 (environment) => environment.serverConfig?.environment.capabilities.pullRequests === true,
152 );
153 const closeMobileSidebar = useCallback(() => {
154 if (isMobile) {
155 setOpenMobile(false);
156 }
157 }, [isMobile, setOpenMobile]);
158 const handlePullRequestsClick = useCallback(() => {
159 closeMobileSidebar();
160 void navigate({
161 to: "/pull-requests",
162 search: readPullRequestListPreferences(),
163 });
164 }, [closeMobileSidebar, navigate]);
165 const handleSettingsClick = useCallback(() => {
166 closeMobileSidebar();
167 void navigate({ to: "/settings" });
168 }, [closeMobileSidebar, navigate]);
169
170 const handleUsageClick = useCallback(() => {
171 if (isMobile) {
172 setOpenMobile(false);
173 }
174 void navigate({ to: "/usage" });
175 }, [isMobile, navigate, setOpenMobile]);
176
177 const handleBackClick = useCallback(() => {
178 closeMobileSidebar();
179 if (canGoBack) {
180 window.history.back();
181 return;
182 }
183 void navigate({ to: "/" });
184 }, [canGoBack, closeMobileSidebar, navigate]);
185
186 return (
187 <SidebarMenu className="flex-row items-center">
188 {currentFooterPage ? (
189 <SidebarMenuItem className="min-w-0 flex-1">
190 <SidebarMenuButton onClick={handleBackClick}>
191 <ArrowLeftIcon />
192 <span>Back</span>
193 </SidebarMenuButton>
194 </SidebarMenuItem>
195 ) : (
196 <>
197 <SidebarUtilityItem
198 icon={<SettingsIcon />}
199 label="Settings"
200 onClick={handleSettingsClick}
201 />
202 {pullRequestsSupported ? (
203 <SidebarUtilityItem
204 icon={<PullRequestGlyph.pullRequest />}
205 label="Pull Requests"
206 onClick={handlePullRequestsClick}
207 />
208 ) : null}
209 <SidebarUtilityItem
210 icon={<ChartNoAxesColumnIcon />}
211 label="Usage"
212 onClick={handleUsageClick}
213 />
214 </>
215 )}
216 <SidebarUpdatePill />
217 </SidebarMenu>
218 );
219});
220
221export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
222 return (
223 <SidebarFooter className="px-[var(--sidebar-content-inset)] py-1">
224 <SidebarProviderUpdatePill />
225 <SidebarUpdateArchitectureWarning />
226 <SidebarUtilityMenu />
227 </SidebarFooter>
228 );
229});