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

SidebarChrome.tsx

240 lines7.4 KB

1import { ArrowLeftIcon, ChartNoAxesColumnIcon, SettingsIcon } from "lucide-react";
2import type { ReactNode } from "react";
3import { memo } 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";
32import { observeResize } from "~/lib/observeResize";
33​
34export const SidebarChromeHeader = memo(function SidebarChromeHeader({
35 isElectron,
36}: {
37 isElectron: boolean;
38}) {
39 const stageLabel = useEnvironmentStageLabel();
40 const environmentIdentificationMode = useEnvironmentIdentificationMode();
41 const backdropVariant = resolveSidebarStageBackdropVariant(
42 stageLabel,
43 environmentIdentificationMode === "artwork",
44 );
45 const pillLabel =
46 environmentIdentificationMode === "pill"
47 ? resolveEnvironmentIdentificationPillLabel(stageLabel)
48 : null;
49​
50 return (
51 <div
52 className={cn(
53 "relative flex h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center gap-2 pr-3",
54 isElectron && "drag-region",
55 )}
56 >
57 {backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}
58 <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">
59 <SidebarTrigger
60 variant={backdropVariant ? "media-navigation" : "ghost"}
61 className="relative top-auto translate-y-0"
62 aria-label="Toggle main sidebar"
63 />
64 </div>
65 <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">
66 <SidebarBrand onBackdrop={backdropVariant !== null} />
67 {pillLabel ? (
68 <div className="ml-1 flex h-7 items-center">
69 <Badge data-environment-identification="pill" size="sm" variant="secondary">
70 {pillLabel}
71 </Badge>
72 </div>
73 ) : null}
74 </div>
75 </div>
76 );
77});
78​
79export function SidebarBrandWidthProbe({
80 onWidthChange,
81}: {
82 onWidthChange: (width: number) => void;
83}) {
84 const observeWidth = (probe: HTMLDivElement) => {
85 return observeResize(probe, ([entry]) => {
86 if (entry) onWidthChange(entry.borderBoxSize[0]?.inlineSize ?? probe.offsetWidth);
87 });
88 };
89​
90 return (
91 <div
92 aria-hidden="true"
93 className="pointer-events-none invisible fixed top-0 left-0 flex w-max border-r border-transparent pr-3"
94 ref={observeWidth}
95 >
96 <div className="ml-[var(--workspace-titlebar-content-left)] flex">
97 <SidebarBrandMark onBackdrop={false} />
98 </div>
99 </div>
100 );
102​
103function SidebarBrand({ onBackdrop }: { onBackdrop: boolean }) {
104 return (
105 <Link
106 aria-label="Go to threads"
107 className={cn(
108 "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",
109 onBackdrop ? "text-white" : "text-foreground",
110 )}
111 to="/"
112 >
113 <SidebarBrandMark onBackdrop={onBackdrop} />
114 </Link>
115 );
117​
118function SidebarBrandMark({ onBackdrop }: { onBackdrop: boolean }) {
119 return (
120 <span className="inline-flex min-w-0 items-baseline gap-1 text-sm font-medium tracking-tight">
121 <T3Wordmark aria-label="T3" className="h-[1cap] w-auto shrink-0" />
122 <span
123 className={cn(
124 "truncate [text-box:trim-both_cap_alphabetic] supports-[text-box:trim-both_cap_alphabetic]:py-1",
125 onBackdrop ? "text-white/70" : "text-muted-foreground",
126 )}
127 >
128 Code
129 </span>
130 </span>
131 );
133​
134function SidebarUtilityItem({
135 icon,
136 label,
137 onClick,
138}: {
139 icon: ReactNode;
140 label: string;
141 onClick: () => void;
142}) {
143 return (
144 <SidebarMenuItem className="shrink-0">
145 <Tooltip>
146 <TooltipTrigger
147 render={
148 <SidebarMenuButton aria-label={label} onClick={onClick} size="icon">
149 {icon}
150 </SidebarMenuButton>
151 }
152 />
153 <TooltipPopup side="top">{label}</TooltipPopup>
154 </Tooltip>
155 </SidebarMenuItem>
156 );
158​
159export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
160 const navigate = useNavigate();
161 const navigateToMainApp = useNavigateToMainApp();
162 const { isMobile, setOpenMobile } = useSidebar();
163 const isOnUtilityPage = useLocation({
164 select: (location) => isSidebarUtilityPage(location.pathname),
165 });
166 const pullRequestsSupported = usePullRequestsSupported();
167 const closeMobileSidebar = () => {
168 if (isMobile) {
169 setOpenMobile(false);
170 }
171 };
172 const handlePullRequestsClick = () => {
173 closeMobileSidebar();
174 void navigate({
175 to: "/pull-requests",
176 search: readPullRequestListPreferences(),
177 });
178 };
179 const handleSettingsClick = () => {
180 closeMobileSidebar();
181 void navigate({ to: "/settings" });
182 };
183​
184 const handleUsageClick = () => {
185 if (isMobile) {
186 setOpenMobile(false);
187 }
188 void navigate({ to: "/usage" });
189 };
190​
191 const handleBackClick = () => {
192 closeMobileSidebar();
193 void navigateToMainApp();
194 };
195​
196 return (
197 <SidebarMenu className="flex-row items-center">
198 {isOnUtilityPage ? (
199 <SidebarMenuItem className="min-w-0 flex-1">
200 <SidebarMenuButton onClick={handleBackClick}>
201 <ArrowLeftIcon />
202 <span>Back</span>
203 </SidebarMenuButton>
204 </SidebarMenuItem>
205 ) : (
206 <>
207 <SidebarUtilityItem
208 icon={<SettingsIcon />}
209 label="Settings"
210 onClick={handleSettingsClick}
211 />
212 {pullRequestsSupported ? (
213 <SidebarUtilityItem
214 icon={<PullRequestGlyph.pullRequest />}
215 label="Pull Requests"
216 onClick={handlePullRequestsClick}
217 />
218 ) : null}
219 <SidebarUtilityItem
220 icon={<ChartNoAxesColumnIcon />}
221 label="Usage"
222 onClick={handleUsageClick}
223 />
224 </>
225 )}
226 <SidebarUpdatePill />
227 </SidebarMenu>
228 );
229});
230​
231export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
232 return (
233 <SidebarFooter>
234 <SidebarThreadUndoNotice />
235 <SidebarProviderUpdatePill />
236 <SidebarUpdateArchitectureWarning />
237 <SidebarUtilityMenu />
238 </SidebarFooter>
239 );
240});