import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { supabase } from "@/integrations/supabase/client"; import { useQuery } from "@tanstack/react-query"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { useToast } from "@/components/ui/use-toast"; import { Pagination, PaginationContent, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, } from "@/components/ui/pagination"; interface HighScore { id: string; player_name: string; score: number; avg_words_per_round: number; created_at: string; } interface HighScoreBoardProps { currentScore: number; avgWordsPerRound: number; onClose: () => void; onPlayAgain: () => void; } const ITEMS_PER_PAGE = 10; const getRankMedal = (rank: number) => { switch (rank) { case 1: return "🥇"; case 2: return "🥈"; case 3: return "🥉"; default: return null; } }; export const HighScoreBoard = ({ currentScore, avgWordsPerRound, onClose, onPlayAgain, }: HighScoreBoardProps) => { const [playerName, setPlayerName] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); const [hasSubmitted, setHasSubmitted] = useState(false); const [currentPage, setCurrentPage] = useState(1); const { toast } = useToast(); const { data: highScores, refetch } = useQuery({ queryKey: ["highScores"], queryFn: async () => { const { data, error } = await supabase .from("high_scores") .select("*") .order("score", { ascending: false }) .order("avg_words_per_round", { ascending: true }); if (error) throw error; return data as HighScore[]; }, }); const handleSubmitScore = async () => { if (!playerName.trim()) { toast({ title: "Error", description: "Please enter your name", variant: "destructive", }); return; } if (currentScore < 1) { toast({ title: "Error", description: "You need to complete at least one round to submit a score", variant: "destructive", }); return; } if (hasSubmitted) { toast({ title: "Error", description: "You have already submitted your score for this game", variant: "destructive", }); return; } setIsSubmitting(true); try { const { error } = await supabase.from("high_scores").insert({ player_name: playerName.trim(), score: currentScore, avg_words_per_round: avgWordsPerRound, }); if (error) throw error; toast({ title: "Success!", description: "Your score has been recorded", }); setHasSubmitted(true); await refetch(); setPlayerName(""); } catch (error) { console.error("Error submitting score:", error); toast({ title: "Error", description: "Failed to submit score. Please try again.", variant: "destructive", }); } finally { setIsSubmitting(false); } }; const totalPages = highScores ? Math.ceil(highScores.length / ITEMS_PER_PAGE) : 0; const startIndex = (currentPage - 1) * ITEMS_PER_PAGE; const paginatedScores = highScores?.slice(startIndex, startIndex + ITEMS_PER_PAGE); const handlePreviousPage = () => { if (currentPage > 1) { setCurrentPage(p => p - 1); } }; const handleNextPage = () => { if (currentPage < totalPages) { setCurrentPage(p => p + 1); } }; return (
Your score: {currentScore} rounds {currentScore > 0 && ` (${avgWordsPerRound.toFixed(1)} words/round)`}