diff --git a/apps/client/src/app/room/[code]/game/[gameId]/game.tsx b/apps/client/src/app/room/[code]/game/[gameId]/game.tsx index 3cf51ac..bcfea39 100644 --- a/apps/client/src/app/room/[code]/game/[gameId]/game.tsx +++ b/apps/client/src/app/room/[code]/game/[gameId]/game.tsx @@ -1,6 +1,6 @@ "use client"; -import { useCallback, useContext, useEffect, useMemo, useState } from "react"; +import { useCallback, useContext, useEffect, useState } from "react"; import { EventFrom, State } from "xstate"; import { useMachine } from "@xstate/react"; import { AnimatePresence } from "framer-motion"; @@ -15,8 +15,8 @@ import { getLeaderboardById, } from "@ai/utils/queries"; import { + CurrentGameComponent, gameMachine, - getCurrentComponent, } from "@ai/components/game/game-machine"; import { SocketContext } from "@ai/utils/socket-provider"; import { cn } from "@ai/utils/cn"; @@ -37,6 +37,7 @@ export default function Game({ gameInfo, session }: GameProps) { // Socket for real-time communication const socket = useContext(SocketContext); + // Next.js router const router = useRouter(); // Wait until the client mounts to avoid hydration errors @@ -168,31 +169,6 @@ export default function Game({ gameInfo, session }: GameProps) { }; }, [handlePlayAnotherGame, handleServerEvent, socket]); - // Game component shown based off state - const currentComponent = useMemo(() => { - return getCurrentComponent( - gameInfo, - state, - send, - hostId, - submittedPlayerIds, - currFaceOffQuestion, - votedPlayers, - leaderboard, - session, - ); - }, [ - gameInfo, - state, - send, - hostId, - submittedPlayerIds, - currFaceOffQuestion, - votedPlayers, - leaderboard, - session, - ]); - return (
- {isMounted ? currentComponent : null} + {isMounted ? ( + + ) : null}
diff --git a/apps/client/src/components/game/game-machine.tsx b/apps/client/src/components/game/game-machine.tsx index 0e92a1b..7b4e2e5 100644 --- a/apps/client/src/components/game/game-machine.tsx +++ b/apps/client/src/components/game/game-machine.tsx @@ -174,102 +174,127 @@ export const gameMachine = createMachine( }, ); -export const getCurrentComponent = ( - gameInfo: GameInfo, - state: StateFrom, - send: (event: EventFrom) => StateFrom, - hostId: string | null, - submittedPlayerIds: Set, - currFaceOffQuestion: QuestionGenerations | undefined, - votedPlayers: UserVote[], - leaderboard: GetGameLeaderboardResponse | undefined, - session: Session, -) => { - const stateMachineComponentMap: Record< - StateValueFrom, - ReactNode - > = { - connectingToMainframe: ( - - - - ), - connectionEstablished: ( - - - - ), - prompt: ( - - - - ), - promptSubmitted: ( - - - - ), - promptDone: ( - - - - ), - faceOff: ( - - - - ), - faceOffResults: ( - - - - ), - nextRound: ( - - - - ), - winnerLeadUp: ( - - - - ), - winner: ( - - - - ), - leaderboard: ( - - - - ), - }; - - return stateMachineComponentMap[ - state.value as StateValueFrom - ]; +export const CurrentGameComponent = ({ + gameInfo, + state, + send, + hostId, + submittedPlayerIds, + currFaceOffQuestion, + votedPlayers, + leaderboard, + session, +}: { + gameInfo: GameInfo; + state: StateFrom; + send: (event: EventFrom) => StateFrom; + hostId: string | null; + submittedPlayerIds: Set; + currFaceOffQuestion: QuestionGenerations | undefined; + votedPlayers: UserVote[]; + leaderboard: GetGameLeaderboardResponse | undefined; + session: Session; +}) => { + switch (state.value as StateValueFrom) { + case "connectingToMainframe": { + return ( + + + + ); + } + case "connectionEstablished": { + return ( + + + + ); + } + case "prompt": { + return ( + + + + ); + } + case "promptSubmitted": { + return ( + + + + ); + } + case "promptDone": { + return ( + + + + ); + } + case "faceOff": { + return ( + + + + ); + } + case "faceOffResults": { + return ( + + + + ); + } + case "nextRound": { + return ( + + + + ); + } + case "winnerLeadUp": { + return ( + + + + ); + } + case "winner": { + return ( + + + + ); + } + case "leaderboard": { + return ( + + + + ); + } + } };