2022-05-12 23:41:24 +03:00
|
|
|
import * as React from "react";
|
|
|
|
|
import { FunctionComponent, useEffect, useState } from "react";
|
2026-10-16 09:35:39 +03:00
|
|
|
import { LiveGameSummary } from "@mancala/core";
|
2026-10-14 02:22:13 +03:00
|
|
|
import { AuthUser, Theme } from "@mancala/core";
|
2026-10-16 09:35:39 +03:00
|
|
|
import AppHeader from "../components/headerbar/AppHeader";
|
2022-07-14 13:38:00 +03:00
|
|
|
import PageContainer from "../components/PageContainer";
|
2026-10-16 09:35:39 +03:00
|
|
|
import { PlusIcon } from "../components/Icons";
|
2026-10-16 00:27:06 +03:00
|
|
|
import ActiveGameBanner from "../components/home/ActiveGameBanner";
|
|
|
|
|
import LiveGamesSection from "../components/home/LiveGamesSection";
|
|
|
|
|
import RecentGamesSection from "../components/home/RecentGamesSection";
|
2026-10-16 10:21:46 +03:00
|
|
|
import PopularGamesSection from "../components/home/PopularGamesSection";
|
|
|
|
|
import "../components/home/Home.css";
|
2022-07-30 12:01:50 +03:00
|
|
|
import { Context } from "../context/context";
|
2026-10-16 09:35:39 +03:00
|
|
|
import { useNavigate } from "react-router-dom";
|
2022-09-04 01:06:26 +03:00
|
|
|
import Util from "../util/Util";
|
2024-06-17 23:50:13 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-05-12 23:41:24 +03:00
|
|
|
|
2022-07-30 12:01:50 +03:00
|
|
|
const Home: FunctionComponent<{
|
|
|
|
|
context: Context,
|
|
|
|
|
userKey?: string,
|
2026-10-14 02:22:13 +03:00
|
|
|
authUser?: AuthUser,
|
2022-07-31 00:59:36 +03:00
|
|
|
theme: Theme,
|
2026-10-14 02:22:13 +03:00
|
|
|
}> = ({ context, userKey, authUser, theme }) => {
|
2021-06-30 19:41:53 +03:00
|
|
|
|
2022-07-31 00:59:36 +03:00
|
|
|
const navigate = useNavigate();
|
2022-06-04 20:54:19 +03:00
|
|
|
|
2026-10-16 00:27:06 +03:00
|
|
|
const [activeGame, setActiveGame] = useState<LiveGameSummary | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!authUser) return;
|
|
|
|
|
let disposed = false;
|
|
|
|
|
context.authService.getAccessToken()
|
|
|
|
|
.then((token) => context.gameStore.getActiveGame(token))
|
|
|
|
|
.then((game) => !disposed && setActiveGame(game))
|
|
|
|
|
.catch((error) => console.error("(Home) Could not load the active game", error));
|
|
|
|
|
return () => { disposed = true; };
|
|
|
|
|
}, [authUser?.id]);
|
|
|
|
|
|
2024-06-17 23:50:13 +03:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2022-07-13 22:39:01 +03:00
|
|
|
const onNewGameClick = () => {
|
2024-06-17 23:50:13 +03:00
|
|
|
if(Util.checkConnectionAndMaybeAlert(context, t)) return;
|
2022-07-31 00:59:36 +03:00
|
|
|
navigate("/loby")
|
2022-05-12 23:41:24 +03:00
|
|
|
};
|
2021-07-02 23:13:38 +03:00
|
|
|
|
2022-05-12 23:41:24 +03:00
|
|
|
return (
|
2026-10-16 09:35:39 +03:00
|
|
|
<PageContainer>
|
|
|
|
|
<AppHeader context={context} authUser={authUser}>
|
|
|
|
|
<button className="btn pri ic-m" aria-label={t("NewGame")} onClick={onNewGameClick}>
|
|
|
|
|
<PlusIcon /><span className="hide-m">{t("NewGame")}</span>
|
|
|
|
|
</button>
|
|
|
|
|
</AppHeader>
|
2026-10-16 10:21:46 +03:00
|
|
|
<main className="wrap">
|
|
|
|
|
{activeGame && authUser && (
|
|
|
|
|
<ActiveGameBanner
|
|
|
|
|
game={activeGame}
|
|
|
|
|
userId={authUser.id}
|
|
|
|
|
onContinue={() => navigate(`/game/${activeGame.id}`)} />
|
|
|
|
|
)}
|
|
|
|
|
<LiveGamesSection context={context} />
|
|
|
|
|
<PopularGamesSection context={context} />
|
|
|
|
|
{authUser && <RecentGamesSection context={context} userId={authUser.id} />}
|
|
|
|
|
</main>
|
2022-07-13 22:39:01 +03:00
|
|
|
</PageContainer>
|
2022-05-12 23:41:24 +03:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Home;
|