import React, { useState, useRef, useEffect } from 'react'; import { useLocation } from 'react-router-dom'; import { Menu, X } from 'lucide-react'; import { Sidebar } from './Sidebar'; import { UploadStatus } from './UploadStatus'; import type { Collection } from '../types'; import clsx from 'clsx'; interface LayoutProps { children: React.ReactNode; collections: Collection[]; selectedCollectionId: string | null | undefined; onSelectCollection: (id: string | null | undefined) => void; onCreateCollection: (name: string) => void; onEditCollection: (id: string, name: string) => void; onDeleteCollection: (id: string) => void; onDrop?: (e: React.DragEvent, collectionId: string | null) => void; } export const Layout: React.FC = ({ children, collections, selectedCollectionId, onSelectCollection, onCreateCollection, onEditCollection, onDeleteCollection, onDrop }) => { const location = useLocation(); const [sidebarWidth, setSidebarWidth] = useState(260); const [isResizing, setIsResizing] = useState(false); const [isMobile, setIsMobile] = useState(false); const [isSidebarOpen, setIsSidebarOpen] = useState(true); const sidebarRef = useRef(null); const startXRef = useRef(0); const startWidthRef = useRef(0); // Handle mouse down on resize handle const handleMouseDown = (e: React.MouseEvent) => { e.preventDefault(); setIsResizing(true); startXRef.current = e.clientX; startWidthRef.current = sidebarWidth; const handleMouseMove = (e: MouseEvent) => { const diff = e.clientX - startXRef.current; const newWidth = Math.max(200, Math.min(600, startWidthRef.current + diff)); setSidebarWidth(newWidth); }; const handleMouseUp = () => { setIsResizing(false); document.removeEventListener('mousemove', handleMouseMove); document.removeEventListener('mouseup', handleMouseUp); }; document.addEventListener('mousemove', handleMouseMove); document.addEventListener('mouseup', handleMouseUp); }; // Cleanup event listeners on unmount useEffect(() => { return () => { document.removeEventListener('mousemove', () => {}); document.removeEventListener('mouseup', () => {}); }; }, []); useEffect(() => { const mq = window.matchMedia('(max-width: 1024px)'); const sync = () => { setIsMobile(mq.matches); setIsSidebarOpen(!mq.matches); }; sync(); mq.addEventListener('change', sync); return () => mq.removeEventListener('change', sync); }, []); useEffect(() => { if (!isMobile) return; setIsSidebarOpen(false); }, [isMobile, location.pathname, location.search]); return (
{isMobile ? (
{children}
setIsSidebarOpen(false)} />
) : (
{children}
)}
); };