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

SidebarChrome.tsx

230 lines7.0 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 gap-1 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 <T3Wordmark aria-label="T3" className="h-2.5 w-auto shrink-0" />
95 <span
96 className={cn(
97 "truncate text-sm font-medium tracking-tight",
98 onBackdrop ? "text-white/70" : "text-muted-foreground",
99 )}
100 >
101 Code
102 </span>
103 </Link>
104 );
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 );
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={<GitPullRequestIcon />}
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 className="p-[var(--sidebar-content-inset)]">
225 <SidebarProviderUpdatePill />
226 <SidebarUpdateArchitectureWarning />
227 <SidebarUtilityMenu />
228 </SidebarFooter>
229 );
230});