import { ReactNode, useState, useEffect } from 'react'; import { Form } from '@remix-run/react'; import { Sidebar } from './Sidebar'; import { Container } from './Container'; import { Flex } from './Flex'; import { Text, Button } from '../ui'; import { designTokens } from '~/lib/design-tokens'; interface DashboardLayoutProps { children: ReactNode; user: { id: number; name: string; authLevel: number; }; title?: string; // Optional page title for mobile header } export function DashboardLayout({ children, user, title }: DashboardLayoutProps) { const [sidebarCollapsed, setSidebarCollapsed] = useState(false); const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const [isMobile, setIsMobile] = useState(false); const [isClient, setIsClient] = useState(false); // Set client-side flag useEffect(() => { setIsClient(true); }, []); // Handle responsive behavior useEffect(() => { if (!isClient) return; const checkMobile = () => { const mobile = window.innerWidth < 768; // md breakpoint setIsMobile(mobile); // Auto-collapse sidebar on mobile and tablet if (mobile) { setSidebarCollapsed(true); } }; checkMobile(); window.addEventListener('resize', checkMobile); return () => window.removeEventListener('resize', checkMobile); }, [isClient]); // Load sidebar state from localStorage useEffect(() => { if (!isClient) return; const savedState = localStorage.getItem('sidebarCollapsed'); if (savedState !== null && !isMobile) { setSidebarCollapsed(JSON.parse(savedState)); } }, [isMobile, isClient]); // Save sidebar state to localStorage const handleSidebarToggle = () => { const newState = !sidebarCollapsed; setSidebarCollapsed(newState); if (!isMobile) { localStorage.setItem('sidebarCollapsed', JSON.stringify(newState)); } }; const handleMobileMenuClose = () => { setMobileMenuOpen(false); }; const getAuthLevelText = (authLevel: number) => { switch (authLevel) { case 1: return "مدير عام"; case 2: return "مدير"; case 3: return "مستخدم"; default: return "غير محدد"; } }; return (
{/* Sidebar */} {/* Main Content */}
{/* Header */}
{/* Mobile menu button and title - Always render, hide with CSS */}
{/* Page title - show on mobile */}

{title || 'لوحة التحكم'}

{/* User info and actions */}
{/* Desktop: Full user info */}
مرحباً، {user.name}
{getAuthLevelText(user.authLevel)}
{/* Tablet: Name only */}
{user.name}
{/* Mobile: Compact display */}
{user.name}
{/* Page Content */}
{children}
); }