mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 23:49:51 +00:00
UI & Core: Support customising search API URL
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'fumadocs-core': patch
|
||||
'fumadocs-ui': patch
|
||||
---
|
||||
|
||||
Support customising search API URL
|
||||
@@ -80,7 +80,7 @@ export function useAlgoliaSearch(
|
||||
const [search, setSearch] = useState('');
|
||||
|
||||
const query = useSWR(
|
||||
['/api/search', search, allowEmpty, options],
|
||||
['algolia-search', search, allowEmpty, options],
|
||||
async () => {
|
||||
if (allowEmpty && search.length === 0) return 'empty';
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ interface DocumentRecord {
|
||||
extra_data?: object;
|
||||
}
|
||||
|
||||
interface SyncOptions {
|
||||
export interface SyncOptions {
|
||||
document?: string;
|
||||
documents: DocumentRecord[];
|
||||
}
|
||||
|
||||
@@ -31,19 +31,22 @@ async function fetchDocs(
|
||||
return (await res.json()) as SortedResult[];
|
||||
}
|
||||
|
||||
export function useDocsSearch(locale?: string, tag?: string): UseDocsSearch {
|
||||
/**
|
||||
* @param locale - Filter with locale
|
||||
* @param tag - Filter with specific tag
|
||||
* @param api - The Search API URL
|
||||
*/
|
||||
export function useDocsSearch(
|
||||
locale?: string,
|
||||
tag?: string,
|
||||
api = '/api/search',
|
||||
): UseDocsSearch {
|
||||
const [search, setSearch] = useState('');
|
||||
const debouncedValue = useDebounce(search, 100);
|
||||
|
||||
const query = useSWR(
|
||||
['/api/search', debouncedValue, locale, tag],
|
||||
async ([api, value]) => {
|
||||
return fetchDocs(api, value, locale, tag);
|
||||
},
|
||||
{
|
||||
keepPreviousData: true,
|
||||
},
|
||||
);
|
||||
const query = useSWR([api, debouncedValue, locale, tag], fetchDocs, {
|
||||
keepPreviousData: true,
|
||||
});
|
||||
|
||||
return { search, setSearch, query };
|
||||
}
|
||||
|
||||
@@ -9,12 +9,13 @@ export type DefaultSearchDialogProps = SharedProps & ContentProps;
|
||||
|
||||
export default function DefaultSearchDialog({
|
||||
tag,
|
||||
api,
|
||||
footer,
|
||||
...props
|
||||
}: DefaultSearchDialogProps): JSX.Element {
|
||||
return (
|
||||
<SearchDialog {...props}>
|
||||
<Content tag={tag} footer={footer} />
|
||||
<Content tag={tag} footer={footer} api={api} />
|
||||
</SearchDialog>
|
||||
);
|
||||
}
|
||||
@@ -25,12 +26,17 @@ interface ContentProps {
|
||||
*/
|
||||
tag?: string;
|
||||
|
||||
/**
|
||||
* Search API URL
|
||||
*/
|
||||
api?: string;
|
||||
|
||||
footer?: ReactNode;
|
||||
}
|
||||
|
||||
function Content({ tag, footer }: ContentProps): JSX.Element {
|
||||
function Content({ tag, footer, api }: ContentProps): JSX.Element {
|
||||
const { locale } = useI18n();
|
||||
const { search, setSearch, query } = useDocsSearch(locale, tag);
|
||||
const { search, setSearch, query } = useDocsSearch(locale, tag, api);
|
||||
|
||||
return (
|
||||
<SearchDialogContent
|
||||
|
||||
Reference in New Issue
Block a user