2026-10-16 00:27:06 +03:00
|
|
|
import * as React from "react";
|
|
|
|
|
import { FunctionComponent, useEffect, useState } from "react";
|
|
|
|
|
import {
|
|
|
|
|
channel_on_live_games,
|
|
|
|
|
channel_subscribe_live_games,
|
|
|
|
|
channel_unsubscribe_live_games,
|
|
|
|
|
LiveGameSummary,
|
|
|
|
|
} from "@mancala/core";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { Context } from "../../context/context";
|
2026-10-16 10:21:46 +03:00
|
|
|
import GameCard from "./GameCard";
|
2026-10-16 00:27:06 +03:00
|
|
|
import HomeSection from "./HomeSection";
|
|
|
|
|
|
2026-10-16 10:21:46 +03:00
|
|
|
// Live games, updated as they are played; hidden while nothing is being played.
|
2026-10-16 00:27:06 +03:00
|
|
|
const LiveGamesSection: FunctionComponent<{ context: Context }> = ({ context }) => {
|
2026-10-16 10:21:46 +03:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [games, setGames] = useState<LiveGameSummary[]>([]);
|
2026-10-16 00:27:06 +03:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-16 10:21:46 +03:00
|
|
|
const onLiveGames = (message: { games: LiveGameSummary[] }) => setGames(message.games);
|
2026-10-16 00:27:06 +03:00
|
|
|
// Subscribe whenever the connection is (re)established; the server forgets subscriptions of offline users.
|
|
|
|
|
const subscribe = () => context.rtmt.sendMessage(channel_subscribe_live_games, {});
|
|
|
|
|
context.rtmt.addMessageListener(channel_on_live_games, onLiveGames);
|
|
|
|
|
context.rtmt.on("open", subscribe);
|
|
|
|
|
if (context.rtmt.connectionState === "connected") subscribe();
|
|
|
|
|
return () => {
|
|
|
|
|
context.rtmt.off("open", subscribe);
|
|
|
|
|
context.rtmt.removeMessageListener(channel_on_live_games, onLiveGames);
|
|
|
|
|
if (context.rtmt.connectionState === "connected") context.rtmt.sendMessage(channel_unsubscribe_live_games, {});
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-10-16 10:21:46 +03:00
|
|
|
if (games.length === 0) return <></>;
|
2026-10-16 00:27:06 +03:00
|
|
|
return (
|
2026-10-16 10:21:46 +03:00
|
|
|
<HomeSection title={t("LiveGames")} subtitle={t("BeingPlayedNow")}>
|
|
|
|
|
{games.map((game) => (
|
|
|
|
|
<GameCard
|
2026-10-16 00:27:06 +03:00
|
|
|
key={game.id}
|
|
|
|
|
id={game.id}
|
|
|
|
|
player1={game.player1}
|
|
|
|
|
player2={game.player2}
|
|
|
|
|
board={game.board}
|
2026-10-16 10:21:46 +03:00
|
|
|
status={t("Live")}
|
|
|
|
|
statusKind="live"
|
|
|
|
|
details={`${t("Watching", { count: game.spectatorCount })} · ${t("MoveNumber", { number: game.moveCount })}`} />
|
2026-10-16 00:27:06 +03:00
|
|
|
))}
|
|
|
|
|
</HomeSection>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default LiveGamesSection;
|