From 553ea31aadb113a8e3d2e7bca250169f6a6f98be Mon Sep 17 00:00:00 2001 From: Omid Nikrah Date: Thu, 2 Jan 2025 13:52:06 +0330 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20notify=20other=20players=20?= =?UTF-8?q?while=20waiting=20for=20game=20master=20to=20spread=20object=20?= =?UTF-8?q?and=20fill=20hands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/Toast/ToastProvider.tsx | 21 ++++++++----------- apps/frontend/src/enums/Toasts.ts | 1 + apps/frontend/src/hooks/useGameControls.ts | 17 ++++++++++----- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/apps/frontend/src/components/Toast/ToastProvider.tsx b/apps/frontend/src/components/Toast/ToastProvider.tsx index 6527816..0777faa 100644 --- a/apps/frontend/src/components/Toast/ToastProvider.tsx +++ b/apps/frontend/src/components/Toast/ToastProvider.tsx @@ -1,5 +1,5 @@ import { AnimatePresence } from 'framer-motion'; -import { createContext, useContext, useState, ReactNode } from 'react'; +import { createContext, useContext, useState, ReactNode, useRef } from 'react'; import { v4 as uuidV4 } from 'uuid'; import { Toast } from '.'; @@ -28,7 +28,7 @@ const ToastContext = createContext(undefined); export const ToastProvider = ({ children }: { children: ReactNode }) => { const [toasts, setToasts] = useState([]); - const [toastIds, setToastIds] = useState>({}); + const toastIdsRef = useRef>({}); const showToast = ( message: string, @@ -38,6 +38,10 @@ export const ToastProvider = ({ children }: { children: ReactNode }) => { ) => { const id = uuidV4(); + if (toastName) { + toastIdsRef.current[toastName] = id; + } + setToasts((prev) => [ ...prev, { @@ -52,10 +56,6 @@ export const ToastProvider = ({ children }: { children: ReactNode }) => { setTimeout(() => dismissToast(id), duration); } - if (toastName) { - setToastId(toastName, id); - } - return id; }; @@ -70,15 +70,12 @@ export const ToastProvider = ({ children }: { children: ReactNode }) => { }; const setToastId = (key: string, id: string) => { - setToastIds((prev) => ({ - ...prev, - [key]: id, - })); + toastIdsRef.current[key] = id; }; const getToastId = (key: string) => { - const { [key]: toastId, ...rest } = toastIds; - setToastIds(rest); + const { [key]: toastId, ...rest } = toastIdsRef.current; + toastIdsRef.current = rest; return toastId; }; diff --git a/apps/frontend/src/enums/Toasts.ts b/apps/frontend/src/enums/Toasts.ts index dae60b5..f0d41bd 100644 --- a/apps/frontend/src/enums/Toasts.ts +++ b/apps/frontend/src/enums/Toasts.ts @@ -1,4 +1,5 @@ export enum Toasts { REQUESTED_EMPTY_PLAY = 'REQUESTED_EMPTY_PLAY', SPREAD_OBJECT = 'SPREAD_OBJECT', + WAITING_FOR_SPREAD_OBJECT = 'WAITING_FOR_SPREAD_OBJECT', } diff --git a/apps/frontend/src/hooks/useGameControls.ts b/apps/frontend/src/hooks/useGameControls.ts index 67b1980..4bb8721 100644 --- a/apps/frontend/src/hooks/useGameControls.ts +++ b/apps/frontend/src/hooks/useGameControls.ts @@ -234,11 +234,17 @@ export const useGameControls = () => { }, []); useEffect(() => { - if ( - phase === GamePhases.SPREADING_OBJECT && - player?.id === gameState?.gameMaster - ) { - showToast('اوستا گل رو پخش کن', 5000, true, Toasts.SPREAD_OBJECT); + if (phase === GamePhases.SPREADING_OBJECT) { + if (player?.id === gameState?.gameMaster) { + showToast('اوستا گل رو پخش کن', 5000, true, Toasts.SPREAD_OBJECT); + } else { + showToast( + 'وایسا اوستا گل رو پخش کنه', + 5000, + true, + Toasts.WAITING_FOR_SPREAD_OBJECT, + ); + } } }, [phase, player, gameState]); @@ -256,6 +262,7 @@ export const useGameControls = () => { setGamePhase(GamePhases.PLAYING); filledHands.current = []; targetFillHandData.current = null; + dismissToastByName(Toasts.WAITING_FOR_SPREAD_OBJECT); } }, 10000); });