Skip to content

Commit f4c07d7

Browse files
mkouzel-yextgithub-actions[bot]Copilot
authored
Deduplicate generative direct answer calls (#554)
The effect hook that triggers a GDA call has two dependencies: the search results and the search ID. These both change when a search is made. In React 18, these updates are batched together so only one call to GDA gets made when the search is updated. However, in React 17, no batching is performed and updates are handled in order, meaning a call to GDA is made when the search results are updated and when the search ID is updated, even if the same search triggers both updates. This change tracks the search results and only executes a new GDA call if the search results have changed. J=WAT-4861 TEST=manual Created a local test site that runs on React 17. Made a local tarball with my changes to search-ui-react using npm pack and pointed the local test site's import of search-ui-react to the tarball. Confirmed that only one GDA call is made per-seasrch. Also spun up the search-ui-react test-site (which runs on React 18) and confirmed that GDA calls are still made once per search as expected. --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7c5e040 commit f4c07d7

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed
5.21 KB
Loading

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yext/search-ui-react",
3-
"version": "1.9.3",
3+
"version": "1.9.4",
44
"description": "A library of React Components for powering Yext Search integrations",
55
"author": "watson@yext.com",
66
"license": "BSD-3-Clause",

src/components/GenerativeDirectAnswer.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useCardAnalytics } from '../hooks/useCardAnalytics';
1111
import { DefaultRawDataType } from '../models/index';
1212
import { executeGenerativeDirectAnswer } from '../utils/search-operations';
1313
import { Markdown, MarkdownCssClasses } from './Markdown';
14-
import React, { useMemo } from 'react';
14+
import React, {useMemo, useRef} from 'react';
1515

1616
/**
1717
* The CSS class interface used for {@link GenerativeDirectAnswer}.
@@ -92,16 +92,18 @@ export function GenerativeDirectAnswer({
9292
}
9393
}, [isUniversal, universalResults, verticalResults]);
9494

95+
const lastExecutedSearchResults = useRef(undefined as Result[] | undefined);
9596
const searchActions = useSearchActions();
9697
const gdaResponse = useSearchState(state => state.generativeDirectAnswer?.response);
9798
const isLoading = useSearchState(state => state.generativeDirectAnswer?.isLoading);
9899
const handleClickEvent = useReportClickEvent();
99100

100101
React.useEffect(() => {
101-
if (!searchResults?.length || !searchId) {
102+
if (!searchResults?.length || !searchId || searchResults === lastExecutedSearchResults.current) {
102103
return;
103104
}
104105
executeGenerativeDirectAnswer(searchActions);
106+
lastExecutedSearchResults.current = searchResults;
105107
}, [searchResults, searchId]);
106108

107109
if (!searchResults?.length || isLoading || !gdaResponse || gdaResponse.resultStatus !== 'SUCCESS') {
@@ -152,7 +154,7 @@ function Answer(props: AnswerProps) {
152154
}),
153155
[cssClasses.answerText]
154156
);
155-
157+
156158
return <>
157159
<div className={cssClasses.header}>
158160
{answerHeader ?? t('aiGeneratedAnswer')}

0 commit comments

Comments
 (0)