import { useQuery } from "@tanstack/react-query"; import { supabase } from "@/integrations/supabase/client"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Eye } from "lucide-react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { GameDetailsView } from "./GameDetailsView"; interface HighScoreWithGames { id: string; player_name: string; score: number; avg_words_per_round: number; created_at: string; session_id: string; theme: string; game_results: { id: string; target_word: string; description: string; ai_guess: string; is_correct: boolean; }[]; } export const AdminHighScoresTable = () => { const { data: highScores, isLoading } = useQuery({ queryKey: ["adminHighScores"], queryFn: async () => { console.log("Fetching high scores..."); // First fetch high scores const { data: highScoresData, error: highScoresError } = await supabase .from("high_scores") .select("*") .order("score", { ascending: false }); if (highScoresError) { console.error("Error fetching high scores:", highScoresError); throw highScoresError; } // Then fetch game results for each high score const highScoresWithGames = await Promise.all( highScoresData.map(async (score) => { const { data: gameResults, error: gameResultsError } = await supabase .from("game_results") .select("id, target_word, description, ai_guess, is_correct") .eq("session_id", score.session_id); if (gameResultsError) { console.error("Error fetching game results:", gameResultsError); return { ...score, game_results: [], }; } return { ...score, game_results: gameResults || [], }; }) ); console.log("Fetched high scores with game results:", highScoresWithGames); return highScoresWithGames as HighScoreWithGames[]; }, }); if (isLoading) { return
Loading...
; } return (
Player Score Avg Words/Round Theme Date Details {highScores?.map((score) => ( {score.player_name} {score.score} {score.avg_words_per_round.toFixed(1)} {score.theme} {new Date(score.created_at).toLocaleDateString()} Game Details for {score.player_name} ))}
); };