import { useRef, useEffect } from 'react' import { useNavigate, useLocation } from 'react-router-dom' import { X, ChevronLeft, ChevronRight } from 'lucide-react' import { useTabsStore } from '@/store/tabs' import { useAuthStore } from '@/store/auth' import { cn } from '@/lib/utils' import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuTrigger, } from '@/components/ui/context-menu' import type { SystemMenu } from '@/api/system' /** Resolve page title from menu tree by path */ function resolveTitle(menus: SystemMenu[], path: string): string { for (const m of menus) { if (m.path === path) return m.title || m.name if (m.children?.length) { const found = resolveTitle(m.children, path) if (found) return found } } return '' } export default function TabBar() { const navigate = useNavigate() const location = useLocation() const menus = useAuthStore(s => s.menus) const { tabs, activeTab, addTab, removeTab, setActiveTab, closeOthers, closeAll, closeRight, closeLeft } = useTabsStore() const scrollRef = useRef(null) // Auto-register tab on route change useEffect(() => { const path = location.pathname if (path === '/login') return const title = resolveTitle(menus || [], path) || path.split('/').filter(Boolean).pop()?.replace(/^\w/, c => c.toUpperCase()) || 'Page' addTab({ path, title, closable: path !== '/dashboard' }) }, [location.pathname, menus]) const handleClick = (path: string) => { setActiveTab(path) navigate(path) } const handleClose = (e: React.MouseEvent, path: string) => { e.stopPropagation() const next = removeTab(path) navigate(next) } const handleCloseOthers = (path: string) => { closeOthers(path) navigate(path) } const handleCloseAll = () => { const home = closeAll() navigate(home) } const scroll = (dir: 'left' | 'right') => { scrollRef.current?.scrollBy({ left: dir === 'left' ? -200 : 200, behavior: 'smooth' }) } const showArrows = tabs.length > 8 return (
{showArrows && ( )}
{tabs.map(tab => ( {tab.closable && ( { const next = removeTab(tab.path); navigate(next) }}> 关闭当前 )} handleCloseOthers(tab.path)}> 关闭其他 { closeRight(tab.path); navigate(tab.path) }}> 关闭右侧 { closeLeft(tab.path); navigate(tab.path) }}> 关闭左侧 关闭所有 ))}
{showArrows && ( )}
) }