diff --git a/src/bundle/ui-dev/src/modules/universal-discovery/bookmarks.tab.module.js b/src/bundle/ui-dev/src/modules/universal-discovery/bookmarks.tab.module.js index da95fbef3f..1aa3779db1 100644 --- a/src/bundle/ui-dev/src/modules/universal-discovery/bookmarks.tab.module.js +++ b/src/bundle/ui-dev/src/modules/universal-discovery/bookmarks.tab.module.js @@ -22,7 +22,8 @@ import { getIconPath } from '@ibexa-admin-ui/src/bundle/Resources/public/js/scri import { getTranslator } from '@ibexa-admin-ui/src/bundle/Resources/public/js/scripts/helpers/context.helper'; const BookmarksTabModule = () => { - const shouldRestorePreviousStateRef = useRef(true); + const isMarkedLocationSetByBookmarksRef = useRef(false); + const restorationStateRef = useRef(null); const restInfo = useContext(RestInfoContext); const tabsConfig = useContext(TabsConfigContext); const [currentView] = useContext(CurrentViewContext); @@ -46,13 +47,24 @@ const BookmarksTabModule = () => { }; useEffect(() => { - setMarkedLocationId(null); - dispatchLoadedLocationsAction({ type: 'CLEAR_LOCATIONS' }); + const isCleared = markedLocationId === null && loadedLocationsMap?.length === 0; + if (!isCleared && !isMarkedLocationSetByBookmarksRef.current) { + restorationStateRef.current = { + markedLocationId, + loadedLocationsMap, + }; + + setMarkedLocationId(null); + dispatchLoadedLocationsAction({ type: 'CLEAR_LOCATIONS' }); + } + }, [setMarkedLocationId, dispatchLoadedLocationsAction, markedLocationId, loadedLocationsMap]); + + useEffect(() => { return () => { - if (shouldRestorePreviousStateRef.current) { - setMarkedLocationId(markedLocationId); - dispatchLoadedLocationsAction({ type: 'SET_LOCATIONS', data: loadedLocationsMap }); + if (!isMarkedLocationSetByBookmarksRef.current) { + setMarkedLocationId(restorationStateRef.current.markedLocationId); + dispatchLoadedLocationsAction({ type: 'SET_LOCATIONS', data: restorationStateRef.current.loadedLocationsMap }); } }; }, []); @@ -62,8 +74,9 @@ const BookmarksTabModule = () => { return; } - shouldRestorePreviousStateRef.current = false; + isMarkedLocationSetByBookmarksRef.current = true; setMarkedLocationId(bookmarkedLocationMarked); + loadAccordionData( { ...restInfo, @@ -90,7 +103,7 @@ const BookmarksTabModule = () => {
- {renderBrowseLocations()} + {isMarkedLocationSetByBookmarksRef.current && renderBrowseLocations()}
); diff --git a/src/bundle/ui-dev/src/modules/universal-discovery/search.tab.module.js b/src/bundle/ui-dev/src/modules/universal-discovery/search.tab.module.js index f61a8b1132..226936e185 100644 --- a/src/bundle/ui-dev/src/modules/universal-discovery/search.tab.module.js +++ b/src/bundle/ui-dev/src/modules/universal-discovery/search.tab.module.js @@ -1,4 +1,4 @@ -import React, { useContext, useEffect } from 'react'; +import React, { useContext, useEffect, useRef } from 'react'; import Tab from './components/tab/tab'; import Search from './components/search/search'; @@ -10,6 +10,7 @@ import { getIconPath } from '@ibexa-admin-ui/src/bundle/Resources/public/js/scri const SearchTabModule = () => { const tabsConfig = useContext(TabsConfigContext); + const restorationStateRef = useRef(null); const [markedLocationId, setMarkedLocationId] = useContext(MarkedLocationIdContext); const [loadedLocationsMap, dispatchLoadedLocationsAction] = useContext(LoadedLocationsMapContext); @@ -19,10 +20,23 @@ const SearchTabModule = () => { 'view-switcher': true, }; + useEffect(() => { + const isCleared = markedLocationId === null && loadedLocationsMap?.length === 0; + + if (!isCleared) { + restorationStateRef.current = { + markedLocationId, + loadedLocationsMap, + }; + } + }, [setMarkedLocationId, dispatchLoadedLocationsAction, markedLocationId, loadedLocationsMap]); + useEffect(() => { return () => { - setMarkedLocationId(markedLocationId); - dispatchLoadedLocationsAction({ type: 'SET_LOCATIONS', data: loadedLocationsMap }); + if (restorationStateRef.current) { + setMarkedLocationId(restorationStateRef.current.markedLocationId); + dispatchLoadedLocationsAction({ type: 'SET_LOCATIONS', data: restorationStateRef.current.loadedLocationsMap }); + } }; }, []);