Skip to content

Commit

Permalink
Cache wasInteracted state
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Jan 6, 2023
1 parent 0ce0973 commit 008342d
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions searchlib/components/SearchView/SearchView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ import { SEARCH_STATE_IDS } from '@eeacms/search/constants';
import { useAtom } from 'jotai';
import { isLandingPageAtom } from './state';

const useWasInteracted = ({ searchedTerm, searchContext, appConfig }) => {
// a check that, once toggled true, it always return true

const [cached, setCached] = React.useState();

const wasInteracted = !!(
searchedTerm ||
checkInteracted({
searchContext,
appConfig,
})
);

React.useEffect(() => {
if (wasInteracted && !cached) setCached(true);
}, [wasInteracted, cached]);

return cached || wasInteracted;
};

export const SearchView = (props) => {
const { appConfig, appName, mode = 'view' } = props;

Expand All @@ -34,13 +54,11 @@ export const SearchView = (props) => {
? driver.URLManager.getStateFromURL().searchTerm
: null;

const wasInteracted = !!(
searchedTerm ||
checkInteracted({
searchContext,
appConfig,
})
);
const wasInteracted = useWasInteracted({
searchedTerm,
searchContext,
appConfig,
});

React.useEffect(() => {
window.searchContext = searchContext;
Expand Down

0 comments on commit 008342d

Please sign in to comment.