import { useState, useEffect } from "react"; import { motion } from "framer-motion"; import { useTranslation } from "@/hooks/useTranslation"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Copy, Home } from "lucide-react"; import { Dialog, DialogContent, DialogTrigger, } from "@/components/ui/dialog"; import { HighScoreBoard } from "@/components/HighScoreBoard"; import { GameDetailsView } from "@/components/admin/GameDetailsView"; import { supabase } from "@/integrations/supabase/client"; import { useToast } from "@/components/ui/use-toast"; import { useSearchParams, useNavigate } from "react-router-dom"; import { RoundHeader } from "./sentence-builder/RoundHeader"; interface GameReviewProps { currentScore: number; wrongGuesses: number; totalRounds: number; avgWordsPerRound: number; onPlayAgain: (game_id?: string, fromSession?: string) => void; onBack?: () => void; gameId?: string; sessionId: string; currentTheme: string; fromSession?: string | null; words: string[]; } export const GameReview = ({ currentScore, wrongGuesses, totalRounds, avgWordsPerRound, onPlayAgain, onBack, gameId, sessionId, currentTheme, fromSession, words, }: GameReviewProps) => { const t = useTranslation(); const { toast } = useToast(); const [searchParams] = useSearchParams(); const navigate = useNavigate(); const [showHighScores, setShowHighScores] = useState(false); const [gameResults, setGameResults] = useState([]); const [friendData, setFriendData] = useState<{ score: number; avgWords: number } | null>(null); const [showConfirmDialog, setShowConfirmDialog] = useState(false); const shareUrl = `${window.location.origin}/?from_session=${sessionId}`; useEffect(() => { const fetchGameResults = async () => { const { data, error } = await supabase .from('game_results') .select('*') .eq('session_id', sessionId) .order('created_at', { ascending: true }); if (!error && data) { setGameResults(data); } }; const fetchFriendResults = async () => { if (!fromSession) return; const { data: friendResults, error } = await supabase .from('game_results') .select('target_word, is_correct, description, ai_guess') .eq('session_id', fromSession); if (error) { console.error('Error fetching friend results:', error); return; } if (friendResults) { const successfulRounds = friendResults.filter(r => r.is_correct).length; const totalWords = friendResults.reduce((acc, r) => acc + (r.description?.split(' ').length || 0), 0); const avgWords = successfulRounds > 0 ? totalWords / successfulRounds : 0; setFriendData({ score: successfulRounds, avgWords: avgWords }); } }; fetchGameResults(); if (fromSession) { fetchFriendResults(); } }, [sessionId, fromSession]); useEffect(() => { const handleKeyPress = (e: KeyboardEvent) => { // Only handle Enter key if high scores dialog is not open if (e.key === 'Enter' && !showHighScores) { handlePlayAgain(); } }; window.addEventListener('keydown', handleKeyPress); return () => window.removeEventListener('keydown', handleKeyPress); }, [showHighScores]); // Add showHighScores to dependencies const handleCopyUrl = async () => { try { await navigator.clipboard.writeText(shareUrl); toast({ title: t.game.review.urlCopied, description: t.game.review.urlCopiedDesc, }); } catch (err) { console.error('Failed to copy URL:', err); toast({ title: t.game.review.urlCopyError, description: t.game.review.urlCopyErrorDesc, variant: "destructive", }); } }; const handlePlayAgain = async () => { try { const { data: session, error } = await supabase .from('sessions') .insert({ game_id: gameId }) .select() .single(); if (error) throw error; onPlayAgain(gameId, fromSession); } catch (error) { console.error('Error creating new session:', error); toast({ title: "Error", description: "Failed to restart the game. Please try again.", variant: "destructive", }); } }; const handlePlayNewWords = async () => { onPlayAgain(); }; const handleHomeClick = () => { if (onBack) { onBack(); } }; const renderComparisonResult = () => { if (!friendData) return null; const didWin = currentScore > friendData.score || (currentScore === friendData.score && avgWordsPerRound < friendData.avgWords); return (
{didWin ? `${t.game.review.youWin} 🎉` : `${t.game.review.youLost} 🧘`}
{t.game.review.friendScore(friendData.score, friendData.avgWords.toFixed(1))}
{t.game.review.avgWords}: {avgWordsPerRound.toFixed(1)}
{t.game.review.urlCopiedDesc}