"use client"; import { ScoresaberPlayer } from "@/schemas/scoresaber/player"; import { MagnifyingGlassIcon } from "@heroicons/react/20/solid"; import clsx from "clsx"; import { useEffect, useState } from "react"; import Avatar from "./Avatar"; export default function SearchPlayer() { const [search, setSearch] = useState(""); const [players, setPlayers] = useState([] as ScoresaberPlayer[]); useEffect(() => { // Don't search if the query is too short if (search.length < 4) { setPlayers([]); // Clear players return; } searchPlayer(search); }, [search]); function searchPlayer(search: string) { fetch(`/api/player/search?name=${search}`).then(async (reponse) => { const json = await reponse.json(); if (json.error || !json.players) { setPlayers([]); // Clear players } setPlayers(json.players); // Set players }); } function handleSubmit(e: React.FormEvent) { e.preventDefault(); // Take the user to the first account if (players.length > 0) { window.location.href = `/player/${players[0].id}`; } } return (
setSearch(e.target.value)} />
0 ? "flex" : "hidden", )} > {players.map((player: ScoresaberPlayer) => (

{player.name}

))}
); }