import { useEffect, useRef, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { useGameStore } from '../store/gameStore'; import { emit } from '../lib/socket'; import { leaveGame } from '../lib/leaveGame'; import { displayName } from '../lib/names'; export default function Lobby() { const { gid } = useParams<{ gid: string }>(); const navigate = useNavigate(); const myPlayer = useGameStore((s) => s.myPlayer); const games = useGameStore((s) => s.games); const [showCopied, setShowCopied] = useState(false); const copiedTimeout = useRef>(); const game = games.find((g) => g.gid === gid); const players = game?.players ?? []; const isHost = myPlayer?.order === 0; const canStart = players.length === 4 && isHost; const handleLeave = () => leaveGame(navigate); const handleCopyCode = () => { if (!gid) return; navigator.clipboard.writeText(`${window.location.origin}/lobby/${gid}`); setShowCopied(true); clearTimeout(copiedTimeout.current); copiedTimeout.current = setTimeout(() => setShowCopied(false), 1800); }; useEffect(() => () => clearTimeout(copiedTimeout.current), []); return (
Odkaz na hru skopírovaný

{game?.name ?? 'Hra'}

Kód hry

{gid}

{[0, 1, 2, 3].map((order) => { const p = players.find((pl) => pl.order === order); return (
{p ? (p.is_bot ? '⚙' : '✦') : '○'} {p ? `${displayName(p.name)}${myPlayer?.order === p.order ? ' (ty)' : ''}` : 'Čaká sa…'} {isHost && p?.is_bot && ( )} {isHost && !p && ( )}
); })}
); }