diff --git a/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx b/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx index d47e3c317..531044e2a 100644 --- a/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx +++ b/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx @@ -104,7 +104,7 @@ const invalidSearches = async ({ canvasElement }) => { await userEvent.clear(searchInput) - await userEvent.type(searchInput, "____", { + await userEvent.type(searchInput, "--", { delay: 100, }) } diff --git a/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx b/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx index 902b5b48a..c63cfcc23 100644 --- a/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx +++ b/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx @@ -15,17 +15,26 @@ export const SiteSearch: FunctionComponent = ({ }: SearchProps) => { const [searchQuery, setSearchQuery] = useState([]) const [queryResults, setQueryResults] = useState([]) + const [errorMessage, setErrorMessage] = useState("") useEffect(() => { if (searchQuery.length > 0) { - let searchResults = index.search(searchQuery) - let results = [] - searchResults.forEach(function (result) { - results.push(db[result.ref]) - }) - setQueryResults(searchResults) + // catch the error, and display an error message to help user + try { + let searchResults = index.search(searchQuery) + setQueryResults(searchResults) + setErrorMessage("") + } catch (error) { + if (error instanceof lunr.QueryParseError) { + setErrorMessage(error.message) + return + } else { + throw error + } + } } else { setQueryResults([]) + setErrorMessage("") } }, [searchQuery]) @@ -39,7 +48,8 @@ export const SiteSearch: FunctionComponent = ({ placeholder={"Type to search pages..."} onChange={(e) => setSearchQuery(e.target.value)} /> -
+

{errorMessage}

+
Number of found pages: {queryResults.length}