|
1 | 1 | import { create, search, load } from '@orama/orama'; |
2 | | -import { useState, useEffect } from 'react'; |
| 2 | +import { useState, useEffect, useRef } from 'react'; |
3 | 3 |
|
4 | 4 | import { relativeOrAbsolute } from '../utils/relativeOrAbsolute.mjs'; |
5 | 5 |
|
6 | 6 | /** |
7 | | - * Hook for initializing and managing Orama search database |
| 7 | + * Hook for initializing and managing Orama search database. |
| 8 | + * The search data is lazily fetched on the first search call. |
8 | 9 | * |
9 | 10 | * @param {string} pathname - The current page's path (e.g., '/api/fs') |
10 | 11 | */ |
11 | 12 | export default pathname => { |
12 | 13 | const [client, setClient] = useState(null); |
| 14 | + const loaded = useRef(null); |
13 | 15 |
|
14 | 16 | useEffect(() => { |
15 | 17 | // Create the database instance |
16 | 18 | const db = create({ |
17 | 19 | schema: {}, |
18 | 20 | }); |
19 | 21 |
|
| 22 | + /** |
| 23 | + * Ensures the search data is loaded. |
| 24 | + * @returns {Promise<void>} A promise that resolves when the data is loaded. |
| 25 | + */ |
| 26 | + const ensureLoaded = () => { |
| 27 | + if (!loaded.current) { |
| 28 | + loaded.current = fetch(relativeOrAbsolute('/orama-db.json', pathname)) |
| 29 | + .then(response => response.ok && response.json()) |
| 30 | + .then(data => load(db, data)) |
| 31 | + .catch(() => { |
| 32 | + loaded.current = null; |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + return loaded.current; |
| 37 | + }; |
| 38 | + |
20 | 39 | // TODO(@avivkeller): Ask Orama to support this functionality natively |
21 | 40 | /** |
22 | 41 | * @param {any} options |
23 | 42 | */ |
24 | | - db.search = options => search(db, options); |
| 43 | + db.search = async options => { |
| 44 | + await ensureLoaded(); |
| 45 | + return search(db, options); |
| 46 | + }; |
25 | 47 |
|
26 | 48 | setClient(db); |
27 | | - |
28 | | - // Load the search data |
29 | | - fetch(relativeOrAbsolute('/orama-db.json', pathname)) |
30 | | - .then(response => response.ok && response.json()) |
31 | | - .then(data => load(db, data)); |
32 | 49 | }, []); |
33 | 50 |
|
34 | 51 | return client; |
|
0 commit comments