fix(web): harden route transitions and monitor memory

This commit is contained in:
lingniu
2026-07-15 23:54:21 +08:00
parent 3fabcf181a
commit c29ccdf2da
17 changed files with 332 additions and 36 deletions

View File

@@ -0,0 +1,47 @@
import { lazy, type ComponentType } from 'react';
export type RouteSection = 'monitor' | 'vehicles' | 'tracks' | 'history' | 'statistics' | 'access' | 'alerts' | 'operations';
type RouteModule = { default: ComponentType };
const importers: Record<RouteSection, () => Promise<RouteModule>> = {
monitor: () => import('../pages/MonitorPage'),
vehicles: () => import('../pages/VehiclePage'),
tracks: () => import('../pages/TrackPage'),
history: () => import('../pages/HistoryPage'),
statistics: () => import('../pages/StatisticsPage'),
access: () => import('../pages/AccessPage'),
alerts: () => import('../pages/AlertsPage'),
operations: () => import('../pages/OperationsPage')
};
const pendingModules = new Map<RouteSection, Promise<RouteModule>>();
function loadRoute(section: RouteSection) {
const pending = pendingModules.get(section);
if (pending) return pending;
const next = importers[section]();
pendingModules.set(section, next);
void next.catch(() => pendingModules.delete(section));
return next;
}
export function routeSectionForPath(pathname: string): RouteSection | undefined {
const section = pathname.split('?')[0].split('#')[0].split('/').filter(Boolean)[0] as RouteSection | undefined;
return section && section in importers ? section : undefined;
}
export function preloadRoute(pathname: string) {
const section = routeSectionForPath(pathname);
return section ? loadRoute(section) : undefined;
}
export const RoutePages = {
Monitor: lazy(() => loadRoute('monitor')),
Vehicles: lazy(() => loadRoute('vehicles')),
Tracks: lazy(() => loadRoute('tracks')),
History: lazy(() => loadRoute('history')),
Statistics: lazy(() => loadRoute('statistics')),
Access: lazy(() => loadRoute('access')),
Alerts: lazy(() => loadRoute('alerts')),
Operations: lazy(() => loadRoute('operations'))
} as const;