feat(platform-web): support shareable vehicle routes
This commit is contained in:
25
vehicle-data-platform/apps/web/src/domain/appRoute.test.ts
Normal file
25
vehicle-data-platform/apps/web/src/domain/appRoute.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { buildAppHash, parseAppHash } from './appRoute';
|
||||
|
||||
describe('parseAppHash', () => {
|
||||
test('parses detail page keyword from hash query', () => {
|
||||
expect(parseAppHash('#/detail?keyword=%E7%B2%A4AG18312')).toEqual({
|
||||
page: 'detail',
|
||||
keyword: '粤AG18312'
|
||||
});
|
||||
});
|
||||
|
||||
test('ignores unknown pages', () => {
|
||||
expect(parseAppHash('#/unknown?keyword=VIN001')).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildAppHash', () => {
|
||||
test('builds shareable vehicle page hash with encoded keyword', () => {
|
||||
expect(buildAppHash({ page: 'history', keyword: '粤AG18312' })).toBe('#/history?keyword=%E7%B2%A4AG18312');
|
||||
});
|
||||
|
||||
test('builds page-only hash when keyword is empty', () => {
|
||||
expect(buildAppHash({ page: 'quality', keyword: '' })).toBe('#/quality');
|
||||
});
|
||||
});
|
||||
33
vehicle-data-platform/apps/web/src/domain/appRoute.ts
Normal file
33
vehicle-data-platform/apps/web/src/domain/appRoute.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { PageKey } from '../layout/AppShell';
|
||||
|
||||
const pageKeys = new Set<PageKey>(['dashboard', 'vehicles', 'realtime', 'detail', 'history', 'mileage', 'quality']);
|
||||
|
||||
export type AppRoute = {
|
||||
page?: PageKey;
|
||||
keyword?: string;
|
||||
};
|
||||
|
||||
export function parseAppHash(hash: string): AppRoute {
|
||||
const normalized = hash.trim().replace(/^#\/?/, '');
|
||||
if (!normalized) {
|
||||
return {};
|
||||
}
|
||||
const [pagePart, queryPart = ''] = normalized.split('?', 2);
|
||||
if (!pageKeys.has(pagePart as PageKey)) {
|
||||
return {};
|
||||
}
|
||||
const params = new URLSearchParams(queryPart);
|
||||
const keyword = params.get('keyword')?.trim() || undefined;
|
||||
return { page: pagePart as PageKey, keyword };
|
||||
}
|
||||
|
||||
export function buildAppHash(route: AppRoute): string {
|
||||
const page = route.page && pageKeys.has(route.page) ? route.page : 'dashboard';
|
||||
const params = new URLSearchParams();
|
||||
const keyword = route.keyword?.trim();
|
||||
if (keyword) {
|
||||
params.set('keyword', keyword);
|
||||
}
|
||||
const query = params.toString();
|
||||
return query ? `#/${page}?${query}` : `#/${page}`;
|
||||
}
|
||||
Reference in New Issue
Block a user