1+ import { useLocalStorage } from '$lib/stores/local_storage_helper.svelte' ;
12import { type ContestTableProviders } from '$lib/utils/contest_table_provider' ;
23
34/**
@@ -12,7 +13,10 @@ import { type ContestTableProviders } from '$lib/utils/contest_table_provider';
1213 * with a default value of 'abcLatest20Rounds'.
1314 */
1415export class ActiveContestTypeStore {
15- value = $state < ContestTableProviders > ( 'abcLatest20Rounds' ) ;
16+ private storage = useLocalStorage < ContestTableProviders > (
17+ 'contest_table_providers' ,
18+ 'abcLatest20Rounds' ,
19+ ) ;
1620
1721 /**
1822 * Creates an instance with the specified contest type.
@@ -21,7 +25,9 @@ export class ActiveContestTypeStore {
2125 * Defaults to 'abcLatest20Rounds'.
2226 */
2327 constructor ( defaultContestType : ContestTableProviders = 'abcLatest20Rounds' ) {
24- this . value = defaultContestType ;
28+ if ( defaultContestType !== 'abcLatest20Rounds' || ! this . storage . value ) {
29+ this . storage . value = defaultContestType ;
30+ }
2531 }
2632
2733 /**
@@ -30,7 +36,7 @@ export class ActiveContestTypeStore {
3036 * @returns The current value of contest table providers.
3137 */
3238 get ( ) : ContestTableProviders {
33- return this . value ;
39+ return this . storage . value ;
3440 }
3541
3642 /**
@@ -39,7 +45,7 @@ export class ActiveContestTypeStore {
3945 * @param newContestType - The contest type to set as the current value
4046 */
4147 set ( newContestType : ContestTableProviders ) : void {
42- this . value = newContestType ;
48+ this . storage . value = newContestType ;
4349 }
4450
4551 /**
@@ -48,16 +54,27 @@ export class ActiveContestTypeStore {
4854 * @returns `true` if the current contest type matches the provided contest type, `false` otherwise
4955 */
5056 isSame ( contestType : ContestTableProviders ) : boolean {
51- return this . value === contestType ;
57+ return this . storage . value === contestType ;
5258 }
5359
5460 /**
5561 * Resets the active contest type to the default value.
5662 * Sets the internal value to 'abcLatest20Rounds'.
5763 */
5864 reset ( ) : void {
59- this . value = 'abcLatest20Rounds' ;
65+ this . storage . value = 'abcLatest20Rounds' ;
6066 }
6167}
6268
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