-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Make usePageContext
return the correct path no matter where its called
#21433
Conversation
Deploy preview for dagit-core-storybook ready! ✅ Preview Built with commit c39367f. |
ff073fa
to
3a89409
Compare
analytics.page
useTrackPageView
useTrackPageView
usePageContext
to work anywhere and return the correct path
usePageContext
to work anywhere and return the correct pathusePageContext
return the correct path no matter where its called
import {useLocation, useRouteMatch} from 'react-router-dom'; | ||
import {atom, useRecoilValue, useSetRecoilState} from 'recoil'; | ||
|
||
export const currentPageAtom = atom<{path: string; specificPath: string}>({ |
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 used a recoil atom here to avoid having yet another provider and having to order them properly (which in some cases necessitates another provider yay). Cloud already uses Recoil
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'm not quite sure I follow how Recoil avoids another provider / ordering problem, this PR adds a RecoilProvider?
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.
The Recoil provider is at the root, that's pretty much the extent of ordering that we need to think about.
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.
Lotta React here to keep the useRouteMatch().path
in global state, but I think Recoil is a nice way to have a react-friendly "pile of global stuff"
import {useLocation, useRouteMatch} from 'react-router-dom'; | ||
import {atom, useRecoilValue, useSetRecoilState} from 'recoil'; | ||
|
||
export const currentPageAtom = atom<{path: string; specificPath: string}>({ |
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'm not quite sure I follow how Recoil avoids another provider / ordering problem, this PR adds a RecoilProvider?
// Wait briefly to allow redirects. | ||
const timer = setTimeout(() => { | ||
analytics.page(path, specificPath); | ||
}, PAGEVIEW_DELAY); | ||
setCurrentPage({path, specificPath}); |
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.
This all looks good and I think I'm in favor of the formal React solution. That said, an alternative two line version of this PR would be to set a hidden window.__routerPath = path
here, and then have usePageContext
return {path: window.__routerPath, specificPath: window.location.path}
.
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.
This wouldn't be equivalent because changing __routerPath
wouldn't let anyone relying on the path know that it changed and that's key for page load logging.
…led (dagster-io#21433) ## Summary & Motivation To log page load we need to inform datadog when a new view starts. To do this we currently rely on `usePageContext` to change when the path changes. Currently it doesn't do that because `usePageContext` relies on `useRouteMatch` from react-router-dom which finds the path from the closest parent `Route`. The issue is that we're calling `usePageContext` from a provider that is very high up in the tree, so `useRouteMatch` can't access the context of the `<Route>` currently being rendered. Instead of using `useRouteMatch` lets store the path in global state when `useTrackPageView` is called and have `usePageContext` rely on that global state. Cloud requires this change to dedupe recoil: https://github.com/dagster-io/internal/pull/9471 ## How I Tested These Changes 👀 , play around with the app make sure nothing is broken
Summary & Motivation
To log page load we need to inform datadog when a new view starts. To do this we currently rely on
usePageContext
to change when the path changes. Currently it doesn't do that becauseusePageContext
relies onuseRouteMatch
from react-router-dom which finds the path from the closest parentRoute
. The issue is that we're callingusePageContext
from a provider that is very high up in the tree, souseRouteMatch
can't access the context of the<Route>
currently being rendered. Instead of usinguseRouteMatch
lets store the path in global state whenuseTrackPageView
is called and haveusePageContext
rely on that global state.Cloud requires this change to dedupe recoil: https://github.com/dagster-io/internal/pull/9471
How I Tested These Changes
👀 , play around with the app make sure nothing is broken