"use client"; import { useEffect, useState } from "react"; import { useParams } from 'next/navigation'; import { socket } from "@/socket"; import Card from '@/components/Card'; import type { GameCard, GameUpdate, ClientUpdate } from '@/types'; export default function GamePage() { const { gameID: gameIDParam } = useParams(); const [gameID, setGameID] = useState(''); const [cards, setCards] = useState([]); useEffect(() => { if (gameIDParam) { setGameID(Array.isArray(gameIDParam) ? gameIDParam[0] : gameIDParam); } }, [gameIDParam]) useEffect(() => { if (gameID) { socket.emit('join', gameID); socket.on('init', (data: GameUpdate) => { console.log('init', data); setCards(data.cards); }); socket.on('card-flipped', (data: GameUpdate) => { console.log('>>>', data); setCards(data.cards); }); } return gameID ? () => { socket.off('init'); socket.off('card-flipped'); } : undefined; }, [gameID]); const flipCard = (cardID: string) => { const flip: ClientUpdate = { gameID, cardID, }; socket.emit('flip-card', flip); }; return cards.length ? (

Game ID: {gameID}

) : null; }