diff --git a/src/components/Document/search.tsx b/src/components/Document/search.tsx index d8d3821..61df1ea 100644 --- a/src/components/Document/search.tsx +++ b/src/components/Document/search.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; import { DocumentList } from '@/src/components/Document/list'; import { SearchBar } from '@/src/components/Document/searchBar'; import { useForm } from '@mantine/form'; @@ -7,6 +7,7 @@ import { useMeiliClient } from '@/src/hooks/useMeiliClient'; import { useCurrentInstance } from '@/src/hooks/useCurrentInstance'; import { useTranslation } from 'react-i18next'; import useDebounce from 'ahooks/lib/useDebounce'; +import { Loader } from '../Loader'; const emptySearchResult = { hits: [], @@ -55,7 +56,7 @@ export const SearchPage = ({ currentIndex }: Props) => { const debouncedSearchFormValue = useDebounce(searchForm.values, { wait: 450 }); const searchDocumentsQuery = useQuery({ - queryKey: ['searchDocuments', host, indexClient?.uid, debouncedSearchFormValue], + queryKey: ['searchDocuments', host, indexClient?.uid], queryFn: async ({ queryKey }) => { const { q, @@ -63,7 +64,7 @@ export const SearchPage = ({ currentIndex }: Props) => { offset, filter, sort = '', - } = { ...searchForm.values, ...(queryKey[3] as typeof searchForm.values) }; + } = { ...searchForm.values, ...(debouncedSearchFormValue as typeof searchForm.values) }; // prevent app error from request param invalid if (searchForm.validate().hasErrors) return emptySearchResult; @@ -71,7 +72,7 @@ export const SearchPage = ({ currentIndex }: Props) => { const sortExpressions: string[] = (sort.match(/(([\w\.]+)|(_geoPoint\([\d\.,\s]+\))){1}\:((asc)|(desc))/g) as string[]) || []; - console.log('search sorting expression', sort, sortExpressions); + console.debug('search sorting expression', sort, sortExpressions); try { const data = await indexClient!.search(q, { @@ -104,6 +105,13 @@ export const SearchPage = ({ currentIndex }: Props) => { await searchDocumentsQuery.refetch(); }, [searchDocumentsQuery]); + // use this to refresh search when typing, DO NOT use useQuery dependencies (will cause unknown rerender error). + useEffect(() => { + searchDocumentsQuery.refetch(); + // prevent infinite recursion rerender. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [debouncedSearchFormValue]); + return useMemo( () => (
@@ -127,14 +135,20 @@ export const SearchPage = ({ currentIndex }: Props) => {
{/* Doc List */}
- ({ - indexId: currentIndex, - content: i, - primaryKey: indexPrimaryKeyQuery.data!, - }))} - refetchDocs={searchDocumentsQuery.refetch} - /> + {searchDocumentsQuery.isFetching ? ( +
+ +
+ ) : ( + ({ + indexId: currentIndex, + content: i, + primaryKey: indexPrimaryKeyQuery.data!, + }))} + refetchDocs={searchDocumentsQuery.refetch} + /> + )}
), diff --git a/src/components/Document/searchForm.tsx b/src/components/Document/searchForm.tsx index 8b8f7ff..3cb2f0a 100644 --- a/src/components/Document/searchForm.tsx +++ b/src/components/Document/searchForm.tsx @@ -1,5 +1,5 @@ import { useMemo } from 'react'; -import { Loader, NumberInput, TextInput, Tooltip } from '@mantine/core'; +import { NumberInput, TextInput, Tooltip } from '@mantine/core'; import { IconAlignBoxLeftMiddle, IconArrowsSort, IconFilter, IconSearch } from '@tabler/icons-react'; import clsx from 'clsx'; import { UseFormReturnType } from '@mantine/form'; @@ -98,8 +98,6 @@ export const SearchForm = ({ {/* right btn group */}
- {isFetching && } - {/* submit btn */}
), - [indexIdEnable, isFetching, onFormSubmit, t, searchForm, searchFormError, submitBtnText] + [indexIdEnable, onFormSubmit, t, searchForm, searchFormError, submitBtnText] ); }; diff --git a/src/components/lazy.tsx b/src/components/lazy.tsx new file mode 100644 index 0000000..35b48df --- /dev/null +++ b/src/components/lazy.tsx @@ -0,0 +1,22 @@ +'use client'; +import { FC, ReactNode, Suspense } from 'react'; +import clsx from 'clsx'; +import { Loader } from './Loader'; +interface Props { + className?: string; + children: ReactNode; +} + +export const Lazy: FC = ({ className = '', children }) => { + return ( + + + + } + > + {children} + + ); +}; diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 854d9ee..5c9b048 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -14,6 +14,7 @@ import { EmptyArea } from '../components/EmptyArea'; import { UploadDoc } from '../pages/index/upload'; import { MultiIndexSearch } from '../pages/index/multi-search'; import { useTranslation } from 'react-i18next'; +import { Lazy } from '../components/lazy'; export const AppRoutes = () => { const { t } = useTranslation(); @@ -33,7 +34,14 @@ export const AppRoutes = () => { } /> } /> - } /> + + + + } + /> } /> } />