From b200e939689d57417ccac19d4e532654192edf83 Mon Sep 17 00:00:00 2001 From: Bill Date: Thu, 29 Jul 2021 02:03:30 -0400 Subject: [PATCH] Catalog remember last opened page --- src/views/catalog/CatalogView.tsx | 36 +++++++++++++++---- .../navigation/CatalogNavigationView.tsx | 6 ++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/views/catalog/CatalogView.tsx b/src/views/catalog/CatalogView.tsx index 06d5e17d..59dd05cb 100644 --- a/src/views/catalog/CatalogView.tsx +++ b/src/views/catalog/CatalogView.tsx @@ -11,7 +11,7 @@ import { CatalogMode, CatalogViewProps } from './CatalogView.types'; import { BuildCatalogPageTree } from './common/CatalogUtilities'; import { CatalogContextProvider } from './context/CatalogContext'; import { CatalogReducer, initialCatalog } from './reducers/CatalogReducer'; -import { CatalogNavigationView } from './views/navigation/CatalogNavigationView'; +import { ACTIVE_PAGES, CatalogNavigationView } from './views/navigation/CatalogNavigationView'; import { CatalogPageView } from './views/page/CatalogPageView'; export const CatalogView: FC = props => @@ -20,25 +20,37 @@ export const CatalogView: FC = props => const [ roomPreviewer, setRoomPreviewer ] = useState(null); const [ pendingPageLookup, setPendingPageLookup ] = useState<{ value: string, isOffer: boolean }>(null); const [ pendingTree, setPendingTree ] = useState(null); + const [ pendingOpenTree, setPendingOpenTree ] = useState(null); const [ catalogState, dispatchCatalogState ] = useReducer(CatalogReducer, initialCatalog); const [ currentTab, setCurrentTab ] = useState(null); const { root = null, pageParser = null, activeOffer = null, searchResult = null} = catalogState; + const saveActivePages = useCallback(() => + { + setPendingOpenTree(ACTIVE_PAGES.slice()); + }, []); + const onCatalogEvent = useCallback((event: CatalogEvent) => { + let save = false; + switch(event.type) { case CatalogEvent.SHOW_CATALOG: setIsVisible(true); return; case CatalogEvent.HIDE_CATALOG: + save = true; setIsVisible(false); return; case CatalogEvent.TOGGLE_CATALOG: + save = true; setIsVisible(value => !value); return; } - }, []); + + if(save) saveActivePages(); + }, [ saveActivePages ]); useUiEvent(CatalogEvent.SHOW_CATALOG, onCatalogEvent); useUiEvent(CatalogEvent.HIDE_CATALOG, onCatalogEvent); @@ -88,7 +100,7 @@ export const CatalogView: FC = props => AddEventLinkTracker(linkTracker); return () => RemoveLinkEventTracker(linkTracker); - }, [ linkReceived]); + }, [ linkReceived ]); useEffect(() => { @@ -111,11 +123,21 @@ export const CatalogView: FC = props => return; } - if(pendingPageLookup !== null) + if(pendingPageLookup !== null || pendingOpenTree) { - const tree = BuildCatalogPageTree(catalogState.root, pendingPageLookup.value, pendingPageLookup.isOffer); + let tree: ICatalogPageData[] = []; + + if(pendingPageLookup !== null) + { + tree = BuildCatalogPageTree(catalogState.root, pendingPageLookup.value, pendingPageLookup.isOffer); + } + else + { + tree = pendingOpenTree.slice(); + } setCurrentTab(tree.shift()); + setPendingOpenTree(null); setPendingPageLookup(null); setPendingTree(tree); } @@ -137,7 +159,7 @@ export const CatalogView: FC = props => }); } } - }, [ isVisible, pendingPageLookup, catalogState.root, setCurrentTab ]); + }, [ isVisible, pendingPageLookup, pendingOpenTree, catalogState.root, setCurrentTab ]); useEffect(() => { @@ -169,7 +191,7 @@ export const CatalogView: FC = props => { isVisible && - setIsVisible(false) } /> + { saveActivePages(); setIsVisible(false); } } /> { root && root.children.length && root.children.map((page, index) => { diff --git a/src/views/catalog/views/navigation/CatalogNavigationView.tsx b/src/views/catalog/views/navigation/CatalogNavigationView.tsx index d185f223..98c1bef3 100644 --- a/src/views/catalog/views/navigation/CatalogNavigationView.tsx +++ b/src/views/catalog/views/navigation/CatalogNavigationView.tsx @@ -4,7 +4,7 @@ import { CatalogSearchView } from '../search/CatalogSearchView'; import { CatalogNavigationViewProps } from './CatalogNavigationView.types'; import { CatalogNavigationSetView } from './set/CatalogNavigationSetView'; -export const ACTIVE_PAGES: ICatalogPageData[] = []; +export let ACTIVE_PAGES: ICatalogPageData[] = []; export const CatalogNavigationView: FC = props => { @@ -14,11 +14,11 @@ export const CatalogNavigationView: FC = props => { if(!page) return; - const index = (ACTIVE_PAGES.push(page) - 1); + ACTIVE_PAGES = [ page ]; return () => { - ACTIVE_PAGES.splice(index, 1); + ACTIVE_PAGES = []; } }, [ page ]);