import { useState, useEffect, useRef } from 'react'; import type { MouseEvent, MutableRefObject } from 'react'; import type { CodeEditorFile } from '../types/types'; import CodeEditor from './CodeEditor'; type EditorSidebarProps = { editingFile: CodeEditorFile | null; isMobile: boolean; editorExpanded: boolean; editorWidth: number; hasManualWidth: boolean; resizeHandleRef: MutableRefObject; onResizeStart: (event: MouseEvent) => void; onCloseEditor: () => void; onToggleEditorExpand: () => void; projectPath?: string; fillSpace?: boolean; }; // Minimum width for the left content (file tree, chat, etc.) const MIN_LEFT_CONTENT_WIDTH = 200; // Minimum width for the editor sidebar const MIN_EDITOR_WIDTH = 280; export default function EditorSidebar({ editingFile, isMobile, editorExpanded, editorWidth, hasManualWidth, resizeHandleRef, onResizeStart, onCloseEditor, onToggleEditorExpand, projectPath, fillSpace, }: EditorSidebarProps) { const [poppedOut, setPoppedOut] = useState(false); const containerRef = useRef(null); const [effectiveWidth, setEffectiveWidth] = useState(editorWidth); // Adjust editor width when container size changes to ensure buttons are always visible useEffect(() => { if (!editingFile || isMobile || poppedOut) return; const updateWidth = () => { if (!containerRef.current) return; const parentElement = containerRef.current.parentElement; if (!parentElement) return; const containerWidth = parentElement.clientWidth; // Calculate maximum allowed editor width const maxEditorWidth = containerWidth - MIN_LEFT_CONTENT_WIDTH; if (maxEditorWidth < MIN_EDITOR_WIDTH) { // Not enough space - pop out the editor so user can still see everything setPoppedOut(true); } else if (editorWidth > maxEditorWidth) { // Editor is too wide - constrain it to ensure left content has space setEffectiveWidth(maxEditorWidth); } else { setEffectiveWidth(editorWidth); } }; updateWidth(); window.addEventListener('resize', updateWidth); // Also use ResizeObserver for more accurate detection const resizeObserver = new ResizeObserver(updateWidth); const parentEl = containerRef.current?.parentElement; if (parentEl) { resizeObserver.observe(parentEl); } return () => { window.removeEventListener('resize', updateWidth); resizeObserver.disconnect(); }; }, [editingFile, isMobile, poppedOut, editorWidth]); if (!editingFile) { return null; } if (isMobile || poppedOut) { return ( { setPoppedOut(false); onCloseEditor(); }} projectPath={projectPath} isSidebar={false} /> ); } // In files tab, fill the remaining width unless user has dragged manually. const useFlexLayout = editorExpanded || (fillSpace && !hasManualWidth); return (
{!editorExpanded && (
)}
setPoppedOut(true)} />
); }