mirror of
https://github.com/mue/mue.git
synced 2026-07-22 08:17:28 +02:00
feat: implement navigation history management in MainModal
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Suspense, lazy, useState, memo, useEffect } from 'react';
|
import { Suspense, lazy, useState, memo, useEffect, useRef } from 'react';
|
||||||
import { useT } from 'contexts';
|
import { useT } from 'contexts';
|
||||||
|
|
||||||
import './scss/index.scss';
|
import './scss/index.scss';
|
||||||
@@ -29,6 +29,16 @@ function MainModal({ modalClose, deepLinkData }) {
|
|||||||
const [navigationTrigger, setNavigationTrigger] = useState(null);
|
const [navigationTrigger, setNavigationTrigger] = useState(null);
|
||||||
const [iframeBreadcrumbs, setIframeBreadcrumbs] = useState([]);
|
const [iframeBreadcrumbs, setIframeBreadcrumbs] = useState([]);
|
||||||
|
|
||||||
|
const historyRef = useRef([]);
|
||||||
|
const historyIndexRef = useRef(-1);
|
||||||
|
const [canGoBack, setCanGoBack] = useState(false);
|
||||||
|
const [canGoForward, setCanGoForward] = useState(false);
|
||||||
|
|
||||||
|
const updateNavButtons = () => {
|
||||||
|
setCanGoBack(historyIndexRef.current > 0);
|
||||||
|
setCanGoForward(historyIndexRef.current < historyRef.current.length - 1);
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setProductView(null);
|
setProductView(null);
|
||||||
}, [currentTab]);
|
}, [currentTab]);
|
||||||
@@ -51,6 +61,12 @@ function MainModal({ modalClose, deepLinkData }) {
|
|||||||
`#${deepLinkData.tab}/${deepLinkData.section}/${deepLinkData.subSection}`,
|
`#${deepLinkData.tab}/${deepLinkData.section}/${deepLinkData.subSection}`,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
if (historyIndexRef.current >= 0) {
|
||||||
|
historyRef.current[historyIndexRef.current] = {
|
||||||
|
...historyRef.current[historyIndexRef.current],
|
||||||
|
subSection: deepLinkData.subSection,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,6 +147,9 @@ function MainModal({ modalClose, deepLinkData }) {
|
|||||||
|
|
||||||
const handleChangeTab = (newTab) => {
|
const handleChangeTab = (newTab) => {
|
||||||
setCurrentTab(newTab);
|
setCurrentTab(newTab);
|
||||||
|
historyRef.current = [];
|
||||||
|
historyIndexRef.current = -1;
|
||||||
|
updateNavButtons();
|
||||||
if (newTab === TAB_TYPES.DISCOVER) {
|
if (newTab === TAB_TYPES.DISCOVER) {
|
||||||
updateHash(`#${newTab}/all`);
|
updateHash(`#${newTab}/all`);
|
||||||
} else if (newTab === TAB_TYPES.LIBRARY) {
|
} else if (newTab === TAB_TYPES.LIBRARY) {
|
||||||
@@ -144,6 +163,13 @@ function MainModal({ modalClose, deepLinkData }) {
|
|||||||
setCurrentSection(section);
|
setCurrentSection(section);
|
||||||
setCurrentSectionName(sectionName);
|
setCurrentSectionName(sectionName);
|
||||||
setCurrentSubSection(null);
|
setCurrentSubSection(null);
|
||||||
|
const entry = { section, sectionName, subSection: null };
|
||||||
|
const current = historyRef.current[historyIndexRef.current];
|
||||||
|
if (!current || current.sectionName !== sectionName || current.subSection !== null) {
|
||||||
|
historyRef.current = historyRef.current.slice(0, historyIndexRef.current + 1).concat(entry);
|
||||||
|
historyIndexRef.current = historyRef.current.length - 1;
|
||||||
|
updateNavButtons();
|
||||||
|
}
|
||||||
if (currentTab === TAB_TYPES.DISCOVER) {
|
if (currentTab === TAB_TYPES.DISCOVER) {
|
||||||
const sectionMap = {
|
const sectionMap = {
|
||||||
[t('modals.main.marketplace.all')]: 'all',
|
[t('modals.main.marketplace.all')]: 'all',
|
||||||
@@ -163,6 +189,18 @@ function MainModal({ modalClose, deepLinkData }) {
|
|||||||
|
|
||||||
const handleSubSectionChange = (subSection, sectionName) => {
|
const handleSubSectionChange = (subSection, sectionName) => {
|
||||||
setCurrentSubSection(subSection);
|
setCurrentSubSection(subSection);
|
||||||
|
const effectiveSectionName = sectionName || currentSectionName;
|
||||||
|
const entry = { section: currentSection, sectionName: effectiveSectionName, subSection };
|
||||||
|
const current = historyRef.current[historyIndexRef.current];
|
||||||
|
if (
|
||||||
|
!current ||
|
||||||
|
current.sectionName !== effectiveSectionName ||
|
||||||
|
current.subSection !== subSection
|
||||||
|
) {
|
||||||
|
historyRef.current = historyRef.current.slice(0, historyIndexRef.current + 1).concat(entry);
|
||||||
|
historyIndexRef.current = historyRef.current.length - 1;
|
||||||
|
updateNavButtons();
|
||||||
|
}
|
||||||
if (currentTab === TAB_TYPES.SETTINGS && sectionName) {
|
if (currentTab === TAB_TYPES.SETTINGS && sectionName) {
|
||||||
if (subSection) {
|
if (subSection) {
|
||||||
updateHash(`#${currentTab}/${sectionName}/${subSection}`);
|
updateHash(`#${currentTab}/${sectionName}/${subSection}`);
|
||||||
@@ -181,18 +219,39 @@ function MainModal({ modalClose, deepLinkData }) {
|
|||||||
setTimeout(() => setResetDiscoverToAll(false), 100);
|
setTimeout(() => setResetDiscoverToAll(false), 100);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const restoreHistoryEntry = (entry) => {
|
||||||
|
setCurrentSubSection(entry.subSection);
|
||||||
|
if (entry.sectionName !== currentSectionName) {
|
||||||
|
setCurrentSection(entry.section);
|
||||||
|
setCurrentSectionName(entry.sectionName);
|
||||||
|
setNavigationTrigger({
|
||||||
|
type: 'settings-section',
|
||||||
|
data: entry.sectionName,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (currentTab === TAB_TYPES.SETTINGS) {
|
||||||
|
const hash = entry.subSection
|
||||||
|
? `#${currentTab}/${entry.sectionName}/${entry.subSection}`
|
||||||
|
: `#${currentTab}/${entry.sectionName}`;
|
||||||
|
window.history.replaceState(null, null, hash);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleBack = () => {
|
const handleBack = () => {
|
||||||
setIframeBreadcrumbs([]);
|
if (historyIndexRef.current <= 0) return;
|
||||||
window.history.back();
|
historyIndexRef.current -= 1;
|
||||||
|
updateNavButtons();
|
||||||
|
restoreHistoryEntry(historyRef.current[historyIndexRef.current]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleForward = () => {
|
const handleForward = () => {
|
||||||
window.history.forward();
|
if (historyIndexRef.current >= historyRef.current.length - 1) return;
|
||||||
|
historyIndexRef.current += 1;
|
||||||
|
updateNavButtons();
|
||||||
|
restoreHistoryEntry(historyRef.current[historyIndexRef.current]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const canGoBack = true;
|
|
||||||
const canGoForward = true;
|
|
||||||
|
|
||||||
const TabComponent = TAB_COMPONENTS[currentTab] || Settings;
|
const TabComponent = TAB_COMPONENTS[currentTab] || Settings;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user