fix(spa-nav): refresh navigation immediately when modules are toggled

This commit is contained in:
Dwindi Ramadhana
2026-06-01 00:58:18 +07:00
parent 5b8882e595
commit 30f2fc2ea6
5 changed files with 59 additions and 17 deletions

View File

@@ -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;
}
}