import { useEffect, useState } from 'react' import { Command } from 'cmdk' import { useNavigate } from 'react-router-dom' import { Search, Monitor, Moon, Sun, Laptop } from 'lucide-react' import { useAppStore } from '@/store/app' import { useAuthStore } from '@/store/auth' import './cmdk.css' // We'll add some styles for cmdk export default function CommandPalette() { const { cmdKOpen, setCmdKOpen, setThemeHue } = useAppStore() const menus = useAuthStore(s => s.menus) const navigate = useNavigate() const [, setTheme] = useState(document.documentElement.classList.contains('dark') ? 'dark' : 'light') useEffect(() => { const down = (e: KeyboardEvent) => { if (e.key === 'k' && (e.metaKey || e.ctrlKey)) { e.preventDefault() setCmdKOpen(!cmdKOpen) } } document.addEventListener('keydown', down) return () => document.removeEventListener('keydown', down) }, [cmdKOpen, setCmdKOpen]) const runCommand = (command: () => void) => { setCmdKOpen(false) command() } const toggleTheme = (mode: 'light' | 'dark' | 'system') => { if (mode === 'dark') { document.documentElement.classList.add('dark') setTheme('dark') } else if (mode === 'light') { document.documentElement.classList.remove('dark') setTheme('light') } else { if (window.matchMedia('(prefers-color-scheme: dark)').matches) { document.documentElement.classList.add('dark') setTheme('dark') } else { document.documentElement.classList.remove('dark') setTheme('light') } } } // Flatten menus for search const flatMenus: { title: string, path: string }[] = [] const flatten = (items: any[]) => { items?.forEach(i => { if (i.path && !i.children?.length && i.path !== '/dashboard') { flatMenus.push({ title: i.title || i.name, path: i.path }) } if (i.children) flatten(i.children) }) } if (menus) flatten(menus) return (
没找到相关内容. runCommand(() => navigate('/dashboard'))} className="flex items-center px-3 py-2.5 text-sm rounded-lg cursor-pointer transition-colors aria-selected:bg-primary/10 aria-selected:text-primary text-foreground mt-1"> 仪表盘 {flatMenus.map(m => ( runCommand(() => navigate(m.path))} className="flex items-center px-3 py-2.5 text-sm rounded-lg cursor-pointer transition-colors aria-selected:bg-primary/10 aria-selected:text-primary text-foreground mt-1"> {m.title} ))} runCommand(() => toggleTheme('light'))} className="flex items-center px-3 py-2.5 text-sm rounded-lg cursor-pointer transition-colors aria-selected:bg-primary/10 aria-selected:text-primary text-foreground mt-1"> 浅色模式 runCommand(() => toggleTheme('dark'))} className="flex items-center px-3 py-2.5 text-sm rounded-lg cursor-pointer transition-colors aria-selected:bg-primary/10 aria-selected:text-primary text-foreground mt-1"> 深色模式 runCommand(() => toggleTheme('system'))} className="flex items-center px-3 py-2.5 text-sm rounded-lg cursor-pointer transition-colors aria-selected:bg-primary/10 aria-selected:text-primary text-foreground mt-1"> 跟随系统
) }