1
+ import { useLocalStorage } from '$lib/stores/local_storage_helper.svelte' ;
1
2
import { type ContestTableProviders } from '$lib/utils/contest_table_provider' ;
2
3
3
4
/**
@@ -12,7 +13,10 @@ import { type ContestTableProviders } from '$lib/utils/contest_table_provider';
12
13
* with a default value of 'abcLatest20Rounds'.
13
14
*/
14
15
export class ActiveContestTypeStore {
15
- value = $state < ContestTableProviders > ( 'abcLatest20Rounds' ) ;
16
+ private storage = useLocalStorage < ContestTableProviders > (
17
+ 'contest_table_providers' ,
18
+ 'abcLatest20Rounds' ,
19
+ ) ;
16
20
17
21
/**
18
22
* Creates an instance with the specified contest type.
@@ -21,7 +25,9 @@ export class ActiveContestTypeStore {
21
25
* Defaults to 'abcLatest20Rounds'.
22
26
*/
23
27
constructor ( defaultContestType : ContestTableProviders = 'abcLatest20Rounds' ) {
24
- this . value = defaultContestType ;
28
+ if ( defaultContestType !== 'abcLatest20Rounds' || ! this . storage . value ) {
29
+ this . storage . value = defaultContestType ;
30
+ }
25
31
}
26
32
27
33
/**
@@ -30,7 +36,7 @@ export class ActiveContestTypeStore {
30
36
* @returns The current value of contest table providers.
31
37
*/
32
38
get ( ) : ContestTableProviders {
33
- return this . value ;
39
+ return this . storage . value ;
34
40
}
35
41
36
42
/**
@@ -39,7 +45,7 @@ export class ActiveContestTypeStore {
39
45
* @param newContestType - The contest type to set as the current value
40
46
*/
41
47
set ( newContestType : ContestTableProviders ) : void {
42
- this . value = newContestType ;
48
+ this . storage . value = newContestType ;
43
49
}
44
50
45
51
/**
@@ -48,16 +54,27 @@ export class ActiveContestTypeStore {
48
54
* @returns `true` if the current contest type matches the provided contest type, `false` otherwise
49
55
*/
50
56
isSame ( contestType : ContestTableProviders ) : boolean {
51
- return this . value === contestType ;
57
+ return this . storage . value === contestType ;
52
58
}
53
59
54
60
/**
55
61
* Resets the active contest type to the default value.
56
62
* Sets the internal value to 'abcLatest20Rounds'.
57
63
*/
58
64
reset ( ) : void {
59
- this . value = 'abcLatest20Rounds' ;
65
+ this . storage . value = 'abcLatest20Rounds' ;
60
66
}
61
67
}
62
68
63
- export const activeContestTypeStore = new ActiveContestTypeStore ( ) ;
69
+ let instance : ActiveContestTypeStore | null = null ;
70
+
71
+ export function getActiveContestTypeStore ( ) : ActiveContestTypeStore {
72
+ if ( ! instance ) {
73
+ instance = new ActiveContestTypeStore ( ) ;
74
+ }
75
+
76
+ return instance ;
77
+ }
78
+
79
+ // Export the singleton instance of the store.
80
+ export const activeContestTypeStore = getActiveContestTypeStore ( ) ;
0 commit comments