import React, { useState } from 'react'; import { Player } from '../types'; import { motion, AnimatePresence } from 'framer-motion'; import { Sparkles, User, X, Link, Check } from 'lucide-react'; import { QRCodeSVG } from 'qrcode.react'; import { PlayerAvatar } from './PlayerAvatar'; import toast from 'react-hot-toast'; interface LobbyProps { quizTitle: string; players: Player[]; gamePin: string | null; role: 'HOST' | 'CLIENT'; onStart: () => void; onEndGame?: () => void; currentPlayerId?: string | null; hostParticipates?: boolean; } export const Lobby: React.FC = ({ quizTitle, players, gamePin, role, onStart, onEndGame, currentPlayerId, hostParticipates = false }) => { const isHost = role === 'HOST'; const hostPlayer = players.find(p => p.id === 'host'); const realPlayers = players.filter(p => p.id !== 'host'); const currentPlayer = currentPlayerId ? players.find(p => p.id === currentPlayerId) : null; const [linkCopied, setLinkCopied] = useState(false); const [isQrModalOpen, setIsQrModalOpen] = useState(false); React.useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') setIsQrModalOpen(false); }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, []); const copyJoinLink = async () => { if (!gamePin) return; const joinUrl = `${window.location.origin}/play/${gamePin}`; await navigator.clipboard.writeText(joinUrl); setLinkCopied(true); toast.success('Join link copied!'); setTimeout(() => setLinkCopied(false), 2000); }; return (
{isHost && gamePin && (
setIsQrModalOpen(true)} className="bg-white p-2 rounded-xl shadow-lg hidden md:block cursor-pointer hover:scale-105 transition-transform" title="Click to expand" >
)}
Game PIN
{gamePin}
{isHost && ( )}
{quizTitle}
Live Lobby
{realPlayers.length} Players
{isHost ? ( <>
{realPlayers.length === 0 && !hostParticipates && (
Waiting for players to join...
)} {hostParticipates && hostPlayer && ( {hostPlayer.name} HOST )} {realPlayers.map((player) => ( {player.name} ))}
{onEndGame && ( )} ) : (
{currentPlayer ? ( ) : ( )}

{currentPlayer?.name || "You're in!"}

Waiting for the host to start...

)}
{isQrModalOpen && ( setIsQrModalOpen(false)} > e.stopPropagation()} >

Scan to Join

Or visit URL
{`${window.location.origin}/play/${gamePin}`}
Tap backdrop or ESC to close
)}
); };