import { useLocalStorage } from "@/hooks/useLocalStorage"; import { getLichessUserRecentGames } from "@/lib/lichess"; import { CircularProgress, FormControl, Grid2 as Grid, TextField, List, Autocomplete, } from "@mui/material"; import { Icon } from "@iconify/react"; import { useDebounce } from "@/hooks/useDebounce"; import { useQuery } from "@tanstack/react-query"; import { useMemo, useState } from "react"; import { GameItem } from "./gameItem"; interface Props { onSelect: (pgn: string, boardOrientation?: boolean) => void; } export default function LichessInput({ onSelect }: Props) { const [rawStoredValue, setStoredValues] = useLocalStorage( "lichess-username", "" ); const [lichessUsername, setLichessUsername] = useState(""); const [hasEdited, setHasEdited] = useState(false); const storedValues = useMemo(() => { if (typeof rawStoredValue === "string") { return rawStoredValue .split(",") .map((s) => s.trim()) .filter(Boolean); } return []; }, [rawStoredValue]); if ( !hasEdited && storedValues.length && lichessUsername.trim().toLowerCase() != storedValues[0].trim().toLowerCase() ) { setLichessUsername(storedValues[0].trim()); } const updateHistory = (username: string) => { const trimmed = username.trim(); if (!trimmed) return; const lower = trimmed.toLowerCase(); const exists = storedValues.some((u) => u.toLowerCase() === lower); if (!exists) { const updated = [trimmed, ...storedValues.slice(0, 7)]; setStoredValues(updated.join(",")); } }; const deleteUsername = (usernameToDelete: string) => { const updated = storedValues.filter((u) => u !== usernameToDelete); setStoredValues(updated.join(",")); }; const handleChange = (_: React.SyntheticEvent, newValue: string | null) => { const newInputValue = newValue ?? ""; setLichessUsername(newInputValue.trim()); setHasEdited(true); }; const debouncedUsername = useDebounce(lichessUsername, 500); const { data: games, isFetching, isError, } = useQuery({ queryKey: ["LichessUserGames", debouncedUsername], enabled: !!debouncedUsername, queryFn: ({ signal }) => getLichessUserRecentGames(debouncedUsername ?? "", signal), retry: 1, }); return ( <> { const { key, ...rest } = props; return (
  • {option} { e.stopPropagation(); deleteUsername(option); }} />
  • ); }} renderInput={(params) => ( )} />
    {debouncedUsername && ( {isFetching ? ( ) : isError ? ( User not found. Please check your username. ) : !games?.length ? ( No games found. Please check your username. ) : ( {games.map((game) => { const perspectiveUserColor = game.white.name.toLowerCase() === debouncedUsername.toLowerCase() ? "white" : "black"; return ( { const boardOrientation = debouncedUsername.toLowerCase() !== game.black.name.toLowerCase(); onSelect(game.pgn, boardOrientation); updateHistory(debouncedUsername); }} /> ); })} )} )} ); }