Debian Codegraph @

frontend/src/stores/url.ts

raw
1 import type { Readable } from "svelte/store";
2 import { derived, writable } from "svelte/store";
3
4 /** The current URL hash. */
5 export const urlHash = writable("");
6
7 /** The current URL pathname. Should only be updated by the router. */
8 export const pathname = writable<string>();
9
10 /** The current URL search. Should only be updated by the router. */
11 export const search = writable<string>();
12
13 /** The current URL searchParams. */
14 export const searchParams: Readable<URLSearchParams> = derived(
15 search,
16 ($search) => new URLSearchParams($search),
17 );
18
19 /** These URL parameters for filters and conversion / interval are synced for most links. */
20 const synced_search_param_names = [
21 "account",
22 "charts",
23 "conversion",
24 "filter",
25 "interval",
26 "time",
27 ];
28
29 /** The current searchParamscontaining all values that are synced to the URL. */
30 export const syncedSearchParams: Readable<URLSearchParams> = derived(
31 searchParams,
32 ($searchParams) => {
33 const params = new URLSearchParams();
34 for (const name of synced_search_param_names) {
35 const value = $searchParams.get(name);
36 if (value != null && value) {
37 params.set(name, value);
38 }
39 }
40 return params;
41 },
42 );
43
44 export function closeOverlay(): void {
45 if (window.location.hash) {
46 window.history.pushState(null, "", "#");
47 }
48 urlHash.set("");
49 }