(query: string, limit = 24)
| 9 | * merged. Pick a result → open the chart on exactly that source. |
| 10 | */ |
| 11 | export function useAssetSearch(query: string, limit = 24): { results: BarSourceCandidate[]; loading: boolean } { |
| 12 | const [results, setResults] = useState<BarSourceCandidate[]>([]) |
| 13 | const [loading, setLoading] = useState(false) |
| 14 | |
| 15 | useEffect(() => { |
| 16 | const q = query.trim() |
| 17 | if (!q) { setResults([]); setLoading(false); return } |
| 18 | setLoading(true) |
| 19 | let cancelled = false |
| 20 | const timer = setTimeout(async () => { |
| 21 | try { |
| 22 | const res = await barsApi.searchSources(q, limit) |
| 23 | if (!cancelled) setResults(res.candidates) |
| 24 | } catch (e) { |
| 25 | console.error('asset search failed', e) |
| 26 | if (!cancelled) setResults([]) |
| 27 | } finally { |
| 28 | if (!cancelled) setLoading(false) |
| 29 | } |
| 30 | }, 300) |
| 31 | return () => { cancelled = true; clearTimeout(timer) } |
| 32 | }, [query, limit]) |
| 33 | |
| 34 | return { results, loading } |
| 35 | } |
no test coverage detected