From 2ed943857e2f3b424410bf5ba2631622c55d064a Mon Sep 17 00:00:00 2001 From: aronmal Date: Wed, 26 Apr 2023 13:49:15 +0200 Subject: [PATCH] Use React Context as in the first place --- leaky-ships/components/Lobby/LobbyFrame.tsx | 19 ++++----- leaky-ships/lib/hooks/useGameState.tsx | 43 +++++++++++++++++++-- leaky-ships/lib/zodSchemas.ts | 4 +- leaky-ships/pages/_app.tsx | 8 ++-- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/leaky-ships/components/Lobby/LobbyFrame.tsx b/leaky-ships/components/Lobby/LobbyFrame.tsx index a763f61..fffb44d 100644 --- a/leaky-ships/components/Lobby/LobbyFrame.tsx +++ b/leaky-ships/components/Lobby/LobbyFrame.tsx @@ -4,23 +4,21 @@ import useGameState from "@lib/hooks/useGameState" import { Fragment, useEffect, useState } from "react" function LobbyFrame({ openSettings }: { openSettings: () => void }) { - const { gameProps } = useGameState() + const { payload } = useGameState().gameProps const [dots, setDots] = useState(1) - const { gamePin, player1, player2 } = gameProps.payload - useEffect(() => { - if (player2) return + if (payload?.player2) return const interval = setInterval(() => setDots((e) => (e % 3) + 1), 1000) return () => clearInterval(interval) - }, [player2]) + }, [payload?.player2]) return (
Chat

- Game-PIN: {gamePin?.pin} + Game-PIN: {payload?.gamePin?.pin}

Settings @@ -29,13 +27,16 @@ function LobbyFrame({ openSettings }: { openSettings: () => void }) {

VS

- {player2 ? ( - + {payload?.player2 ? ( + ) : (

Warte auf Spieler 2 {Array.from(Array(dots), () => ".").join("")} diff --git a/leaky-ships/lib/hooks/useGameState.tsx b/leaky-ships/lib/hooks/useGameState.tsx index d2fc2ce..ff549f8 100644 --- a/leaky-ships/lib/hooks/useGameState.tsx +++ b/leaky-ships/lib/hooks/useGameState.tsx @@ -1,14 +1,49 @@ import { GamePropsSchema } from "@lib/zodSchemas" import { useSession } from "next-auth/react" -import { useEffect, useState } from "react" +import { + Dispatch, + ReactNode, + SetStateAction, + createContext, + useContext, + useEffect, + useState, + useMemo, +} from "react" import { toast } from "react-toastify" -function useGameState(initial?: GamePropsSchema) { - const [gameProps, setGameProps] = useState( - initial ?? { payload: {}, hash: "" } +const initialValue = { payload: null, hash: null } + +interface gameContext { + gameProps: GamePropsSchema + setGameProps: Dispatch> +} + +export const gameContext = createContext({ + gameProps: initialValue, + setGameProps: () => {}, +}) + +export function GameContextProvider({ children }: { children: ReactNode }) { + const [gameProps, setGameProps] = useState(initialValue) + + const value = useMemo( + () => ({ gameProps, setGameProps }), + [gameProps, setGameProps] ) + + return {children} +} + +function useGameState(initial?: GamePropsSchema) { + const { gameProps, setGameProps } = useContext(gameContext) const { data: session, status } = useSession() + useEffect(() => { + if (!initial) return + setGameProps(initial) + }, [initial, setGameProps]) + useEffect(() => { if (status === "loading") return if (!session) diff --git a/leaky-ships/lib/zodSchemas.ts b/leaky-ships/lib/zodSchemas.ts index e90d5a7..b2533b8 100644 --- a/leaky-ships/lib/zodSchemas.ts +++ b/leaky-ships/lib/zodSchemas.ts @@ -22,8 +22,8 @@ export const CreateSchema = z .strict() export const GamePropsSchema = z.object({ - payload: CreateSchema, - hash: z.string(), + payload: CreateSchema.nullable(), + hash: z.string().nullable(), }) export type GamePropsSchema = z.infer diff --git a/leaky-ships/pages/_app.tsx b/leaky-ships/pages/_app.tsx index 2973dc4..19b6a0c 100644 --- a/leaky-ships/pages/_app.tsx +++ b/leaky-ships/pages/_app.tsx @@ -2,7 +2,7 @@ import "../styles/App.scss" import "../styles/globals.scss" import "../styles/grid2.scss" import "../styles/grid.scss" -import { CreateSchema } from "@lib/zodSchemas" +import { GameContextProvider } from "@lib/hooks/useGameState" import { SessionProvider } from "next-auth/react" import type { AppProps } from "next/app" import { ToastContainer } from "react-toastify" @@ -14,8 +14,10 @@ export default function App({ }: AppProps) { return ( - - + + + + ) }