add wrapper component to check auth

This commit is contained in:
Nathaniel Tampus
2023-03-05 10:00:55 +08:00
parent 01ebc4a138
commit 69fb7a7c9d
3 changed files with 29 additions and 9 deletions

View File

@@ -1,12 +1,12 @@
import GameWrapper from "@/components/game/GameWrapper";
import GameAuthWrapper from "@/components/game/GameAuthWrapper";
import { getGame } from "@/lib/game";
import { notFound } from "next/navigation";
export default async function GamePage({ params }: { params: { code: string } }) {
export default async function Game({ params }: { params: { code: string } }) {
const game = await getGame(params.code);
if (!game) {
notFound();
}
return <GameWrapper initialLobby={game} />;
return <GameAuthWrapper initialLobby={game} />;
}

View File

@@ -0,0 +1,21 @@
"use client";
import { useContext } from "react";
import { SessionContext } from "@/context/session";
import GamePage from "./GamePage";
import type { Game } from "@chessu/types";
export default function GameAuthWrapper({ initialLobby }: { initialLobby: Game }) {
const session = useContext(SessionContext);
if (!session?.user || !session.user?.id) {
return (
<div className="flex flex-col items-center justify-center gap-4">
<div className="text-2xl font-bold">Loading</div>
<div className="text-xl">Waiting for authentication...</div>
</div>
);
}
return <GamePage initialLobby={initialLobby} />;
}

View File

@@ -12,9 +12,9 @@ import { API_URL } from "@/config";
import { SessionContext } from "@/context/session";
import { Chess } from "chess.js";
const socket = io(API_URL, { withCredentials: true, autoConnect: false });
const socket = io(API_URL, { withCredentials: true, autoConnect: true });
export default function GameWrapper({ initialLobby }: { initialLobby: Game }) {
export default function GamePage({ initialLobby }: { initialLobby: Game }) {
const session = useContext(SessionContext);
// TODO: useReducer
@@ -30,7 +30,7 @@ export default function GameWrapper({ initialLobby }: { initialLobby: Game }) {
window.addEventListener("resize", handleResize);
handleResize();
if (game.pgn() !== lobbyData.pgn) {
if (lobbyData.pgn && game.pgn() !== lobbyData.pgn) {
const gameCopy = { ...game } as Chess;
gameCopy.loadPgn(lobbyData.pgn as string);
setGame(gameCopy);
@@ -72,7 +72,7 @@ export default function GameWrapper({ initialLobby }: { initialLobby: Game }) {
socket.off("receivedLatestLobby");
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [session?.user]);
}, []);
function handleResize() {
if (window.innerWidth >= 1920) {
@@ -99,7 +99,6 @@ export default function GameWrapper({ initialLobby }: { initialLobby: Game }) {
<Chessboard
boardWidth={boardWidth}
/* 350 for mobile, 480 for md up (^768px screen), 540 for 2xl up (^1536px), 580 for ^1920px */
customDarkSquareStyle={{ backgroundColor: "#4b7399" }}
customLightSquareStyle={{ backgroundColor: "#eae9d2" }}
position={game.fen()}
@@ -139,7 +138,7 @@ export default function GameWrapper({ initialLobby }: { initialLobby: Game }) {
<div className="h-36 w-full overflow-y-scroll">
<table className="table-compact table w-full ">
<tbody>
{(lobbyData.pgn as string)
{(lobbyData.pgn || "")
.split(/\d+\./)
.filter((move) => move.trim() !== "")
.map((moveSet, i) => {