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

SidebarChrome.tsx

302 lines9.4 KB

1import { ArrowLeftIcon, ArrowRightIcon, ChartNoAxesColumnIcon, SettingsIcon } from "lucide-react";
2import type { ReactNode } from "react";
3import { memo } from "react";
4import { Link, useLocation, useNavigate } from "@tanstack/react-router";
5​
6import { useAtomValue } from "@effect/atom-react";
7import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
8import { shortcutLabelForCommand } from "../../keybindings";
9import {
10 navigateHistory,
11 useCanNavigateHistory,
12 type HistoryDirection,
13} from "../../navigationHistory";
14import { primaryServerKeybindingsAtom } from "../../state/server";
15import { cn } from "../../lib/utils";
16import { usePullRequestsSupported } from "../../state/environments";
17import { T3Wordmark } from "../T3Wordmark";
18import {
19 resolveEnvironmentIdentificationPillLabel,
20 resolveSidebarStageBackdropVariant,
21 SidebarStageBackdrop,
22 useEnvironmentStageLabel,
23} from "../SidebarStageBackdrop";
24import { Badge } from "../ui/badge";
25import { Button } from "../ui/button";
26import {
27 SidebarFooter,
28 SidebarMenu,
29 SidebarMenuButton,
30 SidebarMenuItem,
31 SidebarTrigger,
32 useSidebar,
33} from "../ui/sidebar";
34import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
35import { readPullRequestListPreferences } from "../pullRequest/pullRequestListPreferences";
36import { isSidebarUtilityPage, useNavigateToMainApp } from "./mainAppLocation";
37import { SidebarThreadUndoNotice } from "./SidebarThreadUndoNotice";
38import { SidebarProviderUpdatePill } from "./SidebarProviderUpdatePill";
39import { SidebarUpdateArchitectureWarning, SidebarUpdatePill } from "./SidebarUpdatePill";
40import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
41import { observeResize } from "~/lib/observeResize";
42​
43export const SidebarChromeHeader = memo(function SidebarChromeHeader({
44 isElectron,
45}: {
46 isElectron: boolean;
47}) {
48 const stageLabel = useEnvironmentStageLabel();
49 const environmentIdentificationMode = useEnvironmentIdentificationMode();
50 const backdropVariant = resolveSidebarStageBackdropVariant(
51 stageLabel,
52 environmentIdentificationMode,
53 );
54 const pillLabel =
55 environmentIdentificationMode === "pill"
56 ? resolveEnvironmentIdentificationPillLabel(stageLabel)
57 : null;
58​
59 return (
60 <div
61 className={cn(
62 "relative flex h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center gap-2 pr-3",
63 isElectron && "drag-region",
64 )}
65 >
66 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
67 <div className="relative top-[calc(var(--workspace-controls-top)-env(safe-area-inset-top))] z-10 ml-[calc(var(--workspace-controls-left)+1px-env(safe-area-inset-left))] flex shrink-0 items-center md:hidden">
68 <SidebarTrigger
69 variant={backdropVariant ? "media-navigation" : "ghost"}
70 className="relative top-auto translate-y-0"
71 aria-label="Toggle main sidebar"
72 />
73 </div>
74 <div className="relative z-10 flex h-8 min-w-0 flex-1 flex-wrap content-start items-center gap-x-2 overflow-hidden py-0.5">
75 <div className="flex shrink-0 items-center gap-1">
76 <SidebarBrand onBackdrop={backdropVariant !== null} />
77 {isElectron ? (
78 <div className="hidden items-center md:flex">
79 <HistoryNavigationButton direction="back" onBackdrop={backdropVariant !== null} />
80 <HistoryNavigationButton direction="forward" onBackdrop={backdropVariant !== null} />
81 </div>
82 ) : null}
83 </div>
84 {pillLabel ? (
85 <div className="ml-1 flex h-7 items-center">
86 <Badge data-environment-identification="pill" size="sm" variant="secondary">
87 {pillLabel}
88 </Badge>
89 </div>
90 ) : null}
91 </div>
92 </div>
93 );
94});
95​
96export function SidebarBrandWidthProbe({
97 isElectron,
98 onWidthChange,
99}: {
100 isElectron: boolean;
101 onWidthChange: (width: number) => void;
102}) {
103 const observeWidth = (probe: HTMLDivElement) => {
104 return observeResize(probe, ([entry]) => {
105 if (entry) onWidthChange(entry.borderBoxSize[0]?.inlineSize ?? probe.offsetWidth);
106 });
107 };
108​
109 return (
110 <div
111 aria-hidden="true"
112 className="pointer-events-none invisible fixed top-0 left-0 flex w-max border-r border-transparent pr-3"
113 ref={observeWidth}
114 >
115 <div className="ml-[var(--workspace-titlebar-content-left)] flex gap-1">
116 <SidebarBrandMark onBackdrop={false} />
117 {isElectron ? <div className="w-[calc(2*var(--workspace-titlebar-control-size))]" /> : null}
118 </div>
119 </div>
120 );
122​
123function HistoryNavigationButton({
124 direction,
125 onBackdrop,
126}: {
127 direction: HistoryDirection;
128 onBackdrop: boolean;
129}) {
130 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
131 const enabled = useCanNavigateHistory(direction);
132 const label = direction === "back" ? "Go back" : "Go forward";
133 const shortcutLabel = shortcutLabelForCommand(
134 keybindings,
135 direction === "back" ? "navigation.back" : "navigation.forward",
136 );
137 const Icon = direction === "back" ? ArrowLeftIcon : ArrowRightIcon;
138 return (
139 <Tooltip>
140 <TooltipTrigger
141 render={
142 <Button
143 aria-label={label}
144 className={cn(
145 "[-webkit-app-region:no-drag]",
146 onBackdrop && "relative top-auto translate-y-0",
147 )}
148 disabled={!enabled}
149 onClick={() => navigateHistory(direction)}
150 size="icon-sm"
151 variant={onBackdrop ? "media-navigation" : "ghost"}
152 />
153 }
154 >
155 <Icon className="size-3.5" />
156 </TooltipTrigger>
157 <TooltipPopup side="bottom">
158 {label}
159 {shortcutLabel ? ` (${shortcutLabel})` : ""}
160 </TooltipPopup>
161 </Tooltip>
162 );
164​
165function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
166 return (
167 <Link
168 aria-label="T3 Code home"
169 className={cn(
170 "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 focus-visible:ring-inset md:flex",
171 onBackdrop ? "text-white" : "text-foreground",
172 )}
173 to="/"
174 >
175 <SidebarBrandMark onBackdrop={onBackdrop} />
176 </Link>
177 );
179​
180function SidebarBrandMark({ onBackdrop }: { onBackdrop: boolean }) {
181 return (
182 <span className="inline-flex min-w-0 items-baseline gap-1 text-sm font-medium tracking-tight">
183 <T3Wordmark aria-label="T3" className="h-[1cap] w-auto shrink-0" />
184 <span
185 className={cn(
186 "truncate [text-box:trim-both_cap_alphabetic] supports-[text-box:trim-both_cap_alphabetic]:py-1",
187 onBackdrop ? "text-white/70" : "text-muted-foreground",
188 )}
189 >
190 Code
191 </span>
192 </span>
193 );
195​
196function SidebarUtilityItem({
197 icon,
198 label,
199 onClick,
200}: {
201 icon: ReactNode;
202 label: string;
203 onClick: () => void;
204}) {
205 return (
206 <SidebarMenuItem className="shrink-0">
207 <Tooltip>
208 <TooltipTrigger
209 render={
210 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
211 {icon}
212 </SidebarMenuButton>
213 }
214 />
215 <TooltipPopup side="top">{label}</TooltipPopup>
216 </Tooltip>
217 </SidebarMenuItem>
218 );
220​
221const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
222 const navigate = useNavigate();
223 const navigateToMainApp = useNavigateToMainApp();
224 const { isMobile, setOpenMobile } = useSidebar();
225 const isOnUtilityPage = useLocation({
226 select: (location) => isSidebarUtilityPage(location.pathname),
227 });
228 const pullRequestsSupported = usePullRequestsSupported();
229 const closeMobileSidebar = () => {
230 if (isMobile) {
231 setOpenMobile(false);
232 }
233 };
234 const handlePullRequestsClick = () => {
235 closeMobileSidebar();
236 void navigate({
237 to: "/pull-requests",
238 search: readPullRequestListPreferences(),
239 });
240 };
241 const handleSettingsClick = () => {
242 closeMobileSidebar();
243 void navigate({ to: "/settings" });
244 };
245​
246 const handleUsageClick = () => {
247 if (isMobile) {
248 setOpenMobile(false);
249 }
250 void navigate({ to: "/usage" });
251 };
252​
253 const handleBackClick = () => {
254 closeMobileSidebar();
255 void navigateToMainApp();
256 };
257​
258 return (
259 <SidebarMenu className="flex-row items-center">
260 {isOnUtilityPage ? (
261 <SidebarMenuItem className="min-w-0 flex-1">
262 <SidebarMenuButton onClick={handleBackClick}>
263 <ArrowLeftIcon />
264 <span>Back</span>
265 </SidebarMenuButton>
266 </SidebarMenuItem>
267 ) : (
268 <>
269 <SidebarUtilityItem
270 icon={<SettingsIcon />}
271 label="Settings"
272 onClick={handleSettingsClick}
273 />
274 {pullRequestsSupported ? (
275 <SidebarUtilityItem
276 icon={<PullRequestGlyph.pullRequest />}
277 label="Pull requests"
278 onClick={handlePullRequestsClick}
279 />
280 ) : null}
281 <SidebarUtilityItem
282 icon={<ChartNoAxesColumnIcon />}
283 label="Usage"
284 onClick={handleUsageClick}
285 />
286 </>
287 )}
288 <SidebarThreadUndoNotice />
289 <SidebarUpdatePill />
290 </SidebarMenu>
291 );
292});
293​
294export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
295 return (
296 <SidebarFooter>
297 <SidebarProviderUpdatePill />
298 <SidebarUpdateArchitectureWarning />
299 <SidebarUtilityMenu />
300 </SidebarFooter>
301 );
302});