feat: enhance deep linking functionality in MainModal and Discover components

This commit is contained in:
alexsparkes
2026-02-07 11:18:57 +00:00
parent 474b4ae237
commit 9e336b0582
4 changed files with 37 additions and 1 deletions

View File

@@ -151,7 +151,9 @@ function MainModal({ modalClose, deepLinkData }) {
historyIndexRef.current = -1;
updateNavButtons();
if (newTab === TAB_TYPES.DISCOVER) {
updateHash(`#${newTab}/all`);
const section = deepLinkData?.category || deepLinkData?.section || 'all';
const itemId = deepLinkData?.itemId ? `/${deepLinkData.itemId}` : '';
updateHash(`#${newTab}/${section}${itemId}`);
} else if (newTab === TAB_TYPES.LIBRARY) {
updateHash(`#${newTab}/added`);
} else {

View File

@@ -30,6 +30,15 @@ const Tabs = ({
};
}
}
if (deepLinkData?.category && sections) {
const section = sections.find((s) => s.name === deepLinkData.category);
if (section) {
return {
label: t(section.label),
name: section.name,
};
}
}
return {
label: children[0]?.props.label,
name: children[0]?.props.name,
@@ -78,6 +87,22 @@ const Tabs = ({
}
}, []);
// React to deep link changes (e.g., when navigating to a suggested pack from settings)
useEffect(() => {
if (deepLinkData && sections) {
const targetSection = deepLinkData.section || deepLinkData.category;
if (targetSection) {
const section = sections.find((s) => s.name === targetSection);
if (section && section.name !== currentName) {
setCurrentName(section.name);
if (contentRef.current) {
contentRef.current.scrollTop = 0;
}
}
}
}
}, [deepLinkData, sections, currentName]);
// useLayoutEffect is appropriate here for synchronous state updates before paint
useLayoutEffect(() => {
if (navigationTrigger?.type === 'settings-section' && sections) {

View File

@@ -108,10 +108,17 @@ const Modals = () => {
}
};
const handleHashChange = () => {
const linkData = parseDeepLink();
setDeepLinkData(linkData);
};
EventBus.on('modal', handleModalOpen);
window.addEventListener('popstate', handleHashChange);
return () => {
EventBus.off('modal', handleModalOpen);
window.removeEventListener('popstate', handleHashChange);
};
}, []);

View File

@@ -342,6 +342,8 @@ function Discover(props) {
current="discover"
currentTab={props.currentTab}
onSectionChange={props.onSectionChange}
sections={sections}
deepLinkData={props.deepLinkData}
>
{sections.map(({ label, name }) => (
<div key={name} label={t(label)} name={name}>