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

SidebarChrome.tsx

215 lines6.9 KB

1import { ArrowLeftIcon, ChartNoAxesColumnIcon, SettingsIcon } from "lucide-react";
2import type { ReactNode } from "react";
3import { memo, useCallback } from "react";
4import { Link, 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 SidebarStageBackdrop,
14 useEnvironmentStageLabel,
15} from "../SidebarStageBackdrop";
16import { Badge } from "../ui/badge";
17import {
18 SidebarFooter,
19 SidebarMenu,
20 SidebarMenuButton,
21 SidebarMenuItem,
22 SidebarTrigger,
23 useSidebar,
24} from "../ui/sidebar";
25import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
26import { readPullRequestListPreferences } from "../pullRequest/pullRequestListPreferences";
27import { isSidebarUtilityPage, useNavigateToMainApp } from "./mainAppLocation";
28import { SidebarThreadUndoNotice } from "./SidebarThreadUndoNotice";
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 // The titlebar row, not a padded SidebarHeader: it aligns to the window controls.
51 <div
52 className={cn(
53 "@container/sidebar-header relative flex h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center gap-2 px-3 md:px-0",
54 isElectron && "drag-region",
55 )}
56 >
57 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
58 <SidebarTrigger
59 // Over the stage artwork: the media viewer's control-on-imagery treatment.
60 variant={backdropVariant ? "media-navigation" : "ghost"}
61 className="relative top-auto z-10 translate-y-0 md:hidden"
62 />
63 <SidebarBrand onBackdrop={backdropVariant !== null} />
64 {pillLabel ? (
65 <Badge
66 className="relative z-10 ml-1 hidden @[15rem]/sidebar-header:inline-flex"
67 data-environment-identification="pill"
68 size="sm"
69 variant="secondary"
70 >
71 {pillLabel}
72 </Badge>
73 ) : null}
74 </div>
75 );
76});
77​
78function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
79 return (
80 <Link
81 aria-label="Go to threads"
82 className={cn(
83 "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",
84 onBackdrop ? "text-white" : "text-foreground",
85 )}
86 to="/"
87 >
88 {/* Center the visible capitals, without the font's ascender/descender space. */}
89 <span className="inline-flex min-w-0 items-baseline gap-1 text-sm font-medium tracking-tight">
90 <T3Wordmark aria-label="T3" className="h-[1cap] w-auto shrink-0" />
91 <span
92 className={cn(
93 "truncate [text-box:trim-both_cap_alphabetic]",
94 onBackdrop ? "text-white/70" : "text-muted-foreground",
95 )}
96 >
97 Code
98 </span>
99 </span>
100 </Link>
101 );
103​
104function SidebarUtilityItem({
105 icon,
106 label,
107 onClick,
108}: {
109 icon: ReactNode;
110 label: string;
111 onClick: () => void;
112}) {
113 return (
114 <SidebarMenuItem className="shrink-0">
115 <Tooltip>
116 <TooltipTrigger
117 render={
118 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
119 {icon}
120 </SidebarMenuButton>
121 }
122 />
123 <TooltipPopup side="top">{label}</TooltipPopup>
124 </Tooltip>
125 </SidebarMenuItem>
126 );
128​
129export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
130 const navigate = useNavigate();
131 const navigateToMainApp = useNavigateToMainApp();
132 const { isMobile, setOpenMobile } = useSidebar();
133 const isOnUtilityPage = useLocation({
134 select: (location) => isSidebarUtilityPage(location.pathname),
135 });
136 const { environments } = useEnvironments();
137 // The page reads every connected server, so one of them offering pull requests is enough for
138 // the link to lead somewhere.
139 const pullRequestsSupported = environments.some(
140 (environment) => environment.serverConfig?.environment.capabilities.pullRequests === true,
141 );
142 const closeMobileSidebar = useCallback(() => {
143 if (isMobile) {
144 setOpenMobile(false);
145 }
146 }, [isMobile, setOpenMobile]);
147 const handlePullRequestsClick = useCallback(() => {
148 closeMobileSidebar();
149 void navigate({
150 to: "/pull-requests",
151 search: readPullRequestListPreferences(),
152 });
153 }, [closeMobileSidebar, navigate]);
154 const handleSettingsClick = useCallback(() => {
155 closeMobileSidebar();
156 void navigate({ to: "/settings" });
157 }, [closeMobileSidebar, navigate]);
158​
159 const handleUsageClick = useCallback(() => {
160 if (isMobile) {
161 setOpenMobile(false);
162 }
163 void navigate({ to: "/usage" });
164 }, [isMobile, navigate, setOpenMobile]);
165​
166 const handleBackClick = useCallback(() => {
167 closeMobileSidebar();
168 void navigateToMainApp();
169 }, [closeMobileSidebar, navigateToMainApp]);
170​
171 return (
172 <SidebarMenu className="flex-row items-center">
173 {isOnUtilityPage ? (
174 <SidebarMenuItem className="min-w-0 flex-1">
175 <SidebarMenuButton onClick={handleBackClick}>
176 <ArrowLeftIcon />
177 <span>Back</span>
178 </SidebarMenuButton>
179 </SidebarMenuItem>
180 ) : (
181 <>
182 <SidebarUtilityItem
183 icon={<SettingsIcon />}
184 label="Settings"
185 onClick={handleSettingsClick}
186 />
187 {pullRequestsSupported ? (
188 <SidebarUtilityItem
189 icon={<PullRequestGlyph.pullRequest />}
190 label="Pull Requests"
191 onClick={handlePullRequestsClick}
192 />
193 ) : null}
194 <SidebarUtilityItem
195 icon={<ChartNoAxesColumnIcon />}
196 label="Usage"
197 onClick={handleUsageClick}
198 />
199 </>
200 )}
201 <SidebarUpdatePill />
202 </SidebarMenu>
203 );
204});
205​
206export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
207 return (
208 <SidebarFooter>
209 <SidebarThreadUndoNotice />
210 <SidebarProviderUpdatePill />
211 <SidebarUpdateArchitectureWarning />
212 <SidebarUtilityMenu />
213 </SidebarFooter>
214 );
215});