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