|
| 1 | +import { useNavigate } from "@tanstack/react-router"; |
| 2 | +import { FileCodeIcon, XIcon } from "lucide-react"; |
| 3 | +import { useCallback, useEffect } from "react"; |
| 4 | + |
| 5 | +import { useCodeViewerStore } from "~/codeViewerStore"; |
| 6 | +import { useTheme } from "~/hooks/useTheme"; |
| 7 | +import { isMacPlatform } from "~/lib/utils"; |
| 8 | +import type { CodeContextSelection } from "../CodeMirrorViewer"; |
| 9 | +import { CodeViewerFileContent, CodeViewerTabStrip } from "../CodeViewerPanel"; |
| 10 | +import { Button } from "../ui/button"; |
| 11 | + |
| 12 | +export function FileViewShell(props: { initialCwd: string; initialPath: string | null }) { |
| 13 | + const { resolvedTheme } = useTheme(); |
| 14 | + const navigate = useNavigate(); |
| 15 | + |
| 16 | + const tabs = useCodeViewerStore((state) => state.tabs); |
| 17 | + const activeTabPath = useCodeViewerStore((state) => state.activeTabPath); |
| 18 | + const setActiveTab = useCodeViewerStore((state) => state.setActiveTab); |
| 19 | + const closeTab = useCodeViewerStore((state) => state.closeTab); |
| 20 | + const closeAllTabs = useCodeViewerStore((state) => state.closeAllTabs); |
| 21 | + const openFile = useCodeViewerStore((state) => state.openFile); |
| 22 | + const setPendingContext = useCodeViewerStore((state) => state.setPendingContext); |
| 23 | + |
| 24 | + // On mount, ensure the initial file is open as a tab |
| 25 | + useEffect(() => { |
| 26 | + if (props.initialPath) { |
| 27 | + openFile(props.initialCwd, props.initialPath); |
| 28 | + } |
| 29 | + // Only run on mount |
| 30 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 31 | + }, []); |
| 32 | + |
| 33 | + const activeTab = tabs.find((tab) => tab.relativePath === activeTabPath); |
| 34 | + |
| 35 | + const onSelectTab = useCallback( |
| 36 | + (relativePath: string) => { |
| 37 | + setActiveTab(relativePath); |
| 38 | + const tab = tabs.find((t) => t.relativePath === relativePath); |
| 39 | + if (tab) { |
| 40 | + void navigate({ |
| 41 | + to: "/file-view", |
| 42 | + search: { cwd: tab.cwd, path: relativePath }, |
| 43 | + replace: true, |
| 44 | + }); |
| 45 | + } |
| 46 | + }, |
| 47 | + [setActiveTab, tabs, navigate], |
| 48 | + ); |
| 49 | + |
| 50 | + const onCloseTab = useCallback( |
| 51 | + (relativePath: string) => { |
| 52 | + closeTab(relativePath); |
| 53 | + // Check if this was the last tab (after closing, store will have tabs.length - 1) |
| 54 | + if (tabs.length <= 1) { |
| 55 | + void navigate({ to: "/" }); |
| 56 | + } |
| 57 | + }, |
| 58 | + [closeTab, tabs.length, navigate], |
| 59 | + ); |
| 60 | + |
| 61 | + const onCloseAll = useCallback(() => { |
| 62 | + closeAllTabs(); |
| 63 | + void navigate({ to: "/" }); |
| 64 | + }, [closeAllTabs, navigate]); |
| 65 | + |
| 66 | + const onAddContext = useCallback( |
| 67 | + (ctx: CodeContextSelection) => { |
| 68 | + setPendingContext({ |
| 69 | + filePath: ctx.filePath, |
| 70 | + fromLine: ctx.fromLine, |
| 71 | + toLine: ctx.toLine, |
| 72 | + }); |
| 73 | + }, |
| 74 | + [setPendingContext], |
| 75 | + ); |
| 76 | + |
| 77 | + const modKey = isMacPlatform(navigator.platform) ? "\u2318" : "Ctrl+"; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className="flex h-full w-full flex-col"> |
| 81 | + {/* Tab bar */} |
| 82 | + <div className="flex items-center justify-between gap-2 border-b border-border px-4 h-12"> |
| 83 | + <CodeViewerTabStrip |
| 84 | + tabs={tabs} |
| 85 | + activeTabPath={activeTabPath} |
| 86 | + onSelectTab={onSelectTab} |
| 87 | + onCloseTab={onCloseTab} |
| 88 | + onCloseAll={onCloseAll} |
| 89 | + /> |
| 90 | + <div className="flex shrink-0 items-center gap-2"> |
| 91 | + <span className="hidden text-[10px] text-muted-foreground/50 sm:inline"> |
| 92 | + Select code + {modKey}L to add context |
| 93 | + </span> |
| 94 | + <Button size="icon-xs" variant="ghost" onClick={onCloseAll} aria-label="Close all tabs"> |
| 95 | + <XIcon className="size-4" /> |
| 96 | + </Button> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + |
| 100 | + {/* Content */} |
| 101 | + <div className="flex min-h-0 flex-1 justify-center overflow-y-auto"> |
| 102 | + <div className="h-full w-full max-w-5xl"> |
| 103 | + {!activeTab ? ( |
| 104 | + <div className="flex h-full flex-col items-center justify-center gap-2 px-5 text-center text-muted-foreground/60"> |
| 105 | + <FileCodeIcon className="size-8 opacity-40" /> |
| 106 | + <p className="text-xs">Click a file in the sidebar to view it here.</p> |
| 107 | + </div> |
| 108 | + ) : ( |
| 109 | + <CodeViewerFileContent |
| 110 | + key={activeTab.relativePath} |
| 111 | + cwd={activeTab.cwd} |
| 112 | + relativePath={activeTab.relativePath} |
| 113 | + resolvedTheme={resolvedTheme} |
| 114 | + onAddContext={onAddContext} |
| 115 | + /> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments