Skip to content
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

Merged
merged 8 commits into from
Apr 26, 2024

Conversation

salazarm
Copy link
Contributor

@salazarm salazarm commented Apr 25, 2024

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

Copy link

github-actions bot commented Apr 25, 2024

Deploy preview for dagit-core-storybook ready!

✅ Preview
https://dagit-core-storybook-pae8lvk3c-elementl.vercel.app
https://salazarm-remove-timeout.core-storybook.dagster-docs.io

Built with commit c39367f.
This pull request is being automatically deployed with vercel-action

@salazarm salazarm requested a review from prha April 25, 2024 19:32
@salazarm salazarm force-pushed the salazarm/remove-timeout branch from ff073fa to 3a89409 Compare April 25, 2024 19:58
@salazarm salazarm changed the title Move analytics timeout out of call site to analytics.page Jailbreak the "currentPath" out of useTrackPageView Apr 25, 2024
@salazarm salazarm changed the title Jailbreak the "currentPath" out of useTrackPageView Fix usePageContext to work anywhere and return the correct path Apr 25, 2024
@salazarm salazarm changed the title Fix usePageContext to work anywhere and return the correct path Make usePageContext return the correct path no matter where its called Apr 26, 2024
import {useLocation, useRouteMatch} from 'react-router-dom';
import {atom, useRecoilValue, useSetRecoilState} from 'recoil';

export const currentPageAtom = atom<{path: string; specificPath: string}>({
Copy link
Contributor Author

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

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

@bengotow bengotow left a 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}>({
Copy link
Collaborator

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});
Copy link
Collaborator

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}.

Copy link
Contributor Author

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.

@salazarm salazarm merged commit 8247864 into master Apr 26, 2024
2 checks passed
@salazarm salazarm deleted the salazarm/remove-timeout branch April 26, 2024 16:38
nikomancy pushed a commit to nikomancy/dagster that referenced this pull request May 1, 2024
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants