fix(spa-nav): refresh navigation immediately when modules are toggled
This commit is contained in:
@@ -1,8 +1,23 @@
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { navTree, MainNode, NAV_TREE_VERSION } from '../nav/tree';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { navTree as initialNavTree, MainNode } from '../nav/tree';
|
||||
|
||||
function getCurrentNavTree(): MainNode[] {
|
||||
const tree = window.WNW_NAV_TREE;
|
||||
return Array.isArray(tree) && tree.length > 0
|
||||
? (tree as MainNode[])
|
||||
: initialNavTree;
|
||||
}
|
||||
|
||||
export function useActiveSection(): { main: MainNode; all: MainNode[] } {
|
||||
const { pathname } = useLocation();
|
||||
const [navTree, setNavTree] = useState<MainNode[]>(getCurrentNavTree);
|
||||
|
||||
useEffect(() => {
|
||||
const handleNavUpdate = () => setNavTree(getCurrentNavTree());
|
||||
window.addEventListener('woonoow:navigation-updated', handleNavUpdate);
|
||||
return () => window.removeEventListener('woonoow:navigation-updated', handleNavUpdate);
|
||||
}, []);
|
||||
|
||||
function pick(): MainNode {
|
||||
// Special case: /settings should match settings section
|
||||
@@ -32,8 +47,5 @@ export function useActiveSection(): { main: MainNode; all: MainNode[] } {
|
||||
const main = pick();
|
||||
const children = Array.isArray(main.children) ? main.children : [];
|
||||
|
||||
// Debug: ensure we are using the latest tree module (driven by PHP-localized window.wnw.isDev)
|
||||
const isDev = Boolean((window as any).wnw?.isDev);
|
||||
|
||||
return { main: { ...main, children }, all: navTree } as const;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user