import { useState, useEffect, useRef } from 'react'; import { motion } from 'framer-motion'; import { CARD_DATABASE, type CardId } from '@shared/types'; import { useGameStore } from '../../store/gameStore'; import { emitKingRaResponse } from '../../socket/socket'; interface KingRaPromptModalProps { playerId?: string; cardPlayed?: CardId; timeout: number; onClose: () => void; } export function KingRaPromptModal({ playerId, cardPlayed, timeout, onClose }: KingRaPromptModalProps) { const [timeLeft, setTimeLeft] = useState(timeout); const gameState = useGameStore((state) => state.gameState); const myHand = useGameStore((state) => state.myHand); const hasResponded = useRef(false); const isMounted = useRef(true); const hasKingRa = myHand.some(c => c.cardId === 'king_ra_says_no'); console.log('KingRaPromptModal render - myHand:', myHand.map(c => c.cardId), 'hasKingRa:', hasKingRa); const playerName = gameState?.players.find(p => p.id === playerId)?.name ?? 'A player'; const cardDef = cardPlayed ? CARD_DATABASE[cardPlayed] : null; // Track mount state useEffect(() => { isMounted.current = true; hasResponded.current = false; // Reset on mount return () => { isMounted.current = false; }; }, []); useEffect(() => { const interval = setInterval(() => { if (!isMounted.current) { clearInterval(interval); return; } setTimeLeft((prev) => { if (prev <= 100) { clearInterval(interval); // Auto-decline on timeout if (!hasResponded.current && isMounted.current) { hasResponded.current = true; emitKingRaResponse(false); onClose(); } return 0; } return prev - 100; }); }, 100); return () => clearInterval(interval); }, [onClose]); const handleResponse = (useKingRa: boolean) => { console.log('King Ra handleResponse called:', { useKingRa, hasResponded: hasResponded.current, hasKingRa }); if (hasResponded.current) { console.log('Already responded, ignoring'); return; } hasResponded.current = true; console.log('Emitting kingRaResponse:', useKingRa); emitKingRaResponse(useKingRa); onClose(); }; const progressPercent = (timeLeft / timeout) * 100; return ( e.stopPropagation()} >

👑 King Ra Says NO?

{playerName} played:

{cardDef?.nameEn}

{cardDef?.nameAr}

{/* Timer */}

{hasKingRa ? 'Do you want to cancel this action?' : "You don't have King Ra Says NO!"}

); }