From 9ca2d7b5adbebf1132e8a82b8b7774b1205aff54 Mon Sep 17 00:00:00 2001 From: Nathaniel Tampus Date: Sun, 5 Mar 2023 00:36:49 +0800 Subject: [PATCH] wrap game page in server component for initial data fetch --- client/src/app/game/[code]/not-found.tsx | 8 ++ client/src/app/game/[code]/page.tsx | 105 ++-------------------- client/src/components/game/Game.tsx | 109 +++++++++++++++++++++++ 3 files changed, 126 insertions(+), 96 deletions(-) create mode 100644 client/src/app/game/[code]/not-found.tsx create mode 100644 client/src/components/game/Game.tsx diff --git a/client/src/app/game/[code]/not-found.tsx b/client/src/app/game/[code]/not-found.tsx new file mode 100644 index 0000000..d49494f --- /dev/null +++ b/client/src/app/game/[code]/not-found.tsx @@ -0,0 +1,8 @@ +export default function NotFound() { + return ( +
+
Error
+
Game not found
+
+ ); +} diff --git a/client/src/app/game/[code]/page.tsx b/client/src/app/game/[code]/page.tsx index 134ae52..8345876 100644 --- a/client/src/app/game/[code]/page.tsx +++ b/client/src/app/game/[code]/page.tsx @@ -1,99 +1,12 @@ -"use client"; -// all of this in a client component for now to easily handle socket events +import Game from "@/components/game/Game"; +import { getGame } from "@/lib/game"; +import { notFound } from "next/navigation"; -import { Chessboard } from "react-chessboard"; -import { IconCopy } from "@tabler/icons-react"; -import Image from "next/image"; +export default async function GamePage({ params }: { params: { code: string } }) { + const game = await getGame(params.code); + if (!game) { + notFound(); + } -const pgn = "1. e4 e5 2. f3 Nf6 3. d4 exd4 4. Qxd4 Nxe4 5. Qxe4+ Qe7 6. Qxe7+"; - -export default function Game() { - return ( -
-
- {/* overlay */} -
-
- Waiting for opponent. - -
-
- - -
- -
-
-
-
-
-
- avatar -
-
- nize -
-
vs
-
-
-
- avatar -
-
- notnize -
-
- -
-
- Invite friends: -
- - ches.su/game/123abc -
-
-
- - - {pgn - .split(/\d+\./) - .filter((move) => move.trim() !== "") - .map((moveSet, i) => { - const moves = moveSet.trim().split(" "); - return ( - - - - - - ); - })} - -
{i + 1}.{moves[0]}{moves[1]}
-
-
-
- -
-
- chatbox -
- - -
-
-
-
Spectators: nize, nize, nize
-
-
- ); + return ; } diff --git a/client/src/components/game/Game.tsx b/client/src/components/game/Game.tsx new file mode 100644 index 0000000..46602af --- /dev/null +++ b/client/src/components/game/Game.tsx @@ -0,0 +1,109 @@ +"use client"; +// all of this in a client component for now to easily handle socket events + +import { Chessboard } from "react-chessboard"; +import { IconCopy } from "@tabler/icons-react"; +import { useState, useEffect } from "react"; +import Image from "next/image"; +import { Game } from "@chessu/types"; + +import { io } from "socket.io-client"; +import { API_URL } from "@/config"; + +const socket = io(API_URL, { withCredentials: true, autoConnect: true }); + +const pgn = "1. e4 e5 2. f3 Nf6 3. d4 exd4 4. Qxd4 Nxe4 5. Qxe4+ Qe7 6. Qxe7+"; + +export default function GameWrapper({ initialLobby }: { initialLobby: Game }) { + const [lobbyData, setLobbyData] = useState(initialLobby); + // TODO + + return ( +
+
+ {/* overlay */} +
+
+ Waiting for opponent. + +
+
+ + +
+ +
+
+
+
+
+
+ avatar +
+
+ nize +
+
vs
+
+
+
+ avatar +
+
+ notnize +
+
+ +
+
+ Invite friends: +
+ + ches.su/game/{initialLobby.code} +
+
+
+ + + {pgn + .split(/\d+\./) + .filter((move) => move.trim() !== "") + .map((moveSet, i) => { + const moves = moveSet.trim().split(" "); + return ( + + + + + + ); + })} + +
{i + 1}.{moves[0]}{moves[1]}
+
+
+
+ +
+
+ chatbox +
+ + +
+
+
+
Spectators: nize, nize, nize
+
+
+ ); +}