import React, { useState } from "react"; import { styles } from "../styles/styles"; import { stylesTokens } from "../styles/theme"; import { useLanguage } from "../i18n"; export default function GamePickerCard({ games, gameId, setGameId, onOpenHelp, members = [], me, hostUserId, isHost = false, started = false, finished = false, winnerName = "", chipCount = 0, onStartGame, onCancelGame, }) { const { language, t } = useLanguage(); const cur = games.find((x) => x.id === gameId); const [starting, setStarting] = useState(false); const [startError, setStartError] = useState(""); const [cancelling, setCancelling] = useState(false); const renderMemberName = (m) => { const base = ((m.display_name || "").trim() || (m.email || "").trim() || "—"); const isMe = !!(me?.id && String(me.id) === String(m.id)); const isHost = !!(hostUserId && String(hostUserId) === String(m.id)); const suffix = `${isHost ? " " : ""}${isMe ? " (du)" : ""}`; return base + suffix; }; const pillStyle = (isHost, isMe) => ({ padding: "7px 10px", borderRadius: 999, border: `1px solid ${ isHost ? "rgba(233,216,166,0.35)" : "rgba(233,216,166,0.16)" }`, background: isHost ? "linear-gradient(180deg, rgba(233,216,166,0.14), rgba(10,10,12,0.35))" : "rgba(10,10,12,0.30)", color: stylesTokens.textMain, fontSize: 13, fontWeight: 950, boxShadow: isHost ? "0 8px 18px rgba(0,0,0,0.25)" : "none", opacity: isMe ? 1 : 0.95, display: "inline-flex", alignItems: "center", gap: 6, whiteSpace: "nowrap", }); const handleStartGame = async () => { if (!onStartGame || starting || members.length < 2) return; if (!window.confirm(language === "en" ? "Start the game now? No more players can join afterwards." : "Spiel jetzt starten? Danach können keine weiteren Spieler beitreten.")) return; setStarting(true); setStartError(""); try { await onStartGame(); } catch (e) { setStartError(e?.message || (language === "en" ? "The game could not be started." : "Das Spiel konnte nicht gestartet werden.")); } finally { setStarting(false); } }; const handleCancelGame = async () => { if (!onCancelGame || cancelling) return; const confirmed = window.confirm(language === "en" ? "Cancel and delete this game? All game data will be removed." : "Dieses Spiel wirklich abbrechen und löschen? Alle Spieldaten werden entfernt."); if (!confirmed) return; setCancelling(true); setStartError(""); try { await onCancelGame(); } catch (e) { setStartError(e?.message || (language === "en" ? "The game could not be cancelled." : "Das Spiel konnte nicht abgebrochen werden.")); } finally { setCancelling(false); } }; return (
{t("game")}
{/* Code Zeile */} {cur?.code && (
Code: {cur.code}
)} {!started ? (
{t("lobby")}
{isHost ? (language === "en" ? "You are the host of this game." : "Du bist der Host dieses Spiels.") : (language === "en" ? "You joined the lobby." : "Du bist der Lobby beigetreten.")}
{members.length}
{t("players")}
{members.length > 0 ? members.map((m, index) => { const isMe = String(me?.id) === String(m.id); const isMemberHost = String(hostUserId) === String(m.id); const name = ((m.display_name || "").trim() || (m.email || "").trim() || t("players")); return (
{index + 1} {name}{isMe ? " (du)" : ""} {isMemberHost && 👑}
); }) : (
{language === "en" ? "Waiting for players …" : "Warte auf Spieler …"}
)}
{isHost ? ( <>
{members.length < 2 ? t("atLeastTwoPlayers") : t("chipsOnStart")}
) : (
{t("waitingForHost")}
)} {startError &&
{startError}
}
) : ( <>
{finished ? `🏆 ${t("finished")}${winnerName ? ` · ${t("winner")}: ${winnerName}` : ""}` : `✓ ${t("started")} · ${chipCount} ${t("chipsCreated")}`}
{isHost && !finished && } )} {/* Spieler */} {started && members?.length > 0 && (
{t("players")}
{members.length}
{members.map((m) => { const isMe = !!(me?.id && String(me.id) === String(m.id)); const isHost = !!(hostUserId && String(hostUserId) === String(m.id)); const label = renderMemberName(m); return (
{isHost && 👑} {label.replace(" 👑", "")}
); })}
👑 = Host
)}
); }