From 1cd70cc330bdff3b15c5f5658fa4995911828d89 Mon Sep 17 00:00:00 2001 From: Josh Taylor Date: Mon, 9 Sep 2024 19:55:09 -0600 Subject: [PATCH] fix bad type --- src/App.tsx | 3 ++- src/components/LeaderboardRow.tsx | 2 +- src/hooks/UseUser.tsx | 16 ++++++++-------- src/hooks/base.tsx | 19 +++++++++++++++++++ src/score/score.ts | 7 ++++++- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a95aab4..a3277db 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -14,6 +14,7 @@ import { type AsyncStorage, type PersistedQuery, } from "@tanstack/react-query-persist-client"; +import { BUSTER } from "./hooks/base"; function createIdbStorage(): AsyncStorage { return { getItem: async (key) => await get(key), @@ -32,7 +33,7 @@ const queryClient = new QueryClient({ gcTime: 1000 * 60 * 60 * 24 * 7, // 1 week staleTime: 1000, persister: experimental_createPersister({ - buster: "1.0.1", + buster: BUSTER, storage: createIdbStorage(), maxAge: 1000 * 60 * 60 * 24 * 7, // 1 week serialize: (persistedQuery) => persistedQuery, diff --git a/src/components/LeaderboardRow.tsx b/src/components/LeaderboardRow.tsx index eeda2d2..6d93a38 100644 --- a/src/components/LeaderboardRow.tsx +++ b/src/components/LeaderboardRow.tsx @@ -40,7 +40,7 @@ export function LeaderboardRow({ userStats, rank, isMe }: LeaderboardRowProps) { const allProblemsLength = thisWeek.kattis.length + thisWeek.codeforces.length; const solvedKattis = new Set(Object.keys(userStats.user.kattis_submissions)); const solvedCodeforces = new Set( - Object.keys(userStats.user.codeforces_submissions) + Object.keys(userStats.user.codeforces_submissions ?? {}) ); return ( diff --git a/src/hooks/UseUser.tsx b/src/hooks/UseUser.tsx index 6fb1be0..46446f1 100644 --- a/src/hooks/UseUser.tsx +++ b/src/hooks/UseUser.tsx @@ -1,16 +1,18 @@ import { useQuery, useQueryClient } from "@tanstack/react-query"; import axios from "axios"; -import { BACKEND_URL } from "./base"; +import { BACKEND_URL, setQueryDataPersist } from "./base"; export type User = { kattis_username: string; codeforces_username: string; display_name: string; id: string; kattis_submissions: Record; - codeforces_submissions: Record< - string, - { type: "practice" | "contestant" | "virtual"; time: number } - > & { contests: Record }; + codeforces_submissions: + | (Record< + string, + { type: "practice" | "contestant" | "virtual"; time: number } + > & { contests: Record }) + | null; }; async function getUsers(): Promise { @@ -26,9 +28,7 @@ export const useUsers = () => { }); if (query.data) { for (const row of query.data) { - queryClient.setQueryData(["user", row.id], row, { - updatedAt: Date.now(), - }); + setQueryDataPersist(["user", row.id], row, queryClient); } } return query.data ?? []; diff --git a/src/hooks/base.tsx b/src/hooks/base.tsx index 5b9e984..a84bdca 100644 --- a/src/hooks/base.tsx +++ b/src/hooks/base.tsx @@ -1,2 +1,21 @@ +import { set } from "idb-keyval"; +import { type QueryClient } from "@tanstack/react-query"; export const BACKEND_URL = "https://byu-cpc-backend-433866642768.us-west1.run.app"; +export const BUSTER = "1.0.2"; + +export function setQueryDataPersist( + key: string[], + data: T, + client: QueryClient +) { + client.setQueryData(key, data, { updatedAt: Date.now() }); + const queryKey = `tanstack-query-${JSON.stringify(key)}`; + const queryState = client.getQueryData(key); + set(queryKey, { + state: queryState, + queryKey, + queryHash: JSON.stringify(key), + buster: BUSTER, + }); +} diff --git a/src/score/score.ts b/src/score/score.ts index b53f724..9aba677 100644 --- a/src/score/score.ts +++ b/src/score/score.ts @@ -81,6 +81,11 @@ const getLevel = (score: number) => { return { level, nextLevel, currentExp }; }; +const emptyCodeforces: Record< + string, + { type: "practice" | "contestant" | "virtual"; time: number } +> = {}; + export function getStats( user: User, allProblems: AllProblems, @@ -96,7 +101,7 @@ export function getStats( {} ); const all_submissions = { - codeforces: user.codeforces_submissions, + codeforces: user.codeforces_submissions ?? emptyCodeforces, kattis: kattisSubmissions, }; let problemCount = 0;