Skip to content

Commit

Permalink
fix: search wrong params make react-query crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
riccox committed Mar 31, 2023
1 parent d1af8b2 commit 19afc90
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/pages/index/documents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { openConfirmModal } from '@mantine/modals';
import { toast } from '@/src/utils/toast';
import MonacoEditor from '@monaco-editor/react';

const emptySearchResult = {
hits: [],
estimatedTotalHits: 0,
processingTimeMs: 0,
};

export const Documents = () => {
const host = useAppStore((state) => state.currentInstance?.host);
const apiKey = useAppStore((state) => state.currentInstance?.apiKey);
Expand Down Expand Up @@ -89,12 +95,18 @@ export const Documents = () => {
);

const searchDocumentsQuery = useQuery(
['searchDocuments', host, indexClient?.uid, searchForm.values],
[
'searchDocuments',
host,
indexClient?.uid,
// dependencies for the search refresh
searchForm.values,
],
async ({ queryKey }) => {
const { q, limit, offset, filter, sort } = { ...searchForm.values, ...(queryKey[3] as typeof searchForm.values) };
// prevent app error from request param invalid
if (searchForm.validate().hasErrors) return;
return await indexClient!.search(q, {
if (searchForm.validate().hasErrors) return emptySearchResult;
const data = await indexClient!.search(q, {
limit,
offset,
filter,
Expand All @@ -103,14 +115,13 @@ export const Documents = () => {
.filter((v) => v.trim().length > 0)
.map((v) => v.trim()),
});
return data || emptySearchResult;
},
{
enabled: !!currentIndex,
keepPreviousData: true,
refetchOnMount: 'always',
refetchInterval: 5000,
onSuccess: () => {},
onSettled: () => {},
}
);
const [isAddDocumentsByEditorModalOpen, setIsAddDocumentsByEditorModalOpen] = useState(false);
Expand Down

1 comment on commit 19afc90

@vercel
Copy link

@vercel vercel bot commented on 19afc90 Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.