mancala/apps/frontend/src/routes/Home.tsx

69 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-05-12 23:41:24 +03:00
import * as React from "react";
import { FunctionComponent, useEffect, useState } from "react";
import { LiveGameSummary } from "@mancala/core";
import { AuthUser, Theme } from "@mancala/core";
import AppHeader from "../components/headerbar/AppHeader";
2022-07-14 13:38:00 +03:00
import PageContainer from "../components/PageContainer";
import { PlusIcon } from "../components/Icons";
import ActiveGameBanner from "../components/home/ActiveGameBanner";
import LiveGamesSection from "../components/home/LiveGamesSection";
import RecentGamesSection from "../components/home/RecentGamesSection";
import PopularGamesSection from "../components/home/PopularGamesSection";
import "../components/home/Home.css";
import { Context } from "../context/context";
import { useNavigate } from "react-router-dom";
2022-09-04 01:06:26 +03:00
import Util from "../util/Util";
import { useTranslation } from "react-i18next";
2022-05-12 23:41:24 +03:00
const Home: FunctionComponent<{
context: Context,
userKey?: string,
authUser?: AuthUser,
2022-07-31 00:59:36 +03:00
theme: Theme,
}> = ({ 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
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]);
const { t } = useTranslation();
2022-07-13 22:39:01 +03:00
const onNewGameClick = () => {
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 (
<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>
<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;