From 3a894093e2ac8d6230bb4caf140360187ba1e472 Mon Sep 17 00:00:00 2001 From: Marco Salazar Date: Thu, 25 Apr 2024 15:58:07 -0400 Subject: [PATCH] use recoil instead to break free the real current path and specific path --- .../dagster-ui/packages/ui-core/package.json | 1 + .../packages/ui-core/src/app/analytics.tsx | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/js_modules/dagster-ui/packages/ui-core/package.json b/js_modules/dagster-ui/packages/ui-core/package.json index 02d34ef6f190c..d4a10b3dcb97b 100644 --- a/js_modules/dagster-ui/packages/ui-core/package.json +++ b/js_modules/dagster-ui/packages/ui-core/package.json @@ -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", diff --git a/js_modules/dagster-ui/packages/ui-core/src/app/analytics.tsx b/js_modules/dagster-ui/packages/ui-core/src/app/analytics.tsx index 54d06c8652d9e..cc993ecb7d9fc 100644 --- a/js_modules/dagster-ui/packages/ui-core/src/app/analytics.tsx +++ b/js_modules/dagster-ui/packages/ui-core/src/app/analytics.tsx @@ -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) => void; @@ -13,10 +18,7 @@ export const AnalyticsContext = createContext(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 = () => { @@ -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);