@@ -6,7 +6,11 @@ import {
66} from "@monkeytype/contracts/schemas/leaderboards" ;
77import { capitalizeFirstLetter } from "../utils/strings" ;
88import Ape from "../ape" ;
9- import { Mode } from "@monkeytype/contracts/schemas/shared" ;
9+ import {
10+ Mode ,
11+ Mode2Schema ,
12+ ModeSchema ,
13+ } from "@monkeytype/contracts/schemas/shared" ;
1014import * as Notifications from "../elements/notifications" ;
1115import Format from "../utils/format" ;
1216import { Auth , isAuthenticated } from "../firebase" ;
@@ -28,9 +32,13 @@ import {
2832 getStartOfDayTimestamp ,
2933} from "@monkeytype/util/date-and-time" ;
3034import { formatDistanceToNow } from "date-fns/formatDistanceToNow" ;
35+ import { z } from "zod" ;
36+ import { LocalStorageWithSchema } from "../utils/local-storage-with-schema" ;
37+ import { LanguageSchema } from "@monkeytype/contracts/schemas/util" ;
3138// import * as ServerConfiguration from "../ape/server-configuration";
3239
33- type LeaderboardType = "allTime" | "weekly" | "daily" ;
40+ const LeaderboardTypeSchema = z . enum ( [ "allTime" , "weekly" , "daily" ] ) ;
41+ type LeaderboardType = z . infer < typeof LeaderboardTypeSchema > ;
3442
3543type AllTimeState = {
3644 type : "allTime" ;
@@ -87,6 +95,21 @@ const state = {
8795 scrollToUserAfterFill : false ,
8896} as State ;
8997
98+ const SelectorSchema = z . object ( {
99+ type : LeaderboardTypeSchema ,
100+ mode : ModeSchema . optional ( ) ,
101+ mode2 : Mode2Schema . optional ( ) ,
102+ language : LanguageSchema . optional ( ) ,
103+ yesterday : z . boolean ( ) . optional ( ) ,
104+ lastWeek : z . boolean ( ) . optional ( ) ,
105+ } ) ;
106+
107+ const selectorLS = new LocalStorageWithSchema ( {
108+ key : "leaderboardSelector" ,
109+ schema : SelectorSchema ,
110+ fallback : { type : "allTime" , mode : "time" , mode2 : "15" } ,
111+ } ) ;
112+
90113function updateTitle ( ) : void {
91114 const type =
92115 state . type === "allTime"
@@ -1140,11 +1163,18 @@ function updateGetParameters(): void {
11401163
11411164 const newUrl = `${ window . location . pathname } ?${ params . toString ( ) } ` ;
11421165 window . history . replaceState ( { } , "" , newUrl ) ;
1166+
1167+ selectorLS . set ( state ) ;
11431168}
11441169
11451170function readGetParameters ( ) : void {
11461171 const params = new URLSearchParams ( window . location . search ) ;
11471172
1173+ if ( params . size == 0 ) {
1174+ Object . assign ( state , selectorLS . get ( ) ) ;
1175+ return ;
1176+ }
1177+
11481178 const type = params . get ( "type" ) as "allTime" | "weekly" | "daily" ;
11491179 if ( type ) {
11501180 state . type = type ;
0 commit comments