Skip to content

Commit

Permalink
✨ feat: notify other players while waiting for game master to spread …
Browse files Browse the repository at this point in the history
…object and fill hands
  • Loading branch information
omidnikrah committed Jan 2, 2025
1 parent 4d2fce3 commit 553ea31
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
21 changes: 9 additions & 12 deletions apps/frontend/src/components/Toast/ToastProvider.tsx
Original file line number Diff line number Diff line change
@@ -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 '.';
Expand Down Expand Up @@ -28,7 +28,7 @@ const ToastContext = createContext<ToastContextType | undefined>(undefined);

export const ToastProvider = ({ children }: { children: ReactNode }) => {
const [toasts, setToasts] = useState<ToastData[]>([]);
const [toastIds, setToastIds] = useState<Record<string, string>>({});
const toastIdsRef = useRef<Record<string, string>>({});

const showToast = (
message: string,
Expand All @@ -38,6 +38,10 @@ export const ToastProvider = ({ children }: { children: ReactNode }) => {
) => {
const id = uuidV4();

if (toastName) {
toastIdsRef.current[toastName] = id;
}

setToasts((prev) => [
...prev,
{
Expand All @@ -52,10 +56,6 @@ export const ToastProvider = ({ children }: { children: ReactNode }) => {
setTimeout(() => dismissToast(id), duration);
}

if (toastName) {
setToastId(toastName, id);
}

return id;
};

Expand All @@ -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;
};

Expand Down
1 change: 1 addition & 0 deletions apps/frontend/src/enums/Toasts.ts
Original file line number Diff line number Diff line change
@@ -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',
}
17 changes: 12 additions & 5 deletions apps/frontend/src/hooks/useGameControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -256,6 +262,7 @@ export const useGameControls = () => {
setGamePhase(GamePhases.PLAYING);
filledHands.current = [];
targetFillHandData.current = null;
dismissToastByName(Toasts.WAITING_FOR_SPREAD_OBJECT);
}
}, 10000);
});
Expand Down

0 comments on commit 553ea31

Please sign in to comment.