|
| 1 | +import { lazy, memo, Suspense, useCallback, useState } from "react"; |
| 2 | +import { useQuery } from "@tanstack/react-query"; |
| 3 | +import { SearchIcon } from "lucide-react"; |
| 4 | +import { fileTreeQueryOptions } from "~/lib/editorReactQuery"; |
| 5 | +import { |
| 6 | + selectProjectEditorTabs, |
| 7 | + selectProjectActiveEditorTabId, |
| 8 | + useSidePanelStore, |
| 9 | +} from "~/sidePanelStore"; |
| 10 | +import { FileTree } from "./editor/FileTree"; |
| 11 | + |
| 12 | +const MonacoEditor = lazy(() => import("./editor/MonacoEditor")); |
| 13 | + |
| 14 | +const EditorPanel = memo(function EditorPanel({ cwd }: { cwd: string | null }) { |
| 15 | + const editorTabs = useSidePanelStore(selectProjectEditorTabs); |
| 16 | + const activeEditorTabId = useSidePanelStore(selectProjectActiveEditorTabId); |
| 17 | + const openEditorFile = useSidePanelStore((s) => s.openEditorFile); |
| 18 | + const pinEditorTab = useSidePanelStore((s) => s.pinEditorTab); |
| 19 | + |
| 20 | + const fileTreeQuery = useQuery(fileTreeQueryOptions(cwd)); |
| 21 | + const entries = fileTreeQuery.data?.entries ?? []; |
| 22 | + |
| 23 | + const [searchQuery, setSearchQuery] = useState(""); |
| 24 | + |
| 25 | + const filteredEntries = |
| 26 | + searchQuery.length > 0 |
| 27 | + ? entries.filter((e) => e.path.toLowerCase().includes(searchQuery.toLowerCase())) |
| 28 | + : entries; |
| 29 | + |
| 30 | + const activeTab = editorTabs.find((t) => t.id === activeEditorTabId) ?? null; |
| 31 | + const activeFilePath = activeTab?.relativePath ?? null; |
| 32 | + |
| 33 | + const handleFileClick = useCallback( |
| 34 | + (relativePath: string) => { |
| 35 | + openEditorFile(relativePath); |
| 36 | + }, |
| 37 | + [openEditorFile], |
| 38 | + ); |
| 39 | + |
| 40 | + const handleFileDoubleClick = useCallback( |
| 41 | + (relativePath: string) => { |
| 42 | + openEditorFile(relativePath); |
| 43 | + const state = useSidePanelStore.getState(); |
| 44 | + const projectId = state.activeProjectId; |
| 45 | + if (!projectId) return; |
| 46 | + const editorState = state.editorStateByProjectId[projectId]; |
| 47 | + const tab = editorState?.tabs.find((t) => t.relativePath === relativePath); |
| 48 | + if (tab) pinEditorTab(tab.id); |
| 49 | + }, |
| 50 | + [openEditorFile, pinEditorTab], |
| 51 | + ); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="flex min-h-0 flex-1"> |
| 55 | + {/* Left: File tree */} |
| 56 | + <div className="flex w-52 shrink-0 flex-col border-r border-border bg-card"> |
| 57 | + <div className="p-1.5"> |
| 58 | + <div className="relative"> |
| 59 | + <SearchIcon className="absolute left-2 top-1/2 size-3 -translate-y-1/2 text-muted-foreground" /> |
| 60 | + <input |
| 61 | + type="text" |
| 62 | + value={searchQuery} |
| 63 | + onChange={(e) => setSearchQuery(e.target.value)} |
| 64 | + className="h-6 w-full rounded-md bg-accent/40 pl-7 pr-2 text-xs text-foreground outline-none placeholder:text-muted-foreground focus:ring-1 focus:ring-ring" |
| 65 | + placeholder="Search files..." |
| 66 | + /> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + <FileTree |
| 70 | + entries={filteredEntries} |
| 71 | + activeFilePath={activeFilePath} |
| 72 | + onFileClick={handleFileClick} |
| 73 | + onFileDoubleClick={handleFileDoubleClick} |
| 74 | + /> |
| 75 | + </div> |
| 76 | + |
| 77 | + {/* Right: Monaco editor (no duplicate tab bar — it's in SidePanel top row) */} |
| 78 | + <div className="flex min-w-0 flex-1 flex-col"> |
| 79 | + <Suspense |
| 80 | + fallback={ |
| 81 | + <div className="flex flex-1 items-center justify-center text-sm text-muted-foreground"> |
| 82 | + Loading editor... |
| 83 | + </div> |
| 84 | + } |
| 85 | + > |
| 86 | + <MonacoEditor cwd={cwd} relativePath={activeFilePath} /> |
| 87 | + </Suspense> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +}); |
| 92 | + |
| 93 | +export default EditorPanel; |
0 commit comments