-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat(recommend): add status state (loading, stalled, idle) #43
Conversation
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.
Code looks good, but I think we could have a useStatus factory to share between react & preact
@@ -0,0 +1,23 @@ | |||
import { RecommendStatus } from '@algolia/recommend-vdom'; | |||
import { useEffect, useRef, useState } from 'react'; |
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.
could we make a factory where useEffect, useRef, useState as passed to the hook creator, so we can share the implementation?
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.
Where would you see this implementation? If we have this duplication more frequently in the future, we could have a recommend-hooks
package but right now it doesn't fit in VDOM
or core
IMO
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.
it could fit in vdom, seeing as only react-like api lives there at the moment. Whenever we would add Vue, I guess a different api than purely hooks would be needed, but not yet atm
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 think it's a bit too soon to abstract this, but we should definitely define a strategy if these duplications need to grow.
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.
Looks clean!
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.
let's keep it in mind for the future
The problem
There was an issue where we rendered the
fallbackComponent
during first render because we checkedprops.recommendations.length === 0
, which is the case before sending the request. Thus, when usingRelatedProducts
infallbackComponent
, it sent another request, which was unnecessary.This solution
This PR adds the loading status of the component in the API, and uses it to render the fallback component when there's no recommendations and that the status is
idle
.Possible statuses are
idle
,loading
andstalled
. The stalled status is useful to render product placeholders afterx
ms, to reduce UI flashes as opposed to rendering it whenloading
.In the future, we might want to expose the stalled threshold value to the user, so that they can synchronize their placeholder content with other pieces on their website.
Considerations
Notice that the
useStatus
hook in Preact and in React have an identical implementation, which is a bummer. I don't think we can use the same code here without additional tooling.