From 45fa9d2f057728d9e6ee8a2a4ca9c443b436efc2 Mon Sep 17 00:00:00 2001 From: Kevin Thomas Date: Mon, 18 Nov 2024 19:33:31 -0800 Subject: [PATCH] feat: add play, hand, brain games to profile page --- src/api/analysis/analysis.ts | 12 ++ src/components/UserProfile/GameList.tsx | 154 +++++++++++++++++---- src/components/UserProfile/UserProfile.tsx | 2 +- src/types/analysis/index.ts | 6 + 4 files changed, 146 insertions(+), 28 deletions(-) diff --git a/src/api/analysis/analysis.ts b/src/api/analysis/analysis.ts index e3efc67..e2b99f1 100644 --- a/src/api/analysis/analysis.ts +++ b/src/api/analysis/analysis.ts @@ -49,6 +49,18 @@ export const getAnalysisList = async (): Promise< return data } +export const getAnalysisGameList = async (type = 'play', page = 1) => { + const res = await fetch(buildUrl(`analysis/user/list/${type}/${page}`)) + + if (res.status === 401) { + throw new Error('Unauthorized') + } + + const data = await res.json() + + return data +} + export const getLichessGames = async ( username: string, onMessage: (data: any) => void, diff --git a/src/components/UserProfile/GameList.tsx b/src/components/UserProfile/GameList.tsx index 07b6803..6c1b325 100644 --- a/src/components/UserProfile/GameList.tsx +++ b/src/components/UserProfile/GameList.tsx @@ -1,32 +1,64 @@ +import { motion } from 'framer-motion' import { AuthContext } from 'src/contexts' import React, { useState, useEffect, useContext } from 'react' -import { getLichessGames } from 'src/api' -import { AnalysisLichessGame } from 'src/types' +import { getLichessGames, getAnalysisGameList } from 'src/api' +import { AnalysisLichessGame, AnalysisWebGame } from 'src/types' export default function GameList() { const { user } = useContext(AuthContext) - const [games, setGames] = useState([]) + const [selected, setSelected] = useState< + 'play' | 'hand' | 'brain' | 'lichess' + >('play') + const [games, setGames] = useState([]) + const [playGames, setPlayGames] = useState([]) + const [handGames, setHandGames] = useState([]) + const [brainGames, setBrainGames] = useState([]) + + useEffect(() => { + if (user?.lichessId) { + const playRequest = getAnalysisGameList('play', 1) + const handRequest = getAnalysisGameList('hand', 1) + const brainRequest = getAnalysisGameList('brain', 1) + + Promise.all([playRequest, handRequest, brainRequest]).then((data) => { + const [play, hand, brain] = data + + const parse = (game: { + game_id: string + maia_name: string + result: string + player_color: 'white' | 'black' + }) => { + const raw = game.maia_name.replace('_kdd_', ' ') + const maia = raw.charAt(0).toUpperCase() + raw.slice(1) + + return { + id: game.game_id, + label: + game.player_color === 'white' + ? `You vs. ${maia}` + : `${maia} vs. You`, + result: game.result, + } + } + + setPlayGames(play.games.map(parse)) + setHandGames(hand.games.map(parse)) + setBrainGames(brain.games.map(parse)) + }) + } + }, [user?.lichessId]) useEffect(() => { if (user?.lichessId) { getLichessGames(user?.lichessId, (data) => { - const playerColor = - data.players.white.user?.id == user?.lichessId ? 'white' : 'black' const result = data.pgn.match(/\[Result\s+"(.+?)"\]/)[1] || '?' const game = { id: data.id, - white: - playerColor == 'white' - ? 'You' - : data.players.white.user?.id || 'Anonymous', - black: - playerColor == 'black' - ? 'You' - : data.players.black.user?.id || 'Anonymous', + label: `${data.players.white.user?.id || 'Unknown'} vs. ${data.players.black.user?.id || 'Unknown'}`, result: result, - pgn: data.pgn, } setGames((x) => [...x, game]) @@ -35,26 +67,56 @@ export default function GameList() { }, [user?.lichessId]) return ( -
-
-

YOUR GAMES

+
+
+

Your Games

- -
- {games.map((game, index) => ( +
+
+
+
+
+
+
+ {(selected === 'play' + ? playGames + : selected === 'hand' + ? handGames + : selected === 'brain' + ? brainGames + : games + ).map((game, index) => ( - ) } + +function Header({ + name, + label, + selected, + setSelected, +}: { + label: string + name: 'play' | 'hand' | 'brain' | 'lichess' + selected: 'play' | 'hand' | 'brain' | 'lichess' + setSelected: (name: 'play' | 'hand' | 'brain' | 'lichess') => void +}) { + return ( + + ) +} diff --git a/src/components/UserProfile/UserProfile.tsx b/src/components/UserProfile/UserProfile.tsx index 93bbae2..596c0e9 100644 --- a/src/components/UserProfile/UserProfile.tsx +++ b/src/components/UserProfile/UserProfile.tsx @@ -80,7 +80,7 @@ const UserProfile: React.FC = () => {
{UserIcon}

{user?.displayName}

-
+