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
|
import { SortDirection } from '$gql/graphql';
import { getContext, setContext } from 'svelte';
import { writable, type Writable } from 'svelte/store';
import { navigate } from './Navigation';
export interface SortData<T> {
on: T;
direction: SortDirection;
seed: number | undefined;
}
export class SortContext<T extends string> {
on: T;
direction: SortDirection;
seed: number | undefined;
labels: Record<T, string>;
constructor({ on, direction, seed }: SortData<T>, labels: Record<T, string>) {
this.on = on;
this.direction = direction;
this.seed = seed;
this.labels = labels;
}
set update({ on, direction, seed }: SortData<T>) {
this.on = on;
this.direction = direction;
this.seed = seed;
}
apply(params: URLSearchParams) {
navigate({ sort: { on: this.on, direction: this.direction, seed: this.seed } }, params);
}
}
export function initSortContext<T extends string>(sort: SortData<T>, labels: Record<T, string>) {
return setContext<Writable<SortContext<T>>>('sort', writable(new SortContext(sort, labels)));
}
export function getSortContext<T extends string>() {
return getContext<Writable<SortContext<T>>>('sort');
}
|