From 1680c53279bac05e9354033691176f0d863409ea Mon Sep 17 00:00:00 2001 From: kkfluous Date: Thu, 2 Apr 2026 18:05:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20URL=20path=20?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E8=BF=9B=E5=85=A5=E4=B8=8D=E5=90=8C=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /vehicle 或 /assets → 资产管理 /mileage → 里程管理 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/components/Shell.tsx | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/components/Shell.tsx b/src/components/Shell.tsx index 09d5dc1..8d952df 100644 --- a/src/components/Shell.tsx +++ b/src/components/Shell.tsx @@ -7,25 +7,47 @@ export interface ModuleConfig { component: ComponentType; } +/** path 到模块 id 的映射 */ +const PATH_MAP: Record = { + '/vehicle': 'assets', + '/assets': 'assets', + '/mileage': 'mileage', +}; + +function getInitialModule(modules: ModuleConfig[]): string { + // 优先看 hash + const hash = window.location.hash.slice(1); + if (modules.some((m) => m.id === hash)) return hash; + // 再看 pathname + const pathModule = PATH_MAP[window.location.pathname]; + if (pathModule && modules.some((m) => m.id === pathModule)) return pathModule; + // 默认第一个 + return modules[0]?.id ?? ''; +} + function getHashModule(modules: ModuleConfig[]): string { const hash = window.location.hash.slice(1); - return modules.some((m) => m.id === hash) ? hash : modules[0]?.id ?? ''; + return modules.some((m) => m.id === hash) ? hash : ''; } export function Shell({ modules }: { modules: ModuleConfig[] }) { - const [activeModule, setActiveModule] = useState(() => getHashModule(modules)); + const [activeModule, setActiveModule] = useState(() => getInitialModule(modules)); useEffect(() => { - const onHashChange = () => setActiveModule(getHashModule(modules)); + const onHashChange = () => { + const h = getHashModule(modules); + if (h) setActiveModule(h); + }; window.addEventListener('hashchange', onHashChange); return () => window.removeEventListener('hashchange', onHashChange); }, [modules]); useEffect(() => { - if (!window.location.hash) { - window.location.hash = modules[0]?.id ?? ''; + // 同步 hash 到当前模块 + if (window.location.hash.slice(1) !== activeModule) { + window.location.hash = activeModule; } - }, [modules]); + }, [activeModule]); const switchModule = (id: string) => { window.location.hash = id;