App.tsx
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { useEffect, useMemo, useState } from 'react';
import { busEvents } from '@uj/contracts';
import { SubAppProvider, readInitialPage, shortName, subappBus } from '@uj/platform-sdk';
import { UEmpty } from '@uj/ui';
import Dashboard from './pages/Dashboard';
import Leads from './pages/Leads';
import LeadDetail from './pages/Leads/LeadDetail';
import Assessment from './pages/Assessment';
import AssessmentDetail from './pages/Assessment/AssessmentDetail';
import Operation from './pages/Operation';
import Vehicles from './pages/Vehicles';
import Delivery from './pages/Delivery';
import Ledger from './pages/Ledger';
import Handover from './pages/Handover';
import Reports from './pages/Reports';
import Condition from './pages/Condition';
import CustomerGroup from './pages/CustomerGroup';
import Payment from './pages/Payment';
import type { PageParams, RouteDef } from './routes';
import { matchRoute } from './routes';
/**
* 页面表:主壳给的 subPath(形如 dashboard)在这里换成组件与路径参数。
* 12 条与 scripts/usedcar-menu-complete.sql 中 menu_type=2 的 route 末段逐一对应,
* 顺序沿用菜单 sort(1..12);新增页面 = 加一行 + 建一个页面目录。
*/
const ROUTES: RouteDef[] = [
{ path: 'dashboard', Page: Dashboard },
{ path: 'leads', Page: Leads },
{ path: 'leads/:id', Page: LeadDetail },
{ path: 'assessment', Page: Assessment },
{ path: 'assessment/:evalOrderNo', Page: AssessmentDetail },
{ path: 'operation', Page: Operation },
{ path: 'vehicles', Page: Vehicles },
{ path: 'delivery', Page: Delivery },
{ path: 'ledger', Page: Ledger },
{ path: 'handover', Page: Handover },
{ path: 'reports', Page: Reports },
{ path: 'condition', Page: Condition },
{ path: 'customer-group', Page: CustomerGroup },
{ path: 'payment', Page: Payment },
];
function renderPage(subPath: string) {
const matched = matchRoute(ROUTES, subPath);
if (!matched) {
return <UEmpty description={`没有匹配「${subPath}」的页面,请从菜单进入`} />;
}
const { Page, params } = matched;
return <Page params={params as PageParams} />;
}
function UsedcarApp() {
const [subPath, setSubPath] = useState<string>(readInitialPage);
useEffect(() => {
const bus = subappBus();
if (!bus) return;
const events = busEvents(shortName());
const onNavigate = (...args: unknown[]) => setSubPath(String(args[0] ?? ''));
const onLogout = () => setSubPath('');
bus.$on(events.navigate, onNavigate);
bus.$on(events.logout, onLogout);
bus.$emit(events.ready);
return () => {
bus.$off(events.navigate, onNavigate);
bus.$off(events.logout, onLogout);
};
}, []);
return useMemo(() => renderPage(subPath), [subPath]);
}
export function App() {
return (
<SubAppProvider>
<div className="uj-scope uj-usedcar-app">{UsedcarApp()}</div>
</SubAppProvider>
);
}