Skip to content

Commit 5acdc6d

Browse files
authored
impr: remember last opened leaderboard in local storage (@fehmer) (monkeytypegame#6304)
1 parent 566ec04 commit 5acdc6d

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

frontend/src/ts/pages/leaderboards.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
} from "@monkeytype/contracts/schemas/leaderboards";
77
import { capitalizeFirstLetter } from "../utils/strings";
88
import 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";
1014
import * as Notifications from "../elements/notifications";
1115
import Format from "../utils/format";
1216
import { Auth, isAuthenticated } from "../firebase";
@@ -28,9 +32,13 @@ import {
2832
getStartOfDayTimestamp,
2933
} from "@monkeytype/util/date-and-time";
3034
import { 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

3543
type 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+
90113
function 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

11451170
function 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

Comments
 (0)