|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState, useCallback } from "react"; |
| 4 | +import { useSearchParams, useRouter } from "next/navigation"; |
| 5 | +import { getFreighterPublicKey } from "@/lib/freighter"; |
| 6 | +import { useDebounce } from "@/hooks/useDebounce"; |
| 7 | +import HighlightedText from "@/components/invoice/HighlightedText"; |
| 8 | +import type { Invoice } from "@stellar-split/sdk"; |
| 9 | + |
| 10 | +const DEBOUNCE_DELAY_MS = 300; |
| 11 | + |
| 12 | +interface InvoiceSearchProps { |
| 13 | + onSelectInvoice?: (invoice: Invoice) => void; |
| 14 | +} |
| 15 | + |
| 16 | +export default function InvoiceSearch({ onSelectInvoice }: InvoiceSearchProps) { |
| 17 | + const router = useRouter(); |
| 18 | + const searchParams = useSearchParams(); |
| 19 | + const initialQuery = searchParams.get("q") || ""; |
| 20 | + |
| 21 | + const [inputValue, setInputValue] = useState(initialQuery); |
| 22 | + const [results, setResults] = useState<Invoice[]>([]); |
| 23 | + const [loading, setLoading] = useState(false); |
| 24 | + const [error, setError] = useState<string | null>(null); |
| 25 | + const [publicKey, setPublicKey] = useState<string | null>(null); |
| 26 | + |
| 27 | + const debouncedQuery = useDebounce(inputValue, DEBOUNCE_DELAY_MS); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + getFreighterPublicKey() |
| 31 | + .then(setPublicKey) |
| 32 | + .catch(() => setPublicKey(null)); |
| 33 | + }, []); |
| 34 | + |
| 35 | + const performSearch = useCallback(async () => { |
| 36 | + if (!debouncedQuery || !publicKey) { |
| 37 | + setResults([]); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + setLoading(true); |
| 42 | + setError(null); |
| 43 | + |
| 44 | + try { |
| 45 | + const response = await fetch( |
| 46 | + `/api/invoices?publicKey=${encodeURIComponent(publicKey)}&q=${encodeURIComponent(debouncedQuery)}` |
| 47 | + ); |
| 48 | + |
| 49 | + if (!response.ok) { |
| 50 | + throw new Error("Search failed"); |
| 51 | + } |
| 52 | + |
| 53 | + const data = await response.json(); |
| 54 | + setResults(data.invoices || []); |
| 55 | + } catch (err) { |
| 56 | + setError(err instanceof Error ? err.message : "Search error"); |
| 57 | + setResults([]); |
| 58 | + } finally { |
| 59 | + setLoading(false); |
| 60 | + } |
| 61 | + }, [debouncedQuery, publicKey]); |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + performSearch(); |
| 65 | + }, [debouncedQuery, performSearch]); |
| 66 | + |
| 67 | + const handleClear = () => { |
| 68 | + setInputValue(""); |
| 69 | + setResults([]); |
| 70 | + setError(null); |
| 71 | + }; |
| 72 | + |
| 73 | + const handleSelectInvoice = (invoice: Invoice) => { |
| 74 | + onSelectInvoice?.(invoice); |
| 75 | + router.push(`/invoice/${invoice.id}`); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="space-y-4"> |
| 80 | + <div className="relative"> |
| 81 | + <input |
| 82 | + type="text" |
| 83 | + placeholder="Search invoices by title or memo..." |
| 84 | + value={inputValue} |
| 85 | + onChange={(e) => setInputValue(e.target.value)} |
| 86 | + className="w-full px-4 py-2 bg-gray-900 border border-gray-700 rounded-lg text-sm text-gray-100 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-indigo-500" |
| 87 | + /> |
| 88 | + {inputValue && ( |
| 89 | + <button |
| 90 | + onClick={handleClear} |
| 91 | + className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-200 text-xs font-medium" |
| 92 | + > |
| 93 | + Clear |
| 94 | + </button> |
| 95 | + )} |
| 96 | + </div> |
| 97 | + |
| 98 | + {error && ( |
| 99 | + <div className="text-sm text-red-400 bg-red-950/40 border border-red-800 rounded-lg px-3 py-2"> |
| 100 | + {error} |
| 101 | + </div> |
| 102 | + )} |
| 103 | + |
| 104 | + {loading && ( |
| 105 | + <div className="text-sm text-gray-400">Searching…</div> |
| 106 | + )} |
| 107 | + |
| 108 | + {!loading && inputValue && results.length === 0 && !error && ( |
| 109 | + <div className="text-sm text-gray-400"> |
| 110 | + No results for "<span className="font-medium">{inputValue}</span>" |
| 111 | + </div> |
| 112 | + )} |
| 113 | + |
| 114 | + {results.length > 0 && ( |
| 115 | + <ul className="space-y-2"> |
| 116 | + {results.map((invoice) => ( |
| 117 | + <li key={invoice.id}> |
| 118 | + <button |
| 119 | + onClick={() => handleSelectInvoice(invoice)} |
| 120 | + className="w-full text-left px-4 py-3 rounded-lg bg-gray-800/40 hover:bg-gray-700/40 border border-gray-700 hover:border-gray-600 transition-colors" |
| 121 | + > |
| 122 | + <div className="font-medium text-gray-100"> |
| 123 | + <HighlightedText |
| 124 | + text={invoice.title || `Invoice #${invoice.id}`} |
| 125 | + query={inputValue} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + {(invoice as any).memo && ( |
| 129 | + <div className="text-xs text-gray-400 mt-1"> |
| 130 | + <HighlightedText |
| 131 | + text={(invoice as any).memo} |
| 132 | + query={inputValue} |
| 133 | + /> |
| 134 | + </div> |
| 135 | + )} |
| 136 | + </button> |
| 137 | + </li> |
| 138 | + ))} |
| 139 | + </ul> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + ); |
| 143 | +} |
0 commit comments