import { useState, useCallback } from 'react' import { Search, FileText, Server, Boxes, Smile, MessageSquare, UserCircle, FileSearch, BarChart3, Package, Settings, Home, Hash } from 'lucide-react' import { useNavigate } from '@tanstack/react-router' import { Dialog, DialogContent, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Input } from '@/components/ui/input' import { ScrollArea } from '@/components/ui/scroll-area' import { cn } from '@/lib/utils' interface SearchDialogProps { open: boolean onOpenChange: (open: boolean) => void } interface SearchItem { icon: React.ComponentType<{ className?: string }> title: string description: string path: string category: string } const searchItems: SearchItem[] = [ { icon: Home, title: '首页', description: '查看仪表板概览', path: '/', category: '概览', }, { icon: FileText, title: '麦麦主程序配置', description: '配置麦麦的核心设置', path: '/config/bot', category: '配置', }, { icon: Server, title: '麦麦模型提供商配置', description: '配置模型提供商', path: '/config/modelProvider', category: '配置', }, { icon: Boxes, title: '麦麦模型配置', description: '配置模型参数', path: '/config/model', category: '配置', }, { icon: Smile, title: '表情包管理', description: '管理麦麦的表情包', path: '/resource/emoji', category: '资源', }, { icon: MessageSquare, title: '表达方式管理', description: '管理麦麦的表达方式', path: '/resource/expression', category: '资源', }, { icon: UserCircle, title: '人物信息管理', description: '管理人物信息', path: '/resource/person', category: '资源', }, { icon: Hash, title: '黑话管理', description: '管理麦麦学习到的黑话和俚语', path: '/resource/jargon', category: '资源', }, { icon: BarChart3, title: '统计信息', description: '查看使用统计', path: '/statistics', category: '监控', }, { icon: Package, title: '插件市场', description: '浏览和安装插件', path: '/plugins', category: '扩展', }, { icon: FileSearch, title: '日志查看器', description: '查看系统日志', path: '/logs', category: '监控', }, { icon: Settings, title: '系统设置', description: '配置系统参数', path: '/settings', category: '系统', }, ] export function SearchDialog({ open, onOpenChange }: SearchDialogProps) { const [searchQuery, setSearchQuery] = useState('') const [selectedIndex, setSelectedIndex] = useState(0) const navigate = useNavigate() // 过滤搜索结果 const filteredItems = searchItems.filter( (item) => item.title.toLowerCase().includes(searchQuery.toLowerCase()) || item.description.toLowerCase().includes(searchQuery.toLowerCase()) || item.category.toLowerCase().includes(searchQuery.toLowerCase()) ) // 导航到页面 const handleNavigate = useCallback((path: string) => { navigate({ to: path }) onOpenChange(false) // 在导航后重置状态 setSearchQuery('') setSelectedIndex(0) }, [navigate, onOpenChange]) // 键盘导航 const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === 'ArrowDown') { e.preventDefault() setSelectedIndex((prev) => (prev + 1) % filteredItems.length) } else if (e.key === 'ArrowUp') { e.preventDefault() setSelectedIndex((prev) => (prev - 1 + filteredItems.length) % filteredItems.length) } else if (e.key === 'Enter' && filteredItems[selectedIndex]) { e.preventDefault() handleNavigate(filteredItems[selectedIndex].path) } }, [filteredItems, selectedIndex, handleNavigate] ) return ( 搜索
{ setSearchQuery(e.target.value) setSelectedIndex(0) }} onKeyDown={handleKeyDown} placeholder="搜索页面..." className="h-12 pl-11 text-base border-0 focus-visible:ring-0 shadow-none" autoFocus />
{filteredItems.length > 0 ? (
{filteredItems.map((item, index) => { const Icon = item.icon return ( ) })}
) : (

{searchQuery ? '未找到匹配的页面' : '输入关键词开始搜索'}

)}
导航 Enter 选择 Esc 关闭
) }