UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

54 lines (53 loc) • 2.08 kB
import { n as useSearch } from "../../use-search-eSAjMJVB.js"; import { useCallback, useMemo } from "react"; // src/search/client/docs-search.ts /** * Provide a hook to query different official search clients. * * Note: it will re-query when its parameters changed, make sure to define `deps` array if you encounter rendering issues. * * @deprecated Use the search hook of your provider instead, like `useFetchSearch()`, or `experimental_useSearch()` for other clients. */ function useDocsSearch({ delayMs, allowEmpty, ...options }, customDeps) { const client = "type" in options ? createPresetClient(options) : options.client; const run = useCallback((query) => client.search(query), customDeps ?? client.deps ?? []); const output = useSearch(run, { delayMs, allowEmpty }); const data = useMemo(() => { if (!output.data) return "empty"; const out = []; for (const item of output.data.items) if (item.type === "table") out.push(...item.rows); else out.push(item); return out; }, [output.data]); return { ...output, setSearch: output.onSearchChange, query: { isLoading: output.isLoading, data, error: output.error } }; } function createPresetClient(options) { const client = loadPreset(options); return { deps: [JSON.stringify(options)], search: async (query) => (await client).search(query) }; } function loadPreset(options) { switch (options.type) { case "fetch": return import("./fetch.js").then((mod) => mod.fetchClient(options)); case "algolia": return import("./algolia.js").then((mod) => mod.algoliaClient(options)); case "orama-cloud": return import("./orama-cloud.js").then((mod) => mod.oramaCloudClient(options)); case "orama-cloud-legacy": return import("./orama-cloud-legacy.js").then((mod) => mod.oramaCloudLegacyClient(options)); case "mixedbread": return import("./mixedbread.js").then((mod) => mod.mixedbreadClient(options)); case "static": return import("./orama-static.js").then((mod) => mod.staticClient(options)); default: throw new Error("unknown search client"); } } export { useDocsSearch };