From 291e7ff8a7899d2e4c25745aec0488867b97bc69 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Thu, 12 Dec 2024 19:10:21 +0100 Subject: [PATCH] =?UTF-8?q?La=20banni=C3=A8re=20de=20p=C3=A9nalit=C3=A9=20?= =?UTF-8?q?tient=20compte=20des=20vraies=20donn=C3=A9es=20et=20est=20anim?= =?UTF-8?q?=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/components/PenalyBanner.tsx | 45 ++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/client/components/PenalyBanner.tsx b/client/components/PenalyBanner.tsx index 28d97ff..079f2b6 100644 --- a/client/components/PenalyBanner.tsx +++ b/client/components/PenalyBanner.tsx @@ -1,20 +1,49 @@ -import { FontAwesome6 } from "@expo/vector-icons"; -import { View } from "react-native"; -import { Banner, MD3Colors, ProgressBar, Text } from "react-native-paper"; +import { useGame } from "@/hooks/useGame" +import { FontAwesome6 } from "@expo/vector-icons" +import { useEffect, useMemo, useState } from "react" +import { View } from "react-native" +import { Banner, MD3Colors, ProgressBar, Text } from "react-native-paper" export default function PenaltyBanner() { + const game = useGame() + const penaltyEnd = game.penaltyEnd + const hasPenalty = penaltyEnd !== null + const penaltyEndDate = useMemo(() => new Date(penaltyEnd || 0), [penaltyEnd]) + const penaltyEndPretty = penaltyEndDate.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' }) + const [remainingTime, setRemainingTime] = useState(0) + const prettyRemainingTime = useMemo(() => `${Math.floor(remainingTime / 60).toString().padStart(2, '0')}:${Math.floor(remainingTime % 60).toString().padStart(2, '0')}`, [remainingTime]) + const iconName = useMemo(() => { + switch (Math.abs(remainingTime % 4)) { + case 0: return 'hourglass-empty' + case 1: return 'hourglass-end' + case 2: return 'hourglass-half' + case 3: return 'hourglass-start' + } + }, [remainingTime]) + + useEffect(() => { + if (!hasPenalty) + return + const interval = setInterval(() => setRemainingTime(Math.floor((penaltyEndDate.getTime() - new Date().getTime()) / 1000)), 1000) + return () => clearInterval(interval) + }, [hasPenalty, penaltyEndDate]) + return ( } + visible={hasPenalty} + icon={({ size }) => } style={{ backgroundColor: MD3Colors.primary30 }}> - Vous avez actuellement une pénalité jusqu'à 18h45. - Temps restant : 14:43 + Vous avez actuellement une pénalité jusqu'à {penaltyEndPretty}. + Temps restant : {prettyRemainingTime} - + ) } \ No newline at end of file