Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SidebarChrome.tsx

233 lines7.2 KB

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