Repository navigation
Expand file tree
/
Copy pathurlState.ts
More file actions
117 lines (101 loc) · 2.98 KB
/
Copy pathurlState.ts
File metadata and controls
117 lines (101 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type { MapLayers } from '@/types';
import type { MapView, TimeRange } from '@/components/Map';
const LAYER_KEYS: (keyof MapLayers)[] = [
'conflicts',
'bases',
'cables',
'pipelines',
'hotspots',
'nuclear',
'irradiators',
'sanctions',
'weather',
'economic',
'countries',
'waterways',
'outages',
'datacenters',
'flights',
'natural',
];
const TIME_RANGES: TimeRange[] = ['1h', '6h', '24h', '48h', '7d', 'all'];
const VIEW_VALUES: MapView[] = ['global', 'america', 'mena', 'eu', 'asia', 'latam', 'africa', 'oceania'];
export interface ParsedMapUrlState {
view?: MapView;
zoom?: number;
lat?: number;
lon?: number;
timeRange?: TimeRange;
layers?: MapLayers;
}
const clamp = (value: number, min: number, max: number): number =>
Math.min(max, Math.max(min, value));
export function parseMapUrlState(
search: string,
fallbackLayers: MapLayers
): ParsedMapUrlState {
const params = new URLSearchParams(search);
const viewParam = params.get('view');
const view = VIEW_VALUES.includes(viewParam as MapView) ? (viewParam as MapView) : undefined;
const zoomParam = params.get('zoom');
const zoomValue = zoomParam ? Number.parseFloat(zoomParam) : NaN;
const zoom = Number.isFinite(zoomValue) ? clamp(zoomValue, 1, 10) : undefined;
const latParam = params.get('lat');
const lonParam = params.get('lon');
const latValue = latParam ? Number.parseFloat(latParam) : NaN;
const lonValue = lonParam ? Number.parseFloat(lonParam) : NaN;
const lat = Number.isFinite(latValue) ? clamp(latValue, -90, 90) : undefined;
const lon = Number.isFinite(lonValue) ? clamp(lonValue, -180, 180) : undefined;
const timeRangeParam = params.get('timeRange');
const timeRange = TIME_RANGES.includes(timeRangeParam as TimeRange)
? (timeRangeParam as TimeRange)
: undefined;
const layersParam = params.get('layers');
let layers: MapLayers | undefined;
if (layersParam) {
const requested = new Set(
layersParam
.split(',')
.map((layer) => layer.trim())
.filter(Boolean)
);
layers = { ...fallbackLayers };
LAYER_KEYS.forEach((key) => {
layers![key] = requested.has(key);
});
}
return {
view,
zoom,
lat,
lon,
timeRange,
layers,
};
}
export function buildMapUrl(
baseUrl: string,
state: {
view: MapView;
zoom: number;
center?: { lat: number; lon: number } | null;
timeRange: TimeRange;
layers: MapLayers;
}
): string {
const url = new URL(baseUrl);
const params = new URLSearchParams();
if (state.center) {
params.set('lat', state.center.lat.toFixed(4));
params.set('lon', state.center.lon.toFixed(4));
}
params.set('zoom', state.zoom.toFixed(2));
params.set('view', state.view);
params.set('timeRange', state.timeRange);
const activeLayers = LAYER_KEYS.filter((layer) => state.layers[layer]);
if (activeLayers.length > 0) {
params.set('layers', activeLayers.join(','));
}
url.search = params.toString();
return url.toString();
}