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

SidebarChrome.tsx

210 lines6.7 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 { usePullRequestsSupported } 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 pullRequestsSupported = usePullRequestsSupported();
137 const closeMobileSidebar = useCallback(() => {
138 if (isMobile) {
139 setOpenMobile(false);
140 }
141 }, [isMobile, setOpenMobile]);
142 const handlePullRequestsClick = useCallback(() => {
143 closeMobileSidebar();
144 void navigate({
145 to: "/pull-requests",
146 search: readPullRequestListPreferences(),
147 });
148 }, [closeMobileSidebar, navigate]);
149 const handleSettingsClick = useCallback(() => {
150 closeMobileSidebar();
151 void navigate({ to: "/settings" });
152 }, [closeMobileSidebar, navigate]);
153​
154 const handleUsageClick = useCallback(() => {
155 if (isMobile) {
156 setOpenMobile(false);
157 }
158 void navigate({ to: "/usage" });
159 }, [isMobile, navigate, setOpenMobile]);
160​
161 const handleBackClick = useCallback(() => {
162 closeMobileSidebar();
163 void navigateToMainApp();
164 }, [closeMobileSidebar, navigateToMainApp]);
165​
166 return (
167 <SidebarMenu className="flex-row items-center">
168 {isOnUtilityPage ? (
169 <SidebarMenuItem className="min-w-0 flex-1">
170 <SidebarMenuButton onClick={handleBackClick}>
171 <ArrowLeftIcon />
172 <span>Back</span>
173 </SidebarMenuButton>
174 </SidebarMenuItem>
175 ) : (
176 <>
177 <SidebarUtilityItem
178 icon={<SettingsIcon />}
179 label="Settings"
180 onClick={handleSettingsClick}
181 />
182 {pullRequestsSupported ? (
183 <SidebarUtilityItem
184 icon={<PullRequestGlyph.pullRequest />}
185 label="Pull Requests"
186 onClick={handlePullRequestsClick}
187 />
188 ) : null}
189 <SidebarUtilityItem
190 icon={<ChartNoAxesColumnIcon />}
191 label="Usage"
192 onClick={handleUsageClick}
193 />
194 </>
195 )}
196 <SidebarUpdatePill />
197 </SidebarMenu>
198 );
199});
200​
201export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
202 return (
203 <SidebarFooter>
204 <SidebarThreadUndoNotice />
205 <SidebarProviderUpdatePill />
206 <SidebarUpdateArchitectureWarning />
207 <SidebarUtilityMenu />
208 </SidebarFooter>
209 );
210});