feat(platform-web): preserve protocol in vehicle links

This commit is contained in:
lingniu
2026-07-04 01:15:39 +08:00
parent 8e5cb5b8c7
commit 657674f233
4 changed files with 41 additions and 15 deletions

View File

@@ -17,6 +17,7 @@ export default function App() {
const [activePage, setActivePage] = useState<PageKey>(initialRoute.page ?? 'dashboard');
const [activeVin, setActiveVin] = useState(initialVehicleKey);
const [analysisVin, setAnalysisVin] = useState(initialVehicleKey);
const [activeProtocol, setActiveProtocol] = useState(initialRoute.protocol ?? '');
const [linkIssueCount, setLinkIssueCount] = useState<number | null>(null);
const refreshOpsHealth = useCallback((showError = true) => {
@@ -55,13 +56,14 @@ export default function App() {
setAnalysisVin(route.keyword);
}
}
setActiveProtocol(route.protocol ?? '');
};
window.addEventListener('hashchange', applyHashRoute);
return () => window.removeEventListener('hashchange', applyHashRoute);
}, []);
const replaceHash = (page: PageKey, keyword?: string) => {
const nextHash = buildAppHash({ page, keyword });
const replaceHash = (page: PageKey, keyword?: string, protocol?: string) => {
const nextHash = buildAppHash({ page, keyword, protocol });
if (window.location.hash !== nextHash) {
window.history.replaceState(null, '', nextHash);
}
@@ -70,11 +72,11 @@ export default function App() {
const navigatePage = (page: PageKey) => {
setActivePage(page);
if (page === 'detail') {
replaceHash(page, activeVin);
replaceHash(page, activeVin, activeProtocol);
return;
}
if (page === 'history' || page === 'mileage') {
replaceHash(page, analysisVin);
replaceHash(page, analysisVin, activeProtocol);
return;
}
replaceHash(page);
@@ -89,6 +91,7 @@ export default function App() {
const resolved = await api.vehicleResolve(new URLSearchParams({ keyword: lookupKey }));
const nextKey = resolved.resolved && resolved.vin ? resolved.vin : lookupKey;
setActiveVin(nextKey);
setActiveProtocol('');
setActivePage('detail');
replaceHash('detail', nextKey);
if (!resolved.resolved) {
@@ -106,7 +109,7 @@ export default function App() {
}
setAnalysisVin(nextVin);
setActivePage('history');
replaceHash('history', nextVin);
replaceHash('history', nextVin, activeProtocol);
};
const openMileageForVehicle = (vin: string) => {
@@ -116,14 +119,25 @@ export default function App() {
}
setAnalysisVin(nextVin);
setActivePage('mileage');
replaceHash('mileage', nextVin);
replaceHash('mileage', nextVin, activeProtocol);
};
const updateVehicleDetailQuery = (keyword: string, protocol?: string) => {
const nextKeyword = keyword.trim();
const nextProtocol = protocol?.trim() ?? '';
if (!nextKeyword) {
return;
}
setActiveVin(nextKeyword);
setActiveProtocol(nextProtocol);
replaceHash('detail', nextKeyword, nextProtocol);
};
const pages: Record<PageKey, JSX.Element> = {
dashboard: <Dashboard onOpenVehicle={openVehicle} onOpenQuality={() => navigatePage('quality')} />,
vehicles: <Vehicles onOpenVehicle={openVehicle} />,
realtime: <Realtime onOpenVehicle={openVehicle} />,
detail: <VehicleDetail vin={activeVin} onOpenHistory={openHistoryForVehicle} onOpenMileage={openMileageForVehicle} />,
detail: <VehicleDetail vin={activeVin} protocol={activeProtocol} onOpenHistory={openHistoryForVehicle} onOpenMileage={openMileageForVehicle} onQueryChange={updateVehicleDetailQuery} />,
history: <History initialVin={analysisVin} onOpenVehicle={openVehicle} />,
mileage: <Mileage initialVin={analysisVin} onOpenVehicle={openVehicle} />,
quality: <Quality onOpenVehicle={openVehicle} onHealthLoaded={(health) => setLinkIssueCount(health.linkHealth.filter((item) => item.status !== 'ok').length)} />