feat(platform-web): carry detail source to analysis links

This commit is contained in:
lingniu
2026-07-04 01:31:50 +08:00
parent a12d86e6f4
commit 9886fe2481
3 changed files with 90 additions and 9 deletions

View File

@@ -103,24 +103,28 @@ export default function App() {
}
};
const openHistoryForVehicle = (vin: string) => {
const openHistoryForVehicle = (vin: string, protocol?: string) => {
const nextVin = vin.trim();
const nextProtocol = protocol?.trim() ?? activeProtocol;
if (!nextVin) {
return;
}
setAnalysisVin(nextVin);
setActiveProtocol(nextProtocol);
setActivePage('history');
replaceHash('history', nextVin, activeProtocol);
replaceHash('history', nextVin, nextProtocol);
};
const openMileageForVehicle = (vin: string) => {
const openMileageForVehicle = (vin: string, protocol?: string) => {
const nextVin = vin.trim();
const nextProtocol = protocol?.trim() ?? activeProtocol;
if (!nextVin) {
return;
}
setAnalysisVin(nextVin);
setActiveProtocol(nextProtocol);
setActivePage('mileage');
replaceHash('mileage', nextVin, activeProtocol);
replaceHash('mileage', nextVin, nextProtocol);
};
const updateVehicleDetailQuery = (keyword: string, protocol?: string) => {

View File

@@ -28,8 +28,8 @@ export function VehicleDetail({
}: {
vin: string;
protocol?: string;
onOpenHistory: (vin: string) => void;
onOpenMileage: (vin: string) => void;
onOpenHistory: (vin: string, protocol?: string) => void;
onOpenMileage: (vin: string, protocol?: string) => void;
onQueryChange?: (keyword: string, protocol?: string) => void;
}) {
const [query, setQuery] = useState<VehicleQuery>({ keyword: vin, protocol });
@@ -130,8 +130,8 @@ export function VehicleDetail({
<Space>
<Button icon={<IconSearch />} htmlType="submit" theme="solid" type="primary"></Button>
<Button icon={<IconRefresh />} onClick={() => load(query)} loading={loading}></Button>
<Button disabled={!hasResolvedVIN} onClick={() => onOpenHistory(resolvedVIN)}></Button>
<Button disabled={!hasResolvedVIN} onClick={() => onOpenMileage(resolvedVIN)}></Button>
<Button disabled={!hasResolvedVIN} onClick={() => onOpenHistory(resolvedVIN, activeProtocol)}></Button>
<Button disabled={!hasResolvedVIN} onClick={() => onOpenMileage(resolvedVIN, activeProtocol)}></Button>
</Space>
</Form>
</Card>

View File

@@ -1,8 +1,9 @@
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, expect, test, vi } from 'vitest';
import App from '../App';
afterEach(() => {
cleanup();
window.history.replaceState(null, '', '/');
vi.restoreAllMocks();
});
@@ -321,3 +322,79 @@ test('switches vehicle detail to a source from the coverage cards', async () =>
});
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/vehicles/detail?keyword=VIN001&protocol=JT808'), undefined);
});
test('opens history with the currently selected vehicle detail source', async () => {
window.history.replaceState(null, '', '/#/detail?keyword=VIN001');
vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
const path = String(input);
if (path.includes('/api/vehicles/detail')) {
return {
ok: true,
json: async () => ({
data: {
vin: 'VIN001',
lookupKey: 'VIN001',
lookupResolved: true,
resolution: {
lookupKey: 'VIN001',
resolved: true,
vin: 'VIN001',
plate: '粤AG18312',
phone: '13307795425',
oem: 'G7s',
protocols: ['GB32960', 'JT808'],
online: true,
lastSeen: '2026-07-03 20:12:10'
},
realtimeSummary: {
vin: 'VIN001',
plate: '粤AG18312',
phone: '13307795425',
oem: 'G7s',
protocols: ['GB32960', 'JT808'],
sourceCount: 2,
onlineSourceCount: 2,
online: true,
primaryProtocol: 'GB32960',
longitude: 113.2,
latitude: 23.1,
speedKmh: 30,
socPercent: 78,
totalMileageKm: 100,
lastSeen: '2026-07-03 20:12:10'
},
sources: ['GB32960', 'JT808'],
sourceStatus: [
{ protocol: 'GB32960', online: true, lastSeen: '2026-07-03 20:12:10', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true },
{ protocol: 'JT808', online: true, lastSeen: '2026-07-03 20:12:08', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true }
],
realtime: [],
history: { items: [], total: 0, limit: 20, offset: 0 },
raw: { items: [], total: 0, limit: 10, offset: 0 },
mileage: { items: [], total: 0, limit: 20, offset: 0 },
quality: { items: [], total: 0, limit: 20, offset: 0 }
},
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
}
return {
ok: true,
json: async () => ({
data: { items: [], total: 0, limit: 10, offset: 0 },
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
});
render(<App />);
fireEvent.click(await screen.findByRole('button', { name: '仅看 JT808' }));
fireEvent.click(screen.getByRole('button', { name: '查看历史' }));
await waitFor(() => {
expect(window.location.hash).toBe('#/history?keyword=VIN001&protocol=JT808');
});
});