'use client'; import Image from "next/image"; import { useEffect, useRef, useState } from "react"; import io from 'socket.io-client'; export default function Home() { const nameInputRef = useRef(null); const currentPlayers = useState(null); const [gameStatus, setGameStatus] = useState<{ gameActive: boolean, players: any[] }>({ gameActive: false, players: [], }); let baseUrl = ""; if (typeof window !== "undefined") { baseUrl = `${window.location.protocol}//${window.location.hostname}`; } const backendUrl = `${baseUrl}:${process.env.NEXT_PUBLIC_API_BASE}`; const socket = io(`${backendUrl}`); const fetchStatus = async () => { try { const res = await fetch(`${backendUrl}/status`); const data = await res.json(); setGameStatus(data); } catch (err) { console.error("Failed to fetch status", err); } }; // useEffect(() => { // fetchStatus(); // // Need to change this so it just listens on a socket instead // const interval = setInterval(fetchStatus, 5000); // return () => clearInterval(interval); // }, []); useEffect(() => { socket.on('connect', () => { console.log('Connected to Socket.IO server'); fetchStatus(); }); socket.on('player_update', (data: { nextPlayer: string }) => { fetchStatus(); }); return () => { socket.off('player_update'); }; }, []); const addPlayer = async () => { const playerName = nameInputRef.current?.value?.trim(); if (!playerName) { alert("Please enter a player name"); return; } try { console.log(`${process.env.NEXT_PUBLIC_API_BASE}`); const res = await fetch(`${backendUrl}/add`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: playerName, group: selectedGroup }), }); if (!res.ok) { throw new Error(`Server error: ${res.statusText}`); } if (nameInputRef.current) { nameInputRef.current.value = ""; } } catch (error) { console.error("Failed to add player:", error); } }; const startGame = async() => { fetch(`${backendUrl}/start`); } const [selectedGroup, setSelectedGroup] = useState<"stripes" | "solids">("stripes"); const advanceTurn = async () => { await fetch(`${backendUrl}/next`); // triggers backend to emit event }; const resetGame = async () => { await fetch(`${backendUrl}/reset`); // triggers backend to emit event }; return (

🎯 Game Info

Game Active: {gameStatus.gameActive ? "Yes" : "No"}

{gameStatus.players.map((player, idx) => ( ))}
PlayerGroup
{player.name} {player.group}
); }