import { useState, useRef } from 'react'; import { motion } from 'framer-motion'; import { Card } from '../game/Card'; import { emitRearrangeTopCards } from '../../socket/socket'; import type { CardInstance } from '@shared/types'; interface FlipTableModalProps { cards: CardInstance[]; onClose: () => void; } export function FlipTableModal({ cards, onClose }: FlipTableModalProps) { const [orderedCards, setOrderedCards] = useState(cards.map(c => c.instanceId)); const [selectedIndex, setSelectedIndex] = useState(null); const hasConfirmed = useRef(false); const handleCardClick = (index: number) => { if (selectedIndex === null) { setSelectedIndex(index); } else if (selectedIndex === index) { setSelectedIndex(null); } else { // Swap cards const newOrder = [...orderedCards]; [newOrder[selectedIndex], newOrder[index]] = [newOrder[index], newOrder[selectedIndex]]; setOrderedCards(newOrder); setSelectedIndex(null); } }; const handleConfirm = () => { if (hasConfirmed.current) return; hasConfirmed.current = true; emitRearrangeTopCards(orderedCards); onClose(); }; const getCardByInstanceId = (instanceId: string) => { return cards.find(c => c.instanceId === instanceId); }; return ( e.stopPropagation()} >

🔄 Flip the Table

Rearrange the top {cards.length} cards of the deck

Tap two cards to swap positions. Position 1 will be drawn first.

{orderedCards.map((instanceId, index) => { const card = getCardByInstanceId(instanceId); if (!card) return null; const isMummy = card.cardId === 'mummified'; const isSelected = selectedIndex === index; return ( handleCardClick(index)} whileHover={{ scale: 1.05 }} whileTap={{ scale: 0.95 }} animate={isSelected ? { y: -10 } : { y: 0 }} > {index + 1} {isMummy && ( 💀 )} ); })}
{selectedIndex !== null && (

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

)}

💡 Tip: Push mummies to higher positions (drawn later)

); }