"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter, useParams } from "next/navigation"; import RoomConnector from "../../components/RoomConnector"; import PlayerList from "../../components/PlayerList"; import ChatBox from "../../components/ChatBox"; import { getSocket } from "../../lib/socket-client"; import { useGame } from "../../lib/store"; export default function LobbyPage() { const params = useParams<{ code: string }>(); const code = String(params.code || "").toUpperCase(); const router = useRouter(); const room = useGame((s) => s.room); const myId = useGame((s) => s.myId); const [copied, setCopied] = useState(false); const [err, setErr] = useState(""); useEffect(() => { if (room && room.phase === "playing") router.push(`/room/${code}/play`); if (room && room.phase === "results") router.push(`/room/${code}/results`); }, [room?.phase, code, router]); const isHost = room && myId && room.hostId === myId; const shareUrl = typeof window !== "undefined" ? `${window.location.origin}/join?code=${code}` : ""; const start = () => { setErr(""); getSocket().emit("game:start", null, (resp: any) => { if (!resp?.ok) setErr(resp?.error || "could not start"); }); }; const copy = async () => { try { await navigator.clipboard.writeText(shareUrl); setCopied(true); setTimeout(()=>setCopied(false), 1500); } catch {} }; return (