Skip to content

Commit

Permalink
Merge pull request #5 from BYU-CPC/add-leaderboard-by-id-route
Browse files Browse the repository at this point in the history
use leaderboard by id route
  • Loading branch information
joshbtay authored Sep 14, 2024
2 parents e5afa83 + e3c39b5 commit a223555
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 68 deletions.
27 changes: 15 additions & 12 deletions src/components/Leaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import useUser from "../hooks/UseProfile";
import "./Leaderboard.css";
import { useUsers } from "../hooks/UseUser";
import { LeaderboardRow } from "./LeaderboardRow";
import { useThisWeek } from "../hooks/UseWeek";
import { useSearchParams } from "react-router-dom";
import { useAllStudyProblems, useProblems } from "../hooks/UseProblem";
import { useProblems } from "../hooks/UseProblem";
import { useQueries } from "@tanstack/react-query";
import { getStats } from "../score/score";
import { useLeaderboardIndex } from "../hooks/UseLeaderboard";
import {
useCurrentLeaderboard,
useLeaderboard,
useLeaderboardIndex,
} from "../hooks/UseLeaderboard";

function formatCodeforcesId(input: string) {
const match = input.match(/^(\d+)(\D.*)$/);
Expand All @@ -22,8 +24,13 @@ function formatCodeforcesId(input: string) {
export function Leaderboard() {
const user = useUser();
const users = useUsers();
const thisWeek = useThisWeek();
const allProblemsLength = thisWeek.kattis.length + thisWeek.codeforces.length;
const leaderboard = useCurrentLeaderboard();

const { data } = useLeaderboard(leaderboard);
const thisWeek = data?.thisWeek;
const allStudyProblems = data?.allProblems;
const allProblemsLength =
(thisWeek?.kattis?.length ?? 0) + (thisWeek?.codeforces?.length ?? 0);
const solvedProblems: {
kattis: Map<string, number>;
codeforces: Map<string, number>;
Expand All @@ -39,11 +46,8 @@ export function Leaderboard() {
solvedProblems.codeforces.set(problem, 1);
}
}
const links = thisWeek.links ?? {};
const [params] = useSearchParams();
const leaderboard = params.get("leaderboard") || "all";
const links = thisWeek?.links ?? {};
const { data: allProblems } = useProblems();
const { data: allStudyProblems } = useAllStudyProblems();
const { data: leaderboardIndex } = useLeaderboardIndex();
const leaderboardData = leaderboardIndex?.[leaderboard];

Expand All @@ -56,7 +60,6 @@ export function Leaderboard() {
: undefined;
},
enabled: !!allProblems && !!allStudyProblems && !!leaderboardData,
staleTime: 1000 * 60 * 5,
})),
combine: (results) => results.map((r) => r.data).filter((a) => !!a),
});
Expand All @@ -70,7 +73,7 @@ export function Leaderboard() {
}
return (
<div className="Leaderboard flexCol w-full align-center">
{thisWeek.topic && (
{thisWeek?.topic && (
<div className="responsive-fg bg-secondary flexCol">
<div className="align-center">
<h4 className="large text-green-400">
Expand Down
9 changes: 6 additions & 3 deletions src/components/LeaderboardRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Flame from "../icons/Flame";
import FlameBorder from "../icons/FlameBorder";
import DeadFlame from "../icons/DeadFlame";
import React from "react";
import { useThisWeek } from "../hooks/UseWeek";
import { UserStats } from "../score/score";
import ProgressBar from "./ProgressBar";
import { useCurrentLeaderboard, useLeaderboard } from "../hooks/UseLeaderboard";
function WeeklyProblemBox({
solved,
allProblemsLength,
Expand Down Expand Up @@ -36,8 +36,11 @@ type LeaderboardRowProps = {

export function LeaderboardRow({ userStats, rank, isMe }: LeaderboardRowProps) {
const userId = userStats.user.id;
const thisWeek = useThisWeek();
const allProblemsLength = thisWeek.kattis.length + thisWeek.codeforces.length;
const leaderboard = useCurrentLeaderboard();
const { data } = useLeaderboard(leaderboard);
const thisWeek = data?.thisWeek;
const allProblemsLength =
(thisWeek?.kattis?.length ?? 0) + (thisWeek?.codeforces?.length ?? 0);
const solvedKattis = new Set(Object.keys(userStats.user.kattis_submissions));
const solvedCodeforces = new Set(
Object.keys(userStats.user.codeforces_submissions ?? {})
Expand Down
5 changes: 2 additions & 3 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from "react";
import UserBadge from "./UserBadge";
import { useSearchParams } from "react-router-dom";
import Countdown from "./Countdown";
import { useCurrentLeaderboard } from "../hooks/UseLeaderboard";

const Sidebar = ({ children }: React.PropsWithChildren) => {
const [params] = useSearchParams();
const leaderboard = params.get("leaderboard") ?? "all";
const leaderboard = useCurrentLeaderboard();
return (
<div className="flexCol align-center">
<div className="shadow w-full flexRow space-between bg-secondary flex-center">
Expand Down
55 changes: 53 additions & 2 deletions src/hooks/UseLeaderboard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useQuery } from "@tanstack/react-query";
import axios from "axios";
import { BACKEND_URL } from "./base";
import { Platform, platformValues } from "../types/platform";
import { useSearchParams } from "react-router-dom";

export type Leaderboard = { start: Date; end: Date };
export type LeaderboardEntry = { start: Date; end: Date };

export type LeaderboardIndex = Record<string, Leaderboard>;
export type LeaderboardIndex = Record<string, LeaderboardEntry>;
type LeaderboardResponse = Record<
string,
{
Expand Down Expand Up @@ -37,3 +39,52 @@ export const useLeaderboardIndex = () => {
: undefined;
return { ...query, data: transformedData };
};

type PracticeWeek = string;

export type Leaderboard = {
practice_set?: Record<
PracticeWeek,
{ topic: string; links: Record<string, string> } & Partial<
Record<Platform, string[]>
>
>;
school?: string;
};

export type StudyProblems = Record<Platform, Set<string>>;

const getLeaderboard = async (id: string): Promise<Leaderboard> => {
return (await axios.get(`${BACKEND_URL}/leaderboard/${id}`)).data;
};

export const useLeaderboard = (id: string) => {
const query = useQuery({
queryKey: ["leaderboard", id],
queryFn: () => getLeaderboard(id),
staleTime: 1000 * 60 * 5,
});
if (!query.data) return query;
const practiceSets = Object.entries(query.data.practice_set ?? {})
.map(([key, value]) => ({ ...value, start: key }))
.sort((a, b) => a.start.localeCompare(b.start));
const allProblems = practiceSets.reduce((acc, week) => {
platformValues.forEach((platform) => {
const problems = week[platform];
problems?.forEach((problem) => {
acc[platform].add(problem);
});
});
return acc;
}, platformValues.reduce((x, platform) => ({ ...x, [platform]: new Set<string>() }), {}) as StudyProblems);
const thisWeek = practiceSets.at(practiceSets.length - 1);
return {
...query,
data: { allProblems, practiceSets, thisWeek },
};
};

export const useCurrentLeaderboard = () => {
const [params] = useSearchParams();
return params.get("leaderboard") ?? "byu_summer_24";
};
14 changes: 0 additions & 14 deletions src/hooks/UseProblem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { Platform } from "../types/platform";
type Problem = { [key: string]: { rating: number; name: string } };
export type AllProblems = Record<Platform, Problem>;

export type StudyProblems = Record<Platform, string[]>;

async function getAllProblems(): Promise<AllProblems> {
return (await axios.get(`${BACKEND_URL}/get_all_problems`)).data;
}
Expand All @@ -18,15 +16,3 @@ export const useProblems = () => {
staleTime: 1000 * 60 * 60 * 24,
});
};

async function getAllStudyProblems(): Promise<StudyProblems> {
return (await axios.get(`${BACKEND_URL}/get_all_study_problems`)).data;
}

export const useAllStudyProblems = () => {
return useQuery({
queryFn: getAllStudyProblems,
queryKey: ["allStudyProblems"],
staleTime: 1000 * 60 * 60,
});
};
30 changes: 0 additions & 30 deletions src/hooks/UseWeek.tsx

This file was deleted.

8 changes: 4 additions & 4 deletions src/score/score.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Leaderboard } from "../hooks/UseLeaderboard";
import { AllProblems, StudyProblems } from "../hooks/UseProblem";
import type { LeaderboardEntry, StudyProblems } from "../hooks/UseLeaderboard";
import { AllProblems } from "../hooks/UseProblem";
import { User } from "../hooks/UseUser";
import { Platform, platformValues } from "../types/platform";

Expand Down Expand Up @@ -90,7 +90,7 @@ export function getStats(
user: User,
allProblems: AllProblems,
studyProblems: StudyProblems,
leaderboard: Leaderboard
leaderboard: LeaderboardEntry
) {
const kattisSubmissions: Record<string, { type: "practice"; time: number }> =
Object.keys(user.kattis_submissions).reduce(
Expand Down Expand Up @@ -141,7 +141,7 @@ export function getStats(
const current_exp = exp.get(day) ?? DAILY_BONUS;
const multiplier =
submission.type === "contestant" ||
studyProblems[platform].includes(problemId)
studyProblems[platform].has(problemId)
? 2
: 1;
if (submission.type === "contestant") {
Expand Down

0 comments on commit a223555

Please sign in to comment.