"use client"; import { useEffect, useState } from "react"; import { Chessboard } from "react-chessboard"; import { Chess } from "chess.js"; import Link from "next/link"; import { API_URL } from "@/config"; import type { Game } from "@michess/types"; function getFen(pgn?: string): string { if (!pgn) return "start"; try { const c = new Chess(); c.loadPgn(pgn); return c.fen(); } catch { return "start"; } } const BOARD_SIZE = 160; export default function RecentGames({ userId }: { userId: number }) { const [games, setGames] = useState([]); useEffect(() => { fetch(`${API_URL}/v1/games?userid=${userId}`, { cache: "no-store" }) .then((r) => (r.ok ? r.json() : [])) .then((data: Game[] | Game) => { if (Array.isArray(data)) setGames(data.slice(0, 3)); }) .catch(() => {}); }, [userId]); if (!games.length) return null; return (

Letzte Spiele

{games.map((game) => { const fen = getFen(game.pgn); const myColor = game.white?.id === userId ? "white" : "black"; const opponent = myColor === "white" ? game.black?.name : game.white?.name; const won = game.winner === myColor; const drew = game.winner === "draw"; return (
false} customDarkSquareStyle={{ backgroundColor: "#4b7399" }} customLightSquareStyle={{ backgroundColor: "#eae9d2" }} areArrowsAllowed={false} />
vs {opponent ?? "?"}
{won ? "Gewonnen" : drew ? "Remis" : "Verloren"}
); })}
); }