Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bad type #4

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type AsyncStorage,
type PersistedQuery,
} from "@tanstack/react-query-persist-client";
import { BUSTER } from "./hooks/base";
function createIdbStorage(): AsyncStorage<PersistedQuery> {
return {
getItem: async (key) => await get(key),
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/components/LeaderboardRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
16 changes: 8 additions & 8 deletions src/hooks/UseUser.tsx
Original file line number Diff line number Diff line change
@@ -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<string, number>;
codeforces_submissions: Record<
string,
{ type: "practice" | "contestant" | "virtual"; time: number }
> & { contests: Record<string, number> };
codeforces_submissions:
| (Record<
string,
{ type: "practice" | "contestant" | "virtual"; time: number }
> & { contests: Record<string, number> })
| null;
};

async function getUsers(): Promise<User[]> {
Expand All @@ -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 ?? [];
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/base.tsx
Original file line number Diff line number Diff line change
@@ -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<T>(
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,
});
}
7 changes: 6 additions & 1 deletion src/score/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -96,7 +101,7 @@ export function getStats(
{}
);
const all_submissions = {
codeforces: user.codeforces_submissions,
codeforces: user.codeforces_submissions ?? emptyCodeforces,
kattis: kattisSubmissions,
};
let problemCount = 0;
Expand Down