UI & Core: Support customising search API URL

This commit is contained in:
Fuma Nama
2024-03-05 18:53:23 +08:00
parent 46dc148e4e
commit 0e78dc8f7f
5 changed files with 30 additions and 15 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'fumadocs-core': patch
'fumadocs-ui': patch
---
Support customising search API URL
+1 -1
View File
@@ -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';
+1 -1
View File
@@ -21,7 +21,7 @@ interface DocumentRecord {
extra_data?: object;
}
interface SyncOptions {
export interface SyncOptions {
document?: string;
documents: DocumentRecord[];
}
+13 -10
View File
@@ -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