-
Notifications
You must be signed in to change notification settings - Fork 3
[CI-3436] Autocomplete UI - Add an option to call zero state requests on focus #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
115c037
70e6889
6f239f5
d7c57c6
1229bea
c1110c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ import ConstructorIO from '@constructor-io/constructorio-client-javascript'; | |
| import { Section } from '../types'; | ||
| import { isRecommendationsSection } from '../typeGuards'; | ||
|
|
||
| const viewedRecommendations = new Set(); | ||
|
|
||
| /** | ||
| * Custom hook that observes the visibility of recommendation sections and calls trackRecommendationView event. | ||
| * This is done by using the IntersectionObserver API to observe the visibility of each recommendation section. | ||
|
|
@@ -41,8 +43,9 @@ function useRecommendationsObserver( | |
| const observer = new IntersectionObserver((entries) => { | ||
| // For each section, check if it's intersecting | ||
| entries.forEach((entry) => { | ||
| if (entry.isIntersecting) { | ||
| if (entry.isIntersecting && !viewedRecommendations.has(entry.target)) { | ||
| trackRecommendationView(entry.target as HTMLElement, sections, constructorIO); | ||
| viewedRecommendations.add(entry.target); | ||
| } | ||
| }); | ||
| }, intersectionObserverOptions); | ||
|
|
@@ -63,6 +66,13 @@ function useRecommendationsObserver( | |
| }); | ||
| }; | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [menuIsOpen, sections]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a race condition where Adding sections as dependency fixed that but caused multiple |
||
|
|
||
| // clear viewed recommendations when menu is closed | ||
| useEffect(() => { | ||
| if (!menuIsOpen) { | ||
| viewedRecommendations.clear(); | ||
| } | ||
| }, [menuIsOpen]); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moving function outside useEffect so it can be exported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this! What do you think about moving it outside of useFetchRecommendationPod into utils file. So it can be imported in useCioAutocomplete without having to keep exposing it up across different hooks?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lordvkrum I checked your POC branch, and I thought of not calling fetchRecommendationResults inside the onFocus callback because of missing dependencies. And depend on the useEffect here to run an effect when the input is focused but there was still complexity with the dependencies. Because of that it's better that we move forward with your current implementation for now.