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

SidebarChrome.tsx

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