diff --git a/src/app/api/ratings-info/[region]/route.ts b/src/app/api/ratings-info/[region]/route.ts new file mode 100644 index 000000000..dcb0bb87f --- /dev/null +++ b/src/app/api/ratings-info/[region]/route.ts @@ -0,0 +1,10 @@ +import { NextResponse } from 'next/server'; +import { Region } from '../../../../constants/regions'; +import getRatingsInfo from '../../../../core/blitz/getRatingsInfo'; + +export async function GET( + request: Request, + { params }: { params: { region: Region } }, +) { + return NextResponse.json(await getRatingsInfo(params.region)); +} diff --git a/src/app/tools/ratings/page.tsx b/src/app/tools/ratings/page.tsx new file mode 100644 index 000000000..eda68e495 --- /dev/null +++ b/src/app/tools/ratings/page.tsx @@ -0,0 +1,76 @@ +'use client'; + +import { Flex, Select } from '@radix-ui/themes'; +import { useEffect, useState } from 'react'; +import { RatingsInfo } from '../../../commands/ratings'; +import PageWrapper from '../../../components/PageWrapper'; +import { Region } from '../../../constants/regions'; +import getRatingsInfo from '../../../core/blitz/getRatingsInfo'; +import { RatingsType, useRatings } from '../../../stores/ratings'; + +export default function Page() { + const ratings = useRatings(); + const [awaitedRatingsInfo, setRatingsInfo] = useState( + null, + ); + + useEffect(() => { + getRatingsInfo(ratings.region).then(setRatingsInfo); + }, [ratings.region]); + + return ( + + + + useRatings.setState({ region: value as Region }) + } + > + + + + North America + Europe + Asia + + + + useRatings.setState({ type: value as RatingsType }) + } + > + + + + League + Neighbors + + + + {ratings.type === 'league' ? ( + + useRatings.setState({ league: parseInt(value) }) + } + > + + + + {awaitedRatingsInfo?.detail === undefined && + awaitedRatingsInfo?.leagues.map((league) => ( + + {league.title} + + ))} + + + ) : ( + '1' + )} + + + ); +} diff --git a/src/constants/tools.ts b/src/constants/tools.ts index c9c0b0241..137b53975 100644 --- a/src/constants/tools.ts +++ b/src/constants/tools.ts @@ -15,7 +15,6 @@ export const TOOLS: Tool[] = [ id: 'ratings', title: 'Ratings', description: 'Current and old season leader boards', - disabled: true, }, { id: 'tankopedia', diff --git a/src/core/blitz/getRatingsInfo.ts b/src/core/blitz/getRatingsInfo.ts index 56588cdac..5527ab044 100644 --- a/src/core/blitz/getRatingsInfo.ts +++ b/src/core/blitz/getRatingsInfo.ts @@ -1,12 +1,15 @@ import { RatingsInfo } from '../../commands/ratings'; import { Region } from '../../constants/regions'; +import { context } from '../blitzkrieg/context'; import regionToRegionSubdomain from './regionToRegionSubdomain'; export default async function getRatingsInfo(region: Region) { const response = await fetch( - `https://${regionToRegionSubdomain( - region, - )}.wotblitz.com/en/api/rating-leaderboards/season/`, + context === 'bot' + ? `https://${regionToRegionSubdomain( + region, + )}.wotblitz.com/en/api/rating-leaderboards/season/` + : `/api/ratings-info/${region}`, ); return (await response.json()) as Promise; diff --git a/src/stores/ratings.ts b/src/stores/ratings.ts new file mode 100644 index 000000000..6a9a1fc21 --- /dev/null +++ b/src/stores/ratings.ts @@ -0,0 +1,32 @@ +import { create } from 'zustand'; +import { devtools, persist } from 'zustand/middleware'; +import { Region } from '../constants/regions'; + +export type RatingsType = 'neighbors' | 'league'; + +interface Ratings { + type: RatingsType; + /** + * When null, the current season is used unless there ain't one in which case + * we do whatever lol + */ + season: number | null; + region: Region; + league: number; +} + +export const useRatings = create()( + devtools( + persist( + (set) => ({ + type: 'league', + season: null, + league: 0, + region: 'com', + }), + { + name: 'ratings', + }, + ), + ), +);