App.tsx
2.33 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
import { useEffect, useMemo, useState } from 'react';
import { busEvents } from '@uj/contracts';
import { SubAppProvider, readInitialPage, shortName, subappBus } from '@uj/platform-sdk';
import OrderList from './pages/OrderList';
import OrderDetail from './pages/OrderDetail';
import ScoreUsers from './pages/ScoreUsers';
import UserScoreDetail from './pages/UserScoreDetail';
import WithdrawManage from './pages/WithdrawManage';
import WithdrawBatch from './pages/WithdrawBatch';
import WithdrawArrival from './pages/WithdrawArrival';
import type { PageParams, RouteDef } from './routes';
import { matchRoute } from './routes';
import { UEmpty } from '@uj/ui';
/**
* 页面表:主壳给的 subPath(形如 order/123)在这里换成组件与路径参数。
* 新增页面 = 加一行 + 建一个页面目录;地址栏规则只在 routes.ts 里描述一次。
*/
const ROUTES: RouteDef[] = [
{ path: 'list', Page: OrderList },
{ path: 'order/:id', Page: OrderDetail },
{ path: 'users', Page: ScoreUsers },
{ path: 'user/:channelSource/:staffId', Page: UserScoreDetail },
{ path: 'user/:channelSource/:merchantId/:phone', Page: UserScoreDetail },
{ path: 'withdraw', Page: WithdrawManage },
{ path: 'batch/:id', Page: WithdrawBatch },
{ path: 'arrival/:id', Page: WithdrawArrival },
];
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 PointsApp() {
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-points-app">{PointsApp()}</div>
</SubAppProvider>
);
}