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

SidebarChrome.tsx

234 lines7.3 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 group-data-[collapsible=icon]:hidden",
90 onBackdrop ? "text-white" : "text-foreground",
91 )}
92 to="/"
93 >
94 <span className="inline-flex min-w-0 items-baseline gap-1">
95 <T3Wordmark aria-label="T3" className="h-2.5 w-auto shrink-0" />
96 <span
97 className={cn(
98 "truncate text-sm font-medium tracking-tight",
99 onBackdrop ? "text-white/70" : "text-muted-foreground",
100 )}
101 >
102 Code
103 </span>
104 </span>
105 </Link>
106 );
108​
109function SidebarUtilityItem({
110 icon,
111 label,
112 onClick,
113}: {
114 icon: ReactNode;
115 label: string;
116 onClick: () => void;
117}) {
118 return (
119 <SidebarMenuItem className="shrink-0">
120 <Tooltip>
121 <TooltipTrigger
122 render={
123 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
124 {icon}
125 </SidebarMenuButton>
126 }
127 />
128 <TooltipPopup side="top">{label}</TooltipPopup>
129 </Tooltip>
130 </SidebarMenuItem>
131 );
133​
134export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
135 const navigate = useNavigate();
136 const canGoBack = useCanGoBack();
137 const { isMobile, setOpenMobile } = useSidebar();
138 const currentFooterPage = useLocation({
139 select: (location) =>
140 /^\/settings(?:\/|$)/.test(location.pathname)
141 ? "settings"
142 : /^\/projects\/[^/]+\/?$/.test(location.pathname)
143 ? "project-settings"
144 : location.pathname === "/usage"
145 ? "usage"
146 : location.pathname === "/pull-requests"
147 ? "pull-requests"
148 : null,
149 });
150 const { environments } = useEnvironments();
151 // The page reads every connected server, so one of them offering pull requests is enough for
152 // the link to lead somewhere.
153 const pullRequestsSupported = environments.some(
154 (environment) => environment.serverConfig?.environment.capabilities.pullRequests === true,
155 );
156 const closeMobileSidebar = useCallback(() => {
157 if (isMobile) {
158 setOpenMobile(false);
159 }
160 }, [isMobile, setOpenMobile]);
161 const handlePullRequestsClick = useCallback(() => {
162 closeMobileSidebar();
163 void navigate({
164 to: "/pull-requests",
165 search: readPullRequestListPreferences(),
166 });
167 }, [closeMobileSidebar, navigate]);
168 const handleSettingsClick = useCallback(() => {
169 closeMobileSidebar();
170 void navigate({ to: "/settings" });
171 }, [closeMobileSidebar, navigate]);
172​
173 const handleUsageClick = useCallback(() => {
174 if (isMobile) {
175 setOpenMobile(false);
176 }
177 void navigate({ to: "/usage" });
178 }, [isMobile, navigate, setOpenMobile]);
179​
180 const handleBackClick = useCallback(() => {
181 closeMobileSidebar();
182 if (canGoBack) {
183 window.history.back();
184 return;
185 }
186 void navigate({ to: "/" });
187 }, [canGoBack, closeMobileSidebar, navigate]);
188​
189 return (
190 <SidebarMenu className="flex-row items-center group-data-[collapsible=icon]:flex-col">
191 {currentFooterPage ? (
192 <SidebarMenuItem className="min-w-0 flex-1">
193 <SidebarMenuButton onClick={handleBackClick} aria-label="Back" tooltip="Back">
194 <ArrowLeftIcon />
195 <span className="group-data-[collapsible=icon]:hidden">Back</span>
196 </SidebarMenuButton>
197 </SidebarMenuItem>
198 ) : (
199 <>
200 <SidebarUtilityItem
201 icon={<SettingsIcon />}
202 label="Settings"
203 onClick={handleSettingsClick}
204 />
205 {pullRequestsSupported ? (
206 <SidebarUtilityItem
207 icon={<GitPullRequestIcon />}
208 label="Pull Requests"
209 onClick={handlePullRequestsClick}
210 />
211 ) : null}
212 <SidebarUtilityItem
213 icon={<ChartNoAxesColumnIcon />}
214 label="Usage"
215 onClick={handleUsageClick}
216 />
217 </>
218 )}
219 <SidebarUpdatePill />
220 </SidebarMenu>
221 );
222});
223​
224export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
225 return (
226 <SidebarFooter className="px-[var(--sidebar-content-inset)] py-1">
227 <div className="contents group-data-[collapsible=icon]:hidden">
228 <SidebarProviderUpdatePill />
229 <SidebarUpdateArchitectureWarning />
230 </div>
231 <SidebarUtilityMenu />
232 </SidebarFooter>
233 );
234});