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 { usePullRequestsSupported } 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 pullRequestsSupported = usePullRequestsSupported();
148 const closeMobileSidebar = useCallback(() => {
149 if (isMobile) {
150 setOpenMobile(false);
151 }
152 }, [isMobile, setOpenMobile]);
153 const handlePullRequestsClick = useCallback(() => {
154 closeMobileSidebar();
155 void navigate({
156 to: "/pull-requests",
157 search: readPullRequestListPreferences(),
158 });
159 }, [closeMobileSidebar, navigate]);
160 const handleSettingsClick = useCallback(() => {
161 closeMobileSidebar();
162 void navigate({ to: "/settings" });
163 }, [closeMobileSidebar, navigate]);
164
165 const handleUsageClick = useCallback(() => {
166 if (isMobile) {
167 setOpenMobile(false);
168 }
169 void navigate({ to: "/usage" });
170 }, [isMobile, navigate, setOpenMobile]);
171
172 const handleBackClick = useCallback(() => {
173 closeMobileSidebar();
174 if (canGoBack) {
175 window.history.back();
176 return;
177 }
178 void navigate({ to: "/" });
179 }, [canGoBack, closeMobileSidebar, navigate]);
180
181 return (
182 <SidebarMenu className="flex-row items-center">
183 {currentFooterPage ? (
184 <SidebarMenuItem className="min-w-0 flex-1">
185 <SidebarMenuButton onClick={handleBackClick}>
186 <ArrowLeftIcon />
187 <span>Back</span>
188 </SidebarMenuButton>
189 </SidebarMenuItem>
190 ) : (
191 <>
192 <SidebarUtilityItem
193 icon={<SettingsIcon />}
194 label="Settings"
195 onClick={handleSettingsClick}
196 />
197 {pullRequestsSupported ? (
198 <SidebarUtilityItem
199 icon={<PullRequestGlyph.pullRequest />}
200 label="Pull Requests"
201 onClick={handlePullRequestsClick}
202 />
203 ) : null}
204 <SidebarUtilityItem
205 icon={<ChartNoAxesColumnIcon />}
206 label="Usage"
207 onClick={handleUsageClick}
208 />
209 </>
210 )}
211 <SidebarUpdatePill />
212 </SidebarMenu>
213 );
214});
215
216export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
217 return (
218 <SidebarFooter className="px-[var(--sidebar-content-inset)] py-1">
219 <SidebarProviderUpdatePill />
220 <SidebarUpdateArchitectureWarning />
221 <SidebarUtilityMenu />
222 </SidebarFooter>
223 );
224});