From 20da40d38de2333446cce8d55f09b03f4c53dea3 Mon Sep 17 00:00:00 2001 From: David Leek Date: Mon, 1 Jul 2024 10:35:02 +0200 Subject: [PATCH] feat: move recording recently visited into separate component (#7494) --- .../src/component/commandBar/CommandBar.tsx | 9 ++---- .../component/commandBar/CommandRecent.tsx | 8 +++-- .../commandBar/RecentlyVisitedRecorder.tsx | 31 +++++++++++++++++++ .../src/hooks/useRecentlyVisited.test.tsx | 18 ++++++++--- frontend/src/hooks/useRecentlyVisited.ts | 24 -------------- 5 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 frontend/src/component/commandBar/RecentlyVisitedRecorder.tsx diff --git a/frontend/src/component/commandBar/CommandBar.tsx b/frontend/src/component/commandBar/CommandBar.tsx index f0d427495f..c8aa7b5ea8 100644 --- a/frontend/src/component/commandBar/CommandBar.tsx +++ b/frontend/src/component/commandBar/CommandBar.tsx @@ -14,7 +14,6 @@ import { useKeyboardShortcut } from 'hooks/useKeyboardShortcut'; import { SEARCH_INPUT } from 'utils/testIds'; import { useOnClickOutside } from 'hooks/useOnClickOutside'; import { useOnBlur } from 'hooks/useOnBlur'; -import { useRecentlyVisited } from 'hooks/useRecentlyVisited'; import { CommandResultGroup, type CommandResultGroupItem, @@ -27,6 +26,7 @@ import { CommandFeatures } from './CommandFeatures'; import { usePlausibleTracker } from 'hooks/usePlausibleTracker'; import { CommandRecent } from './CommandRecent'; import { CommandPages } from './CommandPages'; +import { RecentlyVisitedRecorder } from './RecentlyVisitedRecorder'; export const CommandResultsPaper = styled(Paper)(({ theme }) => ({ position: 'absolute', @@ -110,7 +110,6 @@ export const CommandBar = () => { >([]); const [searchedFlagCount, setSearchedFlagCount] = useState(0); const [value, setValue] = useState(''); - const { lastVisited } = useRecentlyVisited(); const { routes } = useRoutes(); const allRoutes: Record = {}; for (const route of [ @@ -200,6 +199,7 @@ export const CommandBar = () => { return ( + { elseShow={ showSuggestions && ( - + ) diff --git a/frontend/src/component/commandBar/CommandRecent.tsx b/frontend/src/component/commandBar/CommandRecent.tsx index 96977e3e0a..fbcd92269d 100644 --- a/frontend/src/component/commandBar/CommandRecent.tsx +++ b/frontend/src/component/commandBar/CommandRecent.tsx @@ -5,7 +5,10 @@ import { RecentlyVisitedProjectButton, } from './RecentlyVisited/CommandResultGroup'; import { List } from '@mui/material'; -import type { LastViewedPage } from 'hooks/useRecentlyVisited'; +import { + useRecentlyVisited, + type LastViewedPage, +} from 'hooks/useRecentlyVisited'; const toListItemButton = ( item: LastViewedPage, @@ -38,12 +41,11 @@ const toListItemButton = ( }; export const CommandRecent = ({ - lastVisited, routes, }: { - lastVisited: LastViewedPage[]; routes: Record; }) => { + const { lastVisited } = useRecentlyVisited(); const buttons = lastVisited.map((item, index) => toListItemButton(item, routes, index), ); diff --git a/frontend/src/component/commandBar/RecentlyVisitedRecorder.tsx b/frontend/src/component/commandBar/RecentlyVisitedRecorder.tsx new file mode 100644 index 0000000000..5f6dd1f0e8 --- /dev/null +++ b/frontend/src/component/commandBar/RecentlyVisitedRecorder.tsx @@ -0,0 +1,31 @@ +import { useRecentlyVisited } from 'hooks/useRecentlyVisited'; +import { useLocation, useMatch } from 'react-router-dom'; +import { routes } from 'component/menu/routes'; +import { useEffect } from 'react'; + +export const RecentlyVisitedRecorder = () => { + const { setLastVisited } = useRecentlyVisited(); + const featureMatch = useMatch('/projects/:projectId/features/:featureId'); + const projectMatch = useMatch('/projects/:projectId'); + + const location = useLocation(); + + useEffect(() => { + if (!location.pathname) return; + + const path = routes.find((r) => r.path === location.pathname); + if (path) { + setLastVisited({ pathName: path.path }); + } else if (featureMatch?.params.featureId) { + setLastVisited({ + featureId: featureMatch?.params.featureId, + projectId: featureMatch?.params.projectId, + }); + } else if (projectMatch?.params.projectId) { + setLastVisited({ + projectId: projectMatch?.params.projectId, + }); + } + }, [location, featureMatch, projectMatch]); + return <>; +}; diff --git a/frontend/src/hooks/useRecentlyVisited.test.tsx b/frontend/src/hooks/useRecentlyVisited.test.tsx index 685efc60d7..3bdc9ae7b1 100644 --- a/frontend/src/hooks/useRecentlyVisited.test.tsx +++ b/frontend/src/hooks/useRecentlyVisited.test.tsx @@ -9,11 +9,13 @@ import { createMemoryRouter, } from 'react-router-dom'; import { useRecentlyVisited } from './useRecentlyVisited'; +import { RecentlyVisitedRecorder } from 'component/commandBar/RecentlyVisitedRecorder'; const RouteNameRender: FC<{}> = () => { const { lastVisited } = useRecentlyVisited(); return (
+ {lastVisited.map((visited) => (
{visited.pathName}
))} @@ -34,14 +36,22 @@ test('checks that routes that exist in routes.ts gets added to lastVisited', asy } /> } - /> - } /> } + /> + } + /> + } + /> + } /> segment div
} /> diff --git a/frontend/src/hooks/useRecentlyVisited.ts b/frontend/src/hooks/useRecentlyVisited.ts index be0453def8..5a78f1ba4c 100644 --- a/frontend/src/hooks/useRecentlyVisited.ts +++ b/frontend/src/hooks/useRecentlyVisited.ts @@ -1,6 +1,4 @@ import { useCallback, useEffect, useState } from 'react'; -import { useLocation, useMatch } from 'react-router-dom'; -import { routes } from 'component/menu/routes'; import { getLocalStorageItem, setLocalStorageItem } from '../utils/storage'; import { basePath } from 'utils/formatPath'; import { useCustomEvent } from './useCustomEvent'; @@ -20,8 +18,6 @@ const localStorageItems = (key: string): LastViewedPage[] => { export const useRecentlyVisited = () => { const key = `${basePath}:unleash-lastVisitedPages`; - const featureMatch = useMatch('/projects/:projectId/features/:featureId'); - const projectMatch = useMatch('/projects/:projectId'); const [lastVisited, setLastVisited] = useState( localStorageItems(key), @@ -34,26 +30,6 @@ export const useRecentlyVisited = () => { }, ); - const location = useLocation(); - - useEffect(() => { - if (!location.pathname) return; - - const path = routes.find((r) => r.path === location.pathname); - if (path) { - setCappedLastVisited({ pathName: path.path }); - } else if (featureMatch?.params.featureId) { - setCappedLastVisited({ - featureId: featureMatch?.params.featureId, - projectId: featureMatch?.params.projectId, - }); - } else if (projectMatch?.params.projectId) { - setCappedLastVisited({ - projectId: projectMatch?.params.projectId, - }); - } - }, [location, featureMatch, projectMatch]); - useEffect(() => { if (lastVisited) { setLocalStorageItem(key, lastVisited);