Skip to content

Commit

Permalink
use recoil instead to break free the real current path and specific path
Browse files Browse the repository at this point in the history
  • Loading branch information
salazarm committed Apr 25, 2024
1 parent bd9ba7c commit 3a89409
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions js_modules/dagster-ui/packages/ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"react-is": "^18.2.0",
"react-markdown": "^8.0.6",
"react-virtualized": "^9.22.3",
"recoil": "^0.7.7",
"rehype-highlight": "^6.0.0",
"rehype-sanitize": "^5.0.1",
"remark": "^14.0.2",
Expand Down
17 changes: 11 additions & 6 deletions js_modules/dagster-ui/packages/ui-core/src/app/analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {createContext, useCallback, useContext, useEffect, useMemo} from 'react';
import {useLocation, useRouteMatch} from 'react-router-dom';
import {createContext, useCallback, useContext, useEffect} from 'react';
import {atom, useRecoilValue, useSetRecoilState} from 'recoil';

export const currentPageAtom = atom<{path: string; specificPath: string}>({
key: 'currentPageAtom',
default: {path: '/', specificPath: '/'},
});

export interface GenericAnalytics {
group?: (groupId: string, traits?: Record<string, any>) => void;
Expand All @@ -13,10 +18,7 @@ export const AnalyticsContext = createContext<GenericAnalytics>(undefined!);
const PAGEVIEW_DELAY = 300;

export const usePageContext = () => {
const match = useRouteMatch();
const {pathname: specificPath} = useLocation();
const {path} = match;
return useMemo(() => ({path, specificPath}), [path, specificPath]);
return useRecoilValue(currentPageAtom);
};

const useAnalytics = () => {
Expand Down Expand Up @@ -54,11 +56,14 @@ export const useTrackPageView = () => {
const analytics = useAnalytics();
const {path, specificPath} = usePageContext();

const setCurrentPage = useSetRecoilState(currentPageAtom);

useEffect(() => {
// Wait briefly to allow redirects.
const timer = setTimeout(() => {
analytics.page(path, specificPath);
}, PAGEVIEW_DELAY);
setCurrentPage({path, specificPath});

return () => {
clearTimeout(timer);
Expand Down

0 comments on commit 3a89409

Please sign in to comment.