import { useEffect, useState, memo, useCallback, useRef } from 'react' import { FileBrowser } from './FileBrowser' import { Button } from '@/components/ui/button' import { PathDisplay } from '@/components/ui/path-display' import { X } from 'lucide-react' import { GPU_ACCELERATED_STYLE, MODAL_TRANSITION_MS } from '@/lib/utils' import { useSwipeBack } from '@/hooks/useMobile' interface FileBrowserSheetProps { isOpen: boolean onClose: () => void basePath?: string repoName?: string initialSelectedFile?: string } export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose, basePath = '', repoName, initialSelectedFile }: FileBrowserSheetProps) { const normalizedBasePath = basePath || '.' const [isEditing, setIsEditing] = useState(false) const [displayPath, setDisplayPath] = useState('/') const [shouldRender, setShouldRender] = useState(false) const containerRef = useRef(null) const { bind, swipeStyles } = useSwipeBack(onClose, { enabled: isOpen && !isEditing, }) useEffect(() => { return bind(containerRef.current) }, [bind]) useEffect(() => { if (isOpen) { setShouldRender(true) } else { const timer = setTimeout(() => setShouldRender(false), MODAL_TRANSITION_MS) return () => clearTimeout(timer) } }, [isOpen]) const handleDirectoryLoad = useCallback((info: { workspaceRoot?: string; currentPath: string }) => { if (!info.currentPath || info.currentPath === '.' || info.currentPath === '') { setDisplayPath('/') return } const pathParts = info.currentPath.split('/').filter(Boolean) if (repoName) { const repoIndex = pathParts.findIndex(p => p === repoName || p.startsWith(repoName + '-')) if (repoIndex >= 0) { const subPath = pathParts.slice(repoIndex + 1) setDisplayPath(subPath.length > 0 ? '/' + subPath.join('/') : '/') } else { setDisplayPath('/' + pathParts.join('/')) } } else { setDisplayPath('/' + pathParts.join('/')) } }, [repoName]) useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose() } } const handleEditModeChange = (event: CustomEvent<{ isEditing: boolean }>) => { setIsEditing(event.detail.isEditing) } if (isOpen) { document.addEventListener('keydown', handleEscape) document.addEventListener('editModeChange', handleEditModeChange as EventListener) document.body.style.overflow = 'hidden' } return () => { document.removeEventListener('keydown', handleEscape) document.removeEventListener('editModeChange', handleEditModeChange as EventListener) document.body.style.overflow = 'unset' } }, [isOpen, onClose]) if (!isOpen && !shouldRender) return null return (
{(displayPath === '/' || !repoName) && (

{repoName || 'Workspace Files'}

)}
{!isEditing && ( )}
) })