@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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