feat(platform): persist history filters in route
This commit is contained in:
@@ -26,6 +26,9 @@ export default function App() {
|
||||
const [realtimeFilters, setRealtimeFilters] = useState<Record<string, string>>(
|
||||
initialRoute.page === 'realtime' ? realtimeFiltersFromRoute(initialRoute) : {}
|
||||
);
|
||||
const [historyFilters, setHistoryFilters] = useState<Record<string, string>>(
|
||||
initialRoute.page === 'history' ? historyFiltersFromRoute(initialRoute) : {}
|
||||
);
|
||||
const [qualityFilters, setQualityFilters] = useState<Record<string, string>>(
|
||||
initialRoute.page === 'quality' ? qualityFiltersFromRoute(initialRoute) : {}
|
||||
);
|
||||
@@ -104,6 +107,9 @@ export default function App() {
|
||||
if (route.page === 'realtime') {
|
||||
setRealtimeFilters(realtimeFiltersFromRoute(route));
|
||||
}
|
||||
if (route.page === 'history') {
|
||||
setHistoryFilters(historyFiltersFromRoute(route));
|
||||
}
|
||||
if (route.page === 'quality') {
|
||||
setQualityFilters(qualityFiltersFromRoute(route));
|
||||
}
|
||||
@@ -128,6 +134,10 @@ export default function App() {
|
||||
return;
|
||||
}
|
||||
if (page === 'history' || page === 'mileage') {
|
||||
if (page === 'history') {
|
||||
replaceHistoryHash(historyFilters);
|
||||
return;
|
||||
}
|
||||
replaceHash(page, analysisVin, activeProtocol);
|
||||
return;
|
||||
}
|
||||
@@ -172,6 +182,26 @@ export default function App() {
|
||||
replaceHash('realtime', keyword, protocol, restFilters);
|
||||
};
|
||||
|
||||
const updateHistoryFilters = (filters: Record<string, unknown> = {}, tab = historyTab) => {
|
||||
const nextFilters = normalizeHistoryFilterValues(filters);
|
||||
setHistoryFilters(nextFilters);
|
||||
if (nextFilters.keyword) {
|
||||
setAnalysisVin(nextFilters.keyword);
|
||||
}
|
||||
setActiveProtocol(nextFilters.protocol ?? '');
|
||||
setHistoryTab(tab);
|
||||
replaceHistoryHash(nextFilters, tab);
|
||||
};
|
||||
|
||||
const replaceHistoryHash = (filters: Record<string, string> = {}, tab = historyTab) => {
|
||||
const { keyword, protocol, ...restFilters } = filters;
|
||||
const routeFilters = { ...restFilters };
|
||||
if (tab && tab !== 'location') {
|
||||
routeFilters.tab = tab;
|
||||
}
|
||||
replaceHash('history', keyword ?? analysisVin, protocol ?? activeProtocol, routeFilters);
|
||||
};
|
||||
|
||||
const replaceQualityHash = (filters: Record<string, string> = {}) => {
|
||||
const keyword = filters.keyword;
|
||||
const protocol = filters.protocol;
|
||||
@@ -218,6 +248,7 @@ export default function App() {
|
||||
setAnalysisVin(nextVin);
|
||||
setActiveProtocol(nextProtocol);
|
||||
setHistoryTab('location');
|
||||
setHistoryFilters({ keyword: nextVin, protocol: nextProtocol });
|
||||
setActivePage('history');
|
||||
replaceHash('history', nextVin, nextProtocol);
|
||||
};
|
||||
@@ -231,6 +262,7 @@ export default function App() {
|
||||
setAnalysisVin(nextVin);
|
||||
setActiveProtocol(nextProtocol);
|
||||
setHistoryTab('raw');
|
||||
setHistoryFilters({ keyword: nextVin, protocol: nextProtocol });
|
||||
setActivePage('history');
|
||||
replaceHash('history', nextVin, nextProtocol, { tab: 'raw' });
|
||||
};
|
||||
@@ -263,7 +295,7 @@ export default function App() {
|
||||
vehicles: <Vehicles onOpenVehicle={openVehicle} onFiltersChange={updateVehicleFilters} initialFilters={vehicleFilters} />,
|
||||
realtime: <Realtime onOpenVehicle={openVehicle} onFiltersChange={updateRealtimeFilters} initialFilters={realtimeFilters} />,
|
||||
detail: <VehicleDetail vin={activeVin} protocol={activeProtocol} onOpenHistory={openHistoryForVehicle} onOpenRaw={openRawForVehicle} onOpenMileage={openMileageForVehicle} onOpenVehicles={openVehicles} onQueryChange={updateVehicleDetailQuery} />,
|
||||
history: <History initialVin={analysisVin} initialProtocol={activeProtocol} initialTab={historyTab} onOpenVehicle={openVehicle} />,
|
||||
history: <History initialVin={analysisVin} initialProtocol={activeProtocol} initialTab={historyTab} initialFilters={historyFilters} onFiltersChange={updateHistoryFilters} onOpenVehicle={openVehicle} />,
|
||||
mileage: <Mileage initialVin={analysisVin} initialProtocol={activeProtocol} onOpenVehicle={openVehicle} />,
|
||||
quality: <Quality onOpenVehicle={openVehicle} onHealthLoaded={(health) => setLinkIssueCount(health.linkHealth.filter((item) => item.status !== 'ok').length)} onFiltersChange={updateQualityFilters} initialFilters={qualityFilters} />
|
||||
};
|
||||
@@ -300,6 +332,32 @@ function realtimeFiltersFromRoute(route: ReturnType<typeof parseAppHash>): Recor
|
||||
};
|
||||
}
|
||||
|
||||
function historyFiltersFromRoute(route: ReturnType<typeof parseAppHash>): Record<string, string> {
|
||||
return normalizeHistoryFilterValues({
|
||||
...(route.keyword ? { keyword: route.keyword } : {}),
|
||||
...(route.protocol ? { protocol: route.protocol } : {}),
|
||||
...(route.filters?.dateFrom ? { dateFrom: route.filters.dateFrom } : {}),
|
||||
...(route.filters?.dateTo ? { dateTo: route.filters.dateTo } : {}),
|
||||
...(route.filters?.includeFields ? { includeFields: route.filters.includeFields } : {}),
|
||||
...(route.filters?.fields ? { fields: route.filters.fields } : {})
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeHistoryFilterValues(filters: Record<string, unknown> = {}): Record<string, string> {
|
||||
const normalized: Record<string, string> = {};
|
||||
for (const key of ['keyword', 'protocol', 'dateFrom', 'dateTo', 'fields'] as const) {
|
||||
const value = String(filters[key] ?? '').trim();
|
||||
if (value) {
|
||||
normalized[key] = value;
|
||||
}
|
||||
}
|
||||
const includeFields = filters.includeFields;
|
||||
if (includeFields === true || includeFields === 'true') {
|
||||
normalized.includeFields = 'true';
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function serviceStatusFromOverview(overview: VehicleServiceOverview): VehicleServiceStatus {
|
||||
const sourceCount = overview.sourceCount;
|
||||
const onlineSourceCount = overview.onlineSourceCount;
|
||||
|
||||
Reference in New Issue
Block a user