From d8763997a5d3bd5187746fd337765e5f014e42bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Thu, 9 Jan 2025 17:22:06 +0100 Subject: [PATCH] Fix UI search with no ask (#2713) --- .changeset/angry-lemons-greet.md | 5 +++ .../src/components/Search/SearchResults.tsx | 34 ++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 .changeset/angry-lemons-greet.md diff --git a/.changeset/angry-lemons-greet.md b/.changeset/angry-lemons-greet.md new file mode 100644 index 000000000..b95a43a2a --- /dev/null +++ b/.changeset/angry-lemons-greet.md @@ -0,0 +1,5 @@ +--- +'gitbook': patch +--- + +Fix UI search without ask AI enabled and fix error with questions not returned from API diff --git a/packages/gitbook/src/components/Search/SearchResults.tsx b/packages/gitbook/src/components/Search/SearchResults.tsx index e599a5597..7aaeab0ef 100644 --- a/packages/gitbook/src/components/Search/SearchResults.tsx +++ b/packages/gitbook/src/components/Search/SearchResults.tsx @@ -1,3 +1,4 @@ +import { captureException } from '@sentry/nextjs'; import assertNever from 'assert-never'; import React from 'react'; @@ -47,7 +48,7 @@ export const SearchResults = React.forwardRef(function SearchResults( }, ref: React.Ref, ) { - const { children, query, pointer, spaceId, revisionId, global, withAsk, onSwitchToAsk } = props; + const { children, query, pointer, spaceId, revisionId, withAsk, global, onSwitchToAsk } = props; const language = useLanguage(); const trackEvent = useTrackEvent(); @@ -62,6 +63,7 @@ export const SearchResults = React.forwardRef(function SearchResults( React.useEffect(() => { if (!query) { if (!withAsk) { + setResultsState({ results: [], fetching: false }); return; } @@ -74,6 +76,16 @@ export const SearchResults = React.forwardRef(function SearchResults( setResultsState({ results: [], fetching: true }); getRecommendedQuestions(spaceId).then((questions) => { + if (!questions) { + if (!cancelled) { + setResultsState({ results: [], fetching: false }); + } + captureException( + new Error('questions is null, meaning the cache is probably corrupted'), + ); + return; + } + const results = questions.map((question) => ({ type: 'recommended-question', id: question, @@ -104,6 +116,14 @@ export const SearchResults = React.forwardRef(function SearchResults( return; } + if (!results) { + setResultsState({ results: [], fetching: false }); + captureException( + new Error('questions is null, meaning the cache is probably corrupted'), + ); + return; + } + setResultsState({ results, fetching: false }); trackEvent({ @@ -119,7 +139,7 @@ export const SearchResults = React.forwardRef(function SearchResults( } }, [query, global, pointer, spaceId, revisionId, withAsk, trackEvent]); - const results = React.useMemo(() => { + const results: ResultType[] = React.useMemo(() => { if (!withAsk) { return resultsState.results; } @@ -130,7 +150,7 @@ export const SearchResults = React.forwardRef(function SearchResults( if (!query) { // Reset the cursor when there's no query setCursor(null); - } else if (results && results.length > 0) { + } else if (results.length > 0) { // Auto-focus the first result setCursor(0); } @@ -150,10 +170,6 @@ export const SearchResults = React.forwardRef(function SearchResults( const moveBy = React.useCallback( (delta: number) => { - if (!results) { - return; - } - setCursor((prev) => { if (prev === null) { return 0; @@ -208,7 +224,9 @@ export const SearchResults = React.forwardRef(function SearchResults(
{children} {results.length === 0 ? ( - noResults + query ? ( + noResults + ) : null ) : ( <>