fumadocs-core
Version:
The React.js library for building a documentation website
100 lines (97 loc) • 3.16 kB
JavaScript
import { useOnChange } from "../utils/use-on-change.js";
import { useEffect, useRef, useState } from "react";
//#region src/utils/use-debounce.ts
function useDebounce(value, delayMs = 1e3) {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
if (delayMs === 0) return;
const handler = window.setTimeout(() => {
setDebouncedValue(value);
}, delayMs);
return () => clearTimeout(handler);
}, [delayMs, value]);
if (delayMs === 0) return value;
return debouncedValue;
}
//#endregion
//#region src/search/client.ts
function isDeepEqual(a, b) {
if (a === b) return true;
if (Array.isArray(a) && Array.isArray(b)) return b.length === a.length && a.every((v, i) => isDeepEqual(v, b[i]));
if (typeof a === "object" && a && typeof b === "object" && b) {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
return aKeys.length === bKeys.length && aKeys.every((key) => Object.hasOwn(b, key) && isDeepEqual(a[key], b[key]));
}
return false;
}
/**
* Provide a hook to query different official search clients.
*
* Note: it will re-query when its parameters changed, make sure to use `useMemo()` on `clientOptions` or define `deps` array.
*/
function useDocsSearch(clientOptions, deps) {
const { delayMs = 100, allowEmpty = false, ...client } = clientOptions;
const [search, setSearch] = useState("");
const [results, setResults] = useState("empty");
const [error, setError] = useState();
const [isLoading, setIsLoading] = useState(false);
const debouncedValue = useDebounce(search, delayMs);
const onStart = useRef(void 0);
useOnChange([deps ?? clientOptions, debouncedValue], () => {
if (onStart.current) {
onStart.current();
onStart.current = void 0;
}
setIsLoading(true);
let interrupt = false;
onStart.current = () => {
interrupt = true;
};
async function run() {
if (debouncedValue.length === 0 && !allowEmpty) return "empty";
if (client.type === "fetch") {
const { fetchDocs } = await import("../fetch-B5e9CFfN.js");
return fetchDocs(debouncedValue, client);
}
if (client.type === "algolia") {
const { searchDocs } = await import("../algolia-CQPXCnjV.js");
return searchDocs(debouncedValue, client);
}
if (client.type === "orama-cloud") {
const { searchDocs } = await import("../orama-cloud-DH3g37zc.js");
return searchDocs(debouncedValue, client);
}
if (client.type === "static") {
const { search: search$1 } = await import("../static-Dq8pA8Ay.js");
return search$1(debouncedValue, client);
}
if (client.type === "mixedbread") {
const { search: search$1 } = await import("../mixedbread-DlByNYSd.js");
return search$1(debouncedValue, client);
}
throw new Error("unknown search client");
}
run().then((res) => {
if (interrupt) return;
setError(void 0);
setResults(res);
}).catch((err) => {
setError(err);
}).finally(() => {
setIsLoading(false);
});
}, deps ? void 0 : (a, b) => !isDeepEqual(a, b));
return {
search,
setSearch,
query: {
isLoading,
data: results,
error
}
};
}
//#endregion
export { useDocsSearch };
//# sourceMappingURL=client.js.map