Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/generators/web/ui/hooks/useOrama.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
import { create, search, load } from '@orama/orama';
import { useState, useEffect } from 'react';
import { useState, useEffect, useRef } from 'react';

import { relativeOrAbsolute } from '../utils/relativeOrAbsolute.mjs';

/**
* Hook for initializing and managing Orama search database
* Hook for initializing and managing Orama search database.
* The search data is lazily fetched on the first search call.
*
* @param {string} pathname - The current page's path (e.g., '/api/fs')
*/
export default pathname => {
const [client, setClient] = useState(null);
const loaded = useRef(null);

useEffect(() => {
// Create the database instance
const db = create({
schema: {},
});

/**
* Ensures the search data is loaded.
* @returns {Promise<void>} A promise that resolves when the data is loaded.
*/
const ensureLoaded = () => {
if (!loaded.current) {
loaded.current = fetch(relativeOrAbsolute('/orama-db.json', pathname))
.then(response => response.ok && response.json())
.then(data => load(db, data))
.catch(() => {
loaded.current = null;
});
}

return loaded.current;
};

// TODO(@avivkeller): Ask Orama to support this functionality natively
/**
* @param {any} options
*/
db.search = options => search(db, options);
db.search = async options => {
await ensureLoaded();
return search(db, options);
};

setClient(db);

// Load the search data
fetch(relativeOrAbsolute('/orama-db.json', pathname))
.then(response => response.ok && response.json())
.then(data => load(db, data));
}, []);

return client;
Expand Down
Loading