import { useStore } from '@nanostores/react'; import { memo, useEffect, useMemo, useRef, useState } from 'react'; import { Panel, PanelGroup, PanelResizeHandle, type ImperativePanelHandle } from 'react-resizable-panels'; import { CodeMirrorEditor, type EditorDocument, type EditorSettings, type OnChangeCallback as OnEditorChange, type OnSaveCallback as OnEditorSave, type OnScrollCallback as OnEditorScroll, } from '~/components/editor/codemirror/CodeMirrorEditor'; import { IconButton } from '~/components/ui/IconButton'; import { PanelHeader } from '~/components/ui/PanelHeader'; import { PanelHeaderButton } from '~/components/ui/PanelHeaderButton'; import { shortcutEventEmitter } from '~/lib/hooks'; import type { FileMap } from '~/lib/stores/files'; import { themeStore } from '~/lib/stores/theme'; import { workbenchStore } from '~/lib/stores/workbench'; import { classNames } from '~/utils/classNames'; import { WORK_DIR } from '~/utils/constants'; import { renderLogger } from '~/utils/logger'; import { isMobile } from '~/utils/mobile'; import { FileBreadcrumb } from './FileBreadcrumb'; import { FileTree } from './FileTree'; import { Terminal, type TerminalRef } from './terminal/Terminal'; interface EditorPanelProps { files?: FileMap; unsavedFiles?: Set; editorDocument?: EditorDocument; selectedFile?: string | undefined; isStreaming?: boolean; onEditorChange?: OnEditorChange; onEditorScroll?: OnEditorScroll; onFileSelect?: (value?: string) => void; onFileSave?: OnEditorSave; onFileReset?: () => void; } const MAX_TERMINALS = 3; const DEFAULT_TERMINAL_SIZE = 25; const DEFAULT_EDITOR_SIZE = 100 - DEFAULT_TERMINAL_SIZE; const editorSettings: EditorSettings = { tabSize: 2 }; export const EditorPanel = memo( ({ files, unsavedFiles, editorDocument, selectedFile, isStreaming, onFileSelect, onEditorChange, onEditorScroll, onFileSave, onFileReset, }: EditorPanelProps) => { renderLogger.trace('EditorPanel'); const theme = useStore(themeStore); const showTerminal = useStore(workbenchStore.showTerminal); const terminalRefs = useRef>([]); const terminalPanelRef = useRef(null); const terminalToggledByShortcut = useRef(false); const [activeTerminal, setActiveTerminal] = useState(0); const [terminalCount, setTerminalCount] = useState(1); const activeFileSegments = useMemo(() => { if (!editorDocument) { return undefined; } return editorDocument.filePath.split('/'); }, [editorDocument]); const activeFileUnsaved = useMemo(() => { return editorDocument !== undefined && unsavedFiles?.has(editorDocument.filePath); }, [editorDocument, unsavedFiles]); useEffect(() => { const unsubscribeFromEventEmitter = shortcutEventEmitter.on('toggleTerminal', () => { terminalToggledByShortcut.current = true; }); const unsubscribeFromThemeStore = themeStore.subscribe(() => { for (const ref of Object.values(terminalRefs.current)) { ref?.reloadStyles(); } }); return () => { unsubscribeFromEventEmitter(); unsubscribeFromThemeStore(); }; }, []); useEffect(() => { const { current: terminal } = terminalPanelRef; if (!terminal) { return; } const isCollapsed = terminal.isCollapsed(); if (!showTerminal && !isCollapsed) { terminal.collapse(); } else if (showTerminal && isCollapsed) { terminal.resize(DEFAULT_TERMINAL_SIZE); } terminalToggledByShortcut.current = false; }, [showTerminal]); const addTerminal = () => { if (terminalCount < MAX_TERMINALS) { setTerminalCount(terminalCount + 1); setActiveTerminal(terminalCount); } }; return (
Files
{activeFileSegments?.length && (
{activeFileUnsaved && (
Save
Reset
)}
)}
{ if (!terminalToggledByShortcut.current) { workbenchStore.toggleTerminal(true); } }} onCollapse={() => { if (!terminalToggledByShortcut.current) { workbenchStore.toggleTerminal(false); } }} >
{Array.from({ length: terminalCount }, (_, index) => { const isActive = activeTerminal === index; return ( ); })} {terminalCount < MAX_TERMINALS && } workbenchStore.toggleTerminal(false)} />
{Array.from({ length: terminalCount }, (_, index) => { const isActive = activeTerminal === index; return ( { terminalRefs.current.push(ref); }} onTerminalReady={(terminal) => workbenchStore.attachTerminal(terminal)} onTerminalResize={(cols, rows) => workbenchStore.onTerminalResize(cols, rows)} theme={theme} /> ); })}
); }, );