'use client'; import { useCallback, useState } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { GAME_TYPES, createGame, joinGame, listGames, saveSession } from './api'; import useTicker from './useTicker'; const STATUS_STYLE = { WAITING: 'bg-amber-500/20 text-amber-300 border-amber-500/40', PLAYING: 'bg-blue-500/20 text-blue-300 border-blue-500/40', CLOSED: 'bg-neutral-700/40 text-neutral-400 border-neutral-600', }; export default function MultiplayerLobby() { const router = useRouter(); const [games, setGames] = useState(null); const [listError, setListError] = useState(''); const [showCreate, setShowCreate] = useState(false); const [joining, setJoining] = useState(null); const [joinError, setJoinError] = useState(''); const refresh = useCallback(async () => { try { setGames(await listGames()); setListError(''); } catch (err) { setListError(err.message || 'Cannot reach the server'); } }, []); useTicker(refresh, 3000); const enterGame = (res) => { saveSession(res.gameId, res.playerToken); router.push('/multiplayer/play'); }; const handleJoin = async (id) => { setJoining(id); setJoinError(''); try { enterGame(await joinGame(id)); } catch (err) { setJoinError(err.message); refresh(); } finally { setJoining(null); } }; return (
← Home Multiplayer

Games

{listError &&

{listError}

} {joinError &&

{joinError}

} {games === null && !listError &&

Loading…

} {games?.length === 0 && (

No games yet. Create one and invite a friend!

)}
{showCreate && setShowCreate(false)} onCreated={enterGame} />}
); } function CreateGameModal({ onClose, onCreated }) { const [name, setName] = useState(''); const [digits, setDigits] = useState('4'); const [type, setType] = useState('AUTO_GUESSTURES'); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const digitNum = parseInt(digits, 10); const digitsValid = !isNaN(digitNum) && digitNum >= 3 && digitNum <= 10; const valid = name.trim() !== '' && digitsValid; const handleSubmit = async (e) => { e.preventDefault(); if (!valid) return; setLoading(true); setError(''); try { onCreated(await createGame(name.trim(), digitNum, type)); } catch (err) { setError(err.message); setLoading(false); } }; return (
e.stopPropagation()} className="bg-neutral-800 rounded-xl p-4 sm:p-6 w-full max-w-md flex flex-col gap-4" >

Create New Game

Game Type {Object.entries(GAME_TYPES).map(([key, t]) => ( ))}
{error &&

{error}

}
); }