-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathshareUrl.ts
More file actions
54 lines (47 loc) · 1.86 KB
/
Copy pathshareUrl.ts
File metadata and controls
54 lines (47 loc) · 1.86 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
import { getPathWithoutLang } from 'src/locale/allTranslations'
import { PageSearchState } from 'src/model/pageState'
type ShareableKey = Exclude<keyof PageSearchState, 'routes'>
// Only include params that are actually used on each page.
// Pages absent from this map (homepage, about, donate, etc.) get no params.
export const PAGE_SHARE_PARAMS: Partial<Record<string, ShareableKey[]>> = {
'/timeline': ['timestamp', 'operatorId', 'lineNumber', 'vehicleNumber', 'routeKey', 'startTime'],
'/gaps': ['timestamp', 'operatorId', 'lineNumber', 'routeKey'],
'/gaps_patterns': ['operatorId', 'lineNumber', 'routeKey'],
'/map': [],
'/velocity-heatmap': ['timestamp'],
'/single-line-map': [
'timestamp',
'operatorId',
'lineNumber',
'vehicleNumber',
'routeKey',
'startTime',
],
'/operator': ['operatorId', 'timestamp'],
}
/**
* Build a shareable URL for the given page.
*
* Only the params relevant to that page are included (see PAGE_SHARE_PARAMS).
* Extra params (e.g. page-local state registered via ExtraShareParamsContext)
* are appended last and override any SearchContext param with the same key.
*/
export const buildShareUrl = (
pathname: string,
search: PageSearchState,
extraParams: Record<string, string>,
origin = window.location.origin,
): string => {
const pagePath = getPathWithoutLang(pathname)
const relevantKeys = PAGE_SHARE_PARAMS[pagePath] ?? []
const params = new URLSearchParams()
for (const key of relevantKeys) {
const value = search[key]
if (value) params.set(key, String(value))
}
Object.entries(extraParams).forEach(([key, value]) => params.set(key, value))
const query = params.toString()
// Use the lang-stripped path so shared links are language-agnostic.
// The recipient's language preference (localStorage) picks their own lang.
return `${origin}${pagePath}${query ? `?${query}` : ''}`
}