|
| 1 | +import { router } from '@inertiajs/react'; |
| 2 | +import { useEffect, useRef, useState } from 'react'; |
| 3 | + |
| 4 | +interface SearchResult { |
| 5 | + type: string; |
| 6 | + label: string; |
| 7 | + sub?: string; |
| 8 | + href: string; |
| 9 | +} |
| 10 | + |
| 11 | +interface Props { |
| 12 | + open: boolean; |
| 13 | + onClose: () => void; |
| 14 | +} |
| 15 | + |
| 16 | +const TYPE_COLORS: Record<string, string> = { |
| 17 | + 'Invoice': 'bg-blue-100 text-blue-700', |
| 18 | + 'Contact': 'bg-purple-100 text-purple-700', |
| 19 | + 'Product': 'bg-emerald-100 text-emerald-700', |
| 20 | + 'Purchase Order': 'bg-amber-100 text-amber-700', |
| 21 | + 'Employee': 'bg-teal-100 text-teal-700', |
| 22 | +}; |
| 23 | + |
| 24 | +export function CommandPalette({ open, onClose }: Props) { |
| 25 | + const [query, setQuery] = useState(''); |
| 26 | + const [results, setResults] = useState<SearchResult[]>([]); |
| 27 | + const [loading, setLoading] = useState(false); |
| 28 | + const [selected, setSelected] = useState(0); |
| 29 | + const inputRef = useRef<HTMLInputElement>(null); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (open) { |
| 33 | + setQuery(''); |
| 34 | + setResults([]); |
| 35 | + setSelected(0); |
| 36 | + setTimeout(() => inputRef.current?.focus(), 50); |
| 37 | + } |
| 38 | + }, [open]); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (query.length < 2) { setResults([]); return; } |
| 42 | + |
| 43 | + const timer = setTimeout(async () => { |
| 44 | + setLoading(true); |
| 45 | + try { |
| 46 | + const res = await fetch(`/search?q=${encodeURIComponent(query)}`, { |
| 47 | + headers: { 'X-Requested-With': 'XMLHttpRequest' }, |
| 48 | + }); |
| 49 | + const data = await res.json(); |
| 50 | + setResults(data.results ?? []); |
| 51 | + setSelected(0); |
| 52 | + } catch { |
| 53 | + setResults([]); |
| 54 | + } finally { |
| 55 | + setLoading(false); |
| 56 | + } |
| 57 | + }, 280); |
| 58 | + |
| 59 | + return () => clearTimeout(timer); |
| 60 | + }, [query]); |
| 61 | + |
| 62 | + function navigate(href: string) { |
| 63 | + router.visit(href); |
| 64 | + onClose(); |
| 65 | + } |
| 66 | + |
| 67 | + function handleKeyDown(e: React.KeyboardEvent) { |
| 68 | + if (e.key === 'Escape') { onClose(); return; } |
| 69 | + if (e.key === 'ArrowDown') { e.preventDefault(); setSelected((s) => Math.min(s + 1, results.length - 1)); } |
| 70 | + if (e.key === 'ArrowUp') { e.preventDefault(); setSelected((s) => Math.max(s - 1, 0)); } |
| 71 | + if (e.key === 'Enter' && results[selected]) { navigate(results[selected].href); } |
| 72 | + } |
| 73 | + |
| 74 | + if (!open) return null; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div |
| 78 | + className="fixed inset-0 z-50 flex items-start justify-center pt-20 px-4" |
| 79 | + onClick={onClose} |
| 80 | + > |
| 81 | + {/* Backdrop */} |
| 82 | + <div className="absolute inset-0 bg-slate-900/40 backdrop-blur-sm" /> |
| 83 | + |
| 84 | + {/* Palette */} |
| 85 | + <div |
| 86 | + className="relative w-full max-w-xl rounded-xl border border-slate-200 bg-white shadow-2xl overflow-hidden" |
| 87 | + onClick={(e) => e.stopPropagation()} |
| 88 | + > |
| 89 | + {/* Input */} |
| 90 | + <div className="flex items-center gap-3 px-4 py-3 border-b border-slate-200"> |
| 91 | + <svg className="h-5 w-5 text-slate-400 shrink-0" fill="none" stroke="currentColor" strokeWidth={1.75} viewBox="0 0 24 24"> |
| 92 | + <path strokeLinecap="round" strokeLinejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" /> |
| 93 | + </svg> |
| 94 | + <input |
| 95 | + ref={inputRef} |
| 96 | + type="text" |
| 97 | + value={query} |
| 98 | + onChange={(e) => setQuery(e.target.value)} |
| 99 | + onKeyDown={handleKeyDown} |
| 100 | + placeholder="Search invoices, employees, products…" |
| 101 | + className="flex-1 text-sm text-slate-900 placeholder:text-slate-400 focus:outline-none" |
| 102 | + /> |
| 103 | + <kbd className="hidden sm:flex items-center gap-0.5 rounded border border-slate-200 bg-slate-50 px-1.5 py-0.5 text-[10px] text-slate-400"> |
| 104 | + ESC |
| 105 | + </kbd> |
| 106 | + </div> |
| 107 | + |
| 108 | + {/* Results */} |
| 109 | + {loading && ( |
| 110 | + <div className="px-4 py-3 text-sm text-slate-400">Searching…</div> |
| 111 | + )} |
| 112 | + |
| 113 | + {!loading && query.length >= 2 && results.length === 0 && ( |
| 114 | + <div className="px-4 py-6 text-center text-sm text-slate-400">No results for "{query}"</div> |
| 115 | + )} |
| 116 | + |
| 117 | + {!loading && results.length > 0 && ( |
| 118 | + <ul className="max-h-80 overflow-y-auto py-1"> |
| 119 | + {results.map((r, i) => ( |
| 120 | + <li key={`${r.type}-${r.href}-${i}`}> |
| 121 | + <button |
| 122 | + className={[ |
| 123 | + 'w-full flex items-center gap-3 px-4 py-2.5 text-left transition-colors', |
| 124 | + i === selected ? 'bg-indigo-50' : 'hover:bg-slate-50', |
| 125 | + ].join(' ')} |
| 126 | + onClick={() => navigate(r.href)} |
| 127 | + onMouseEnter={() => setSelected(i)} |
| 128 | + > |
| 129 | + <span className={`shrink-0 inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-medium ${TYPE_COLORS[r.type] ?? 'bg-slate-100 text-slate-600'}`}> |
| 130 | + {r.type} |
| 131 | + </span> |
| 132 | + <span className="flex-1 min-w-0"> |
| 133 | + <span className="block text-sm font-medium text-slate-900 truncate">{r.label}</span> |
| 134 | + {r.sub && <span className="block text-xs text-slate-500 truncate">{r.sub}</span>} |
| 135 | + </span> |
| 136 | + <svg className="h-4 w-4 text-slate-300 shrink-0" fill="none" stroke="currentColor" strokeWidth={1.75} viewBox="0 0 24 24"> |
| 137 | + <path strokeLinecap="round" strokeLinejoin="round" d="M13.5 4.5L21 12m0 0l-7.5 7.5M21 12H3" /> |
| 138 | + </svg> |
| 139 | + </button> |
| 140 | + </li> |
| 141 | + ))} |
| 142 | + </ul> |
| 143 | + )} |
| 144 | + |
| 145 | + {!query && ( |
| 146 | + <div className="px-4 py-4 text-xs text-slate-400"> |
| 147 | + Type at least 2 characters to search across invoices, employees, products, and more. |
| 148 | + </div> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ); |
| 153 | +} |
0 commit comments