|
| 1 | +import { FolderIcon } from "lucide-react"; |
| 2 | +import { memo, type ReactNode, useEffect, useRef, useState } from "react"; |
| 3 | +import { cn } from "~/lib/utils"; |
| 4 | + |
| 5 | +export type WorkspaceLayoutMode = "stacked" | "split"; |
| 6 | + |
| 7 | +const WORKSPACE_SPLIT_MIN_WIDTH_PX = 600; |
| 8 | + |
| 9 | +export function resolveWorkspaceLayoutMode(width: number): WorkspaceLayoutMode { |
| 10 | + return width >= WORKSPACE_SPLIT_MIN_WIDTH_PX ? "split" : "stacked"; |
| 11 | +} |
| 12 | + |
| 13 | +function basenameOfPath(pathValue: string): string { |
| 14 | + const normalizedPath = pathValue.replace(/\/+$/, ""); |
| 15 | + const segments = normalizedPath.split("/"); |
| 16 | + return segments[segments.length - 1] || pathValue; |
| 17 | +} |
| 18 | + |
| 19 | +export const WorkspacePanel = memo(function WorkspacePanel(props: { |
| 20 | + cwd: string | null; |
| 21 | + tree: ReactNode; |
| 22 | + editor: ReactNode; |
| 23 | +}) { |
| 24 | + const containerRef = useRef<HTMLDivElement | null>(null); |
| 25 | + const [layoutMode, setLayoutMode] = useState<WorkspaceLayoutMode>("stacked"); |
| 26 | + const workspaceLabel = props.cwd ? basenameOfPath(props.cwd) : "Workspace"; |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const element = containerRef.current; |
| 30 | + if (!element) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const syncLayoutMode = () => { |
| 35 | + const nextMode = resolveWorkspaceLayoutMode(element.getBoundingClientRect().width); |
| 36 | + setLayoutMode((currentMode) => (currentMode === nextMode ? currentMode : nextMode)); |
| 37 | + }; |
| 38 | + |
| 39 | + syncLayoutMode(); |
| 40 | + |
| 41 | + if (typeof ResizeObserver === "undefined") { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const observer = new ResizeObserver(() => { |
| 46 | + syncLayoutMode(); |
| 47 | + }); |
| 48 | + observer.observe(element); |
| 49 | + |
| 50 | + return () => { |
| 51 | + observer.disconnect(); |
| 52 | + }; |
| 53 | + }, []); |
| 54 | + |
| 55 | + return ( |
| 56 | + <div ref={containerRef} className="flex h-full min-h-0 flex-col bg-background"> |
| 57 | + <div className={cn("flex min-h-0 flex-1", layoutMode === "split" ? "flex-row" : "flex-col")}> |
| 58 | + {props.cwd ? ( |
| 59 | + <section |
| 60 | + className={cn( |
| 61 | + "flex min-h-0 shrink-0 overflow-hidden bg-card/35", |
| 62 | + layoutMode === "split" |
| 63 | + ? "w-[clamp(15rem,32%,19rem)] border-r border-border/60" |
| 64 | + : "h-[clamp(14rem,34vh,20rem)] border-b border-border/60", |
| 65 | + )} |
| 66 | + > |
| 67 | + <div className="flex min-h-0 flex-1 flex-col"> |
| 68 | + <div className="border-b border-border/60 px-3 py-2"> |
| 69 | + <div className="flex items-center gap-2"> |
| 70 | + <FolderIcon className="size-3.5 shrink-0 text-muted-foreground/70" /> |
| 71 | + <div className="min-w-0"> |
| 72 | + <p className="text-[10px] font-medium uppercase tracking-[0.14em] text-muted-foreground/55"> |
| 73 | + Workspace |
| 74 | + </p> |
| 75 | + <p className="truncate font-mono text-[11px] text-foreground/85"> |
| 76 | + {workspaceLabel} |
| 77 | + </p> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + <div className="min-h-0 flex-1 overflow-y-auto py-2">{props.tree}</div> |
| 82 | + </div> |
| 83 | + </section> |
| 84 | + ) : null} |
| 85 | + <section className="min-h-0 min-w-0 flex-1 bg-background">{props.editor}</section> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}); |
0 commit comments