import { useState, useEffect, useRef } from 'react'; import { motion } from 'framer-motion'; import { useGameStore } from '../../store/gameStore'; import { Card } from '../game/Card'; import { emitArrangeHand } from '../../socket/socket'; const AUTO_CONFIRM_TIMEOUT = 15000; // 15 seconds interface ArrangeHandModalProps { onClose: () => void; } export function ArrangeHandModal({ onClose }: ArrangeHandModalProps) { const myHand = useGameStore((state) => state.myHand); const [orderedCards, setOrderedCards] = useState(myHand.map(c => c.instanceId)); const [selectedIndex, setSelectedIndex] = useState(null); const [timeLeft, setTimeLeft] = useState(AUTO_CONFIRM_TIMEOUT / 1000); const hasConfirmed = useRef(false); const handleCardClick = (index: number) => { if (selectedIndex === null) { // First card selected setSelectedIndex(index); } else if (selectedIndex === index) { // Clicked same card, deselect setSelectedIndex(null); } else { // Second card selected - swap them const newOrder = [...orderedCards]; [newOrder[selectedIndex], newOrder[index]] = [newOrder[index], newOrder[selectedIndex]]; setOrderedCards(newOrder); setSelectedIndex(null); } }; const handleConfirm = () => { if (hasConfirmed.current) return; hasConfirmed.current = true; emitArrangeHand(orderedCards); onClose(); }; // Auto-confirm timer useEffect(() => { const interval = setInterval(() => { setTimeLeft((prev) => { if (prev <= 1) { clearInterval(interval); handleConfirm(); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(interval); }, []); return ( e.stopPropagation()} >

🔀 Arrange Your Cards

Someone is about to steal from you!

Tap two cards to swap their positions. Hide your good cards!

⏱️ Auto-confirm in {timeLeft}s

{orderedCards.map((instanceId, index) => { const card = myHand.find(c => c.instanceId === instanceId); if (!card) return null; const isSelected = selectedIndex === index; return ( handleCardClick(index)} whileHover={{ scale: 1.05 }} whileTap={{ scale: 0.95 }} animate={isSelected ? { y: -10 } : { y: 0 }} >

#{index + 1}

); })}
{selectedIndex !== null && (

Card #{selectedIndex + 1} selected. Tap another card to swap!

)}
); }