Skip to content

Commit

Permalink
IBX-7825: UDW Blank browse tab after going from bookmarks from search (
Browse files Browse the repository at this point in the history
  • Loading branch information
tischsoic authored Mar 6, 2024
1 parent ea0e96a commit f62c849
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 });
}
};
}, []);
Expand All @@ -62,8 +74,9 @@ const BookmarksTabModule = () => {
return;
}

shouldRestorePreviousStateRef.current = false;
isMarkedLocationSetByBookmarksRef.current = true;
setMarkedLocationId(bookmarkedLocationMarked);

loadAccordionData(
{
...restInfo,
Expand All @@ -90,7 +103,7 @@ const BookmarksTabModule = () => {
<div className="m-bookmarks-tab">
<Tab>
<BookmarksList itemsPerPage={tabsConfig.bookmarks.itemsPerPage} setBookmarkedLocationMarked={setBookmarkedLocationMarked} />
{renderBrowseLocations()}
{isMarkedLocationSetByBookmarksRef.current && renderBrowseLocations()}
</Tab>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand All @@ -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 });
}
};
}, []);

Expand Down

0 comments on commit f62c849

Please sign in to comment.