From 0adb2129db668295f646d93064bde5dab0a51c0c Mon Sep 17 00:00:00 2001 From: cmyui Date: Sun, 7 Jul 2024 17:35:00 -0400 Subject: [PATCH] Support for country rank graph --- src/adapters/akatsuki-api/profileHistory.ts | 55 ++++++++++++++++++--- src/components/UserProfileHistoryGraph.tsx | 11 +++-- src/pages/ProfilePage.tsx | 34 ++++++++++--- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/adapters/akatsuki-api/profileHistory.ts b/src/adapters/akatsuki-api/profileHistory.ts index ff67e98..fbf4c9f 100644 --- a/src/adapters/akatsuki-api/profileHistory.ts +++ b/src/adapters/akatsuki-api/profileHistory.ts @@ -1,16 +1,55 @@ import axios from "axios" import { GameMode } from "../../gameModes" -import { captureRejectionSymbol } from "events" -import { UNSAFE_useRouteId } from "react-router-dom" + +export enum ProfileHistoryType { + GlobalRank = "global_rank", + CountryRank = "country_rank", + PP = "pp", +} + +const getCaptureType = (historyType: ProfileHistoryType): string => { + switch (historyType) { + case ProfileHistoryType.GlobalRank: + return "rank" + case ProfileHistoryType.CountryRank: + return "rank" + case ProfileHistoryType.PP: + return "pp" + default: + throw new Error("Invalid capture type") + } +} + +const getCaptureValue = ( + requestCapture: any, + historyType: ProfileHistoryType +): number => { + switch (historyType) { + case ProfileHistoryType.GlobalRank: + return requestCapture.overall + case ProfileHistoryType.CountryRank: + return requestCapture.country + case ProfileHistoryType.PP: + return requestCapture.pp + default: + throw new Error("Invalid capture type") + } +} interface ProfileHistoryRequest { - type: "rank" | "pp" + // TODO: this abstraction does not match that of the + // profile-history-service backend, but we do so to + // increase functionality. We should migrate to new + // APIs on the backend to allow this flexibility. + // TODO: what about multiple params in one chart? + type: ProfileHistoryType + userId: number akatsukiMode: GameMode // NOTE: includes rx/ap } + export interface ProfileHistoryCapture { - overall: number - country: number + value: number capturedAt: Date } @@ -29,7 +68,7 @@ export const fetchUserProfileHistory = async ( ): Promise => { try { const response = await profileHistoryApiInstance.get( - `/v1/profile-history/${request.type}`, + `/v1/profile-history/${getCaptureType(request.type)}`, { params: { user_id: request.userId, @@ -37,10 +76,10 @@ export const fetchUserProfileHistory = async ( }, } ) + return { captures: response.data.data.captures.map((capture: any) => ({ - overall: capture.overall, - country: capture.country, + value: getCaptureValue(capture, request.type), capturedAt: new Date(capture.captured_at), })), mode: response.data.data.mode, diff --git a/src/components/UserProfileHistoryGraph.tsx b/src/components/UserProfileHistoryGraph.tsx index bebc698..73e9cb0 100644 --- a/src/components/UserProfileHistoryGraph.tsx +++ b/src/components/UserProfileHistoryGraph.tsx @@ -3,8 +3,9 @@ import { AxisOptions, Chart, Datum, Series } from "react-charts" import { GameMode, RelaxMode } from "../gameModes" import { fetchUserProfileHistory, - ProfileHistoryCapture, + ProfileHistoryType, ProfileHistoryResponse, + ProfileHistoryCapture, } from "../adapters/akatsuki-api/profileHistory" import { Alert } from "@mui/material" @@ -15,7 +16,7 @@ export const UserProfileHistoryGraph = ({ relaxMode, }: { userId: number - type: "rank" | "pp" + type: ProfileHistoryType gameMode: GameMode relaxMode: RelaxMode }) => { @@ -28,7 +29,7 @@ export const UserProfileHistoryGraph = ({ ;(async () => { try { const response = await fetchUserProfileHistory({ - type: type, + type, userId, akatsukiMode: gameMode + relaxMode * 4, }) @@ -48,7 +49,7 @@ export const UserProfileHistoryGraph = ({ ? profileHistoryResponse.captures.map( (capture: ProfileHistoryCapture) => ({ date: capture.capturedAt, - performance: capture.overall, + value: capture.value, }) ) : [], @@ -71,7 +72,7 @@ export const UserProfileHistoryGraph = ({ >( () => [ { - getValue: (datum) => datum.performance, + getValue: (datum) => datum.value, elementType: "line", scaleType: "linear", }, diff --git a/src/pages/ProfilePage.tsx b/src/pages/ProfilePage.tsx index 11b4aaf..dbca0b4 100644 --- a/src/pages/ProfilePage.tsx +++ b/src/pages/ProfilePage.tsx @@ -15,6 +15,7 @@ import { UserProfileHistoryGraph } from "../components/UserProfileHistoryGraph" import { UserProfileStats } from "../components/UserProfileStats" import { UserProfileScores } from "../components/UserProfileScores" import { userIsOnline } from "../adapters/bancho" +import { ProfileHistoryType } from "../adapters/akatsuki-api/profileHistory" const modeToStatsIndex = ( mode: GameMode @@ -40,9 +41,8 @@ export const ProfilePage = () => { const [error, setError] = useState("") const [userProfile, setUserProfile] = useState(null) - const [profileHistoryType, setProfileHistoryType] = useState<"rank" | "pp">( - "rank" - ) + const [profileHistoryType, setProfileHistoryType] = + useState(ProfileHistoryType.GlobalRank) const [isOnline, setIsOnline] = useState(false) const [gameMode, setGameMode] = useState(GameMode.Standard) @@ -178,17 +178,35 @@ export const ProfilePage = () => { +