-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathuseOrama.mjs
More file actions
35 lines (28 loc) · 894 Bytes
/
useOrama.mjs
File metadata and controls
35 lines (28 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { create, search, load } from '@orama/orama';
import { useState, useEffect } from 'react';
import { relativeOrAbsolute } from '../utils/relativeOrAbsolute.mjs';
/**
* Hook for initializing and managing Orama search database
*
* @param {string} pathname - The current page's path (e.g., '/api/fs')
*/
export default pathname => {
const [client, setClient] = useState(null);
useEffect(() => {
// Create the database instance
const db = create({
schema: {},
});
// TODO(@avivkeller): Ask Orama to support this functionality natively
/**
* @param {any} options
*/
db.search = options => 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;
};