'use client'; import { useState, useEffect, useRef } from 'react'; import { Combobox, Dialog, DialogBackdrop, DialogPanel } from '@headlessui/react'; import { MagnifyingGlassIcon } from '@heroicons/react/24/outline'; import { useRouter } from 'next/navigation'; import { HomeIcon, RocketLaunchIcon, ChartBarIcon, BriefcaseIcon, LifebuoyIcon, CreditCardIcon, DocumentTextIcon, FolderIcon, ShareIcon, Cog6ToothIcon, PlusIcon, ArrowRightIcon } from '@heroicons/react/24/outline'; interface CommandPaletteProps { isOpen: boolean; setIsOpen: (isOpen: boolean) => void; } export default function CommandPalette({ isOpen, setIsOpen }: CommandPaletteProps) { const [query, setQuery] = useState(''); const [availableSolutions, setAvailableSolutions] = useState([]); const router = useRouter(); const inputRef = useRef(null); // Buscar soluções disponíveis useEffect(() => { const fetchSolutions = async () => { try { const response = await fetch('/api/tenant/solutions', { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}`, }, }); if (response.ok) { const data = await response.json(); const solutions = data.solutions || []; const slugs = solutions.map((s: any) => s.slug.toLowerCase()); setAvailableSolutions(['dashboard', ...slugs]); // Dashboard sempre disponível } else { // Fallback: mostrar tudo setAvailableSolutions(['dashboard', 'crm', 'erp', 'projetos', 'helpdesk', 'pagamentos', 'contratos', 'documentos', 'social']); } } catch (error) { console.error('Erro ao buscar soluções:', error); // Fallback: mostrar tudo setAvailableSolutions(['dashboard', 'crm', 'erp', 'projetos', 'helpdesk', 'pagamentos', 'contratos', 'documentos', 'social']); } }; if (isOpen) { fetchSolutions(); } }, [isOpen]); // Atalho de teclado (Ctrl+K ou Cmd+K) useEffect(() => { const onKeydown = (event: KeyboardEvent) => { if (event.key === 'k' && (event.metaKey || event.ctrlKey)) { event.preventDefault(); setIsOpen(true); } }; window.addEventListener('keydown', onKeydown); return () => { window.removeEventListener('keydown', onKeydown); }; }, [setIsOpen]); const navigation = [ { name: 'Visão Geral', href: '/dashboard', icon: HomeIcon, category: 'Navegação', solution: 'dashboard' }, { name: 'CRM', href: '/crm', icon: RocketLaunchIcon, category: 'Navegação', solution: 'crm' }, { name: 'ERP', href: '/erp', icon: ChartBarIcon, category: 'Navegação', solution: 'erp' }, { name: 'Projetos', href: '/projetos', icon: BriefcaseIcon, category: 'Navegação', solution: 'projetos' }, { name: 'Helpdesk', href: '/helpdesk', icon: LifebuoyIcon, category: 'Navegação', solution: 'helpdesk' }, { name: 'Pagamentos', href: '/pagamentos', icon: CreditCardIcon, category: 'Navegação', solution: 'pagamentos' }, { name: 'Contratos', href: '/contratos', icon: DocumentTextIcon, category: 'Navegação', solution: 'contratos' }, { name: 'Documentos', href: '/documentos', icon: FolderIcon, category: 'Navegação', solution: 'documentos' }, { name: 'Redes Sociais', href: '/social', icon: ShareIcon, category: 'Navegação', solution: 'social' }, { name: 'Configurações', href: '/configuracoes', icon: Cog6ToothIcon, category: 'Navegação', solution: 'dashboard' }, // Ações { name: 'Novo Projeto', href: '/projetos/novo', icon: PlusIcon, category: 'Ações', solution: 'projetos' }, { name: 'Novo Chamado', href: '/helpdesk/novo', icon: PlusIcon, category: 'Ações', solution: 'helpdesk' }, { name: 'Novo Contrato', href: '/contratos/novo', icon: PlusIcon, category: 'Ações', solution: 'contratos' }, ]; // Filtrar por soluções disponíveis const allowedNavigation = navigation.filter(item => availableSolutions.includes(item.solution) ); const filteredItems = query === '' ? allowedNavigation : allowedNavigation.filter((item) => { return item.name.toLowerCase().includes(query.toLowerCase()); }); // Agrupar itens por categoria const groups = filteredItems.reduce((acc, item) => { if (!acc[item.category]) { acc[item.category] = []; } acc[item.category].push(item); return acc; }, {} as Record); const handleSelect = (item: typeof navigation[0] | null) => { if (!item) return; setIsOpen(false); router.push(item.href); setQuery(''); }; return (
{filteredItems.length > 0 && ( {Object.entries(groups).map(([category, items]) => (
{category}
{items.map((item) => ( `cursor-pointer select-none px-4 py-2.5 transition-colors ${active ? '[background:var(--gradient)] text-white' : '' }` } > {({ active }) => (
{item.name} {active && ( )}
)}
))}
))}
)} {query !== '' && filteredItems.length === 0 && (
)}
Selecionar Navegar
Esc Fechar
); }