import { ScoresaberPlayer } from "@/schemas/scoresaber/player"; import { usePlayerScoresStore } from "@/store/playerScoresStore"; import { useSettingsStore } from "@/store/settingsStore"; import { formatNumber } from "@/utils/number"; import { calcPpBoundary, getAveragePp, getHighestPpPlay, } from "@/utils/scoresaber/scores"; import { GlobeAsiaAustraliaIcon, HomeIcon, UserIcon, } from "@heroicons/react/20/solid"; import { useRef } from "react"; import ReactCountryFlag from "react-country-flag"; import { toast } from "react-toastify"; import { useStore } from "zustand"; import Avatar from "../Avatar"; import Card from "../Card"; import Label from "../Label"; import PlayerChart from "./PlayerChart"; type PlayerInfoProps = { playerData: ScoresaberPlayer; }; export default function PlayerInfo({ playerData }: PlayerInfoProps) { const playerId = playerData.id; const settingsStore = useStore(useSettingsStore, (store) => store); const playerScoreStore = useStore(usePlayerScoresStore, (store) => store); // Whether we have scores for this player in the local database const hasLocalScores = playerScoreStore?.exists(playerId); const toastId: any = useRef(null); async function claimProfile() { settingsStore?.setUserId(playerId); addProfile(false); } async function addFriend() { const friend = await settingsStore?.addFriend(playerData.id); if (!friend) { toast.error(`Failed to add ${playerData.name} as a friend`); return; } addProfile(true); } async function addProfile(isFriend: boolean) { if (!usePlayerScoresStore.getState().exists(playerId)) { const reponse = await playerScoreStore?.addPlayer( playerId, (page, totalPages) => { const autoClose = page == totalPages ? 5000 : false; if (page == 1) { toastId.current = toast.info( `Fetching scores ${page}/${totalPages}`, { autoClose: autoClose, progress: page / totalPages, }, ); } else { toast.update(toastId.current, { progress: page / totalPages, render: `Fetching scores ${page}/${totalPages}`, autoClose: autoClose, }); } console.log( `Fetching scores for ${playerId} (${page}/${totalPages})`, ); }, ); if (reponse?.error) { toast.error("Failed to fetch scores"); console.log(reponse.message); return; } } if (!isFriend) { toast.success(`Successfully set ${playerData.name} as your profile`); } else { toast.success(`Successfully added ${playerData.name} as a friend`); } } const isOwnProfile = settingsStore?.userId == playerId; return ( {/* Player Info */}
{/* Avatar */}
{/* Settings Buttons */}
{!isOwnProfile && ( )} {!settingsStore?.isFriend(playerId) && !isOwnProfile && ( )}
{/* Name */}

{playerData.name}

{/* Global Rank */}

#{playerData.rank}

{/* Country Rank */}

#{playerData.countryRank}

{/* PP */}

{formatNumber(playerData.pp)}pp

{/* Labels */}
{/* Chart */}
); }