import { useLocalStorage } from "@/hooks/useLocalStorage"; import { getLichessUserRecentGames } from "@/lib/lichess"; import { capitalize } from "@/lib/helpers"; import { LichessGame } from "@/types/lichess"; import { CircularProgress, FormControl, Grid2 as Grid, ListItemButton, ListItemText, TextField, } from "@mui/material"; import { useEffect, useState } from "react"; interface Props { onSelect: (pgn: string) => void; } export default function LichessInput({ onSelect }: Props) { const [requestCount, setRequestCount] = useState(0); const [lichessUsername, setLichessUsername] = useLocalStorage( "lichess-username", "" ); const [games, setGames] = useState([]); useEffect(() => { if (!lichessUsername) { setGames([]); return; } const timeout = setTimeout( async () => { const games = await getLichessUserRecentGames(lichessUsername); setGames(games); }, requestCount === 0 ? 0 : 500 ); setRequestCount((prev) => prev + 1); return () => { clearTimeout(timeout); }; }, [lichessUsername]); // eslint-disable-line react-hooks/exhaustive-deps return ( <> setLichessUsername(e.target.value)} /> {lichessUsername && ( {games.map((game) => ( onSelect(game.pgn)} style={{ width: 350, maxWidth: 350 }} key={game.id} > ))} {games.length === 0 && } )} ); }