import { Outlet, useLocation, useNavigate } from 'react-router-dom'; import { useEffect, useMemo, useState } from 'react'; import type { QuickActionType } from '../data/mockData'; import { QuickActionModal } from '../components/modals/QuickActionModal'; import { LoginModal } from '../components/modals/LoginModal'; import { Sidebar, type SectionKey } from '../components/layout/Sidebar'; import { TopBar } from '../components/layout/TopBar'; export interface MainLayoutContext { search: string; setSearch: (value: string) => void; } const sectionToRoute: Record = { home: '/home', exam: '/exam', booking: '/booking', support: '/support', }; const routeToSection = Object.entries(sectionToRoute).reduce>( (acc, [section, route]) => { acc[route] = section as SectionKey; return acc; }, {}, ); export const MainLayout = () => { const [search, setSearch] = useState(''); const [quickAction, setQuickAction] = useState('none'); const [noteText, setNoteText] = useState(''); const [loginModalOpen, setLoginModalOpen] = useState(false); const [operatorName, setOperatorName] = useState(''); const navigate = useNavigate(); const location = useLocation(); const activeSection: SectionKey = useMemo(() => { const matched = Object.entries(routeToSection).find(([path]) => location.pathname.startsWith(path)); return (matched?.[1] || 'home') as SectionKey; }, [location.pathname]); const handleNavigate = (section: SectionKey) => { navigate(sectionToRoute[section]); }; const handleLoginSuccess = (phone: string) => { // 实际项目中应该从后端获取用户信息 // 这里暂时使用手机号后4位作为操作员名称 const displayName = phone.slice(-4); setOperatorName(displayName); // 可以存储到 localStorage 或状态管理中 localStorage.setItem('operatorPhone', phone); localStorage.setItem('operatorName', displayName); }; // 初始化时检查是否有已登录的操作员 useEffect(() => { const savedName = localStorage.getItem('operatorName'); if (savedName) { setOperatorName(savedName); } }, []); return (
setLoginModalOpen(true)} />
{loginModalOpen && ( setLoginModalOpen(false)} onLoginSuccess={handleLoginSuccess} /> )} {quickAction !== 'none' && ( setQuickAction('none')} /> )}
); };