From 677fd79bee59e05870ec115b1c87fbffd07c4683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20R=C3=A5dberg?= Date: Sat, 16 Mar 2024 10:23:15 +0100 Subject: [PATCH] fix long press down --- src/Components/Buttons/CommanderDamage.tsx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Components/Buttons/CommanderDamage.tsx b/src/Components/Buttons/CommanderDamage.tsx index e72aa23..1ea9d9b 100644 --- a/src/Components/Buttons/CommanderDamage.tsx +++ b/src/Components/Buttons/CommanderDamage.tsx @@ -66,8 +66,7 @@ export const CommanderDamage = ({ }: CommanderDamageButtonComponentProps) => { const { updatePlayer } = usePlayers(); const timeoutRef = useRef(undefined); - const [timeoutFinished, setTimeoutFinished] = useState(false); - const [hasPressedDown, setHasPressedDown] = useState(false); + const [downLongPressed, setDownLongPressed] = useState(false); const downPositionRef = useRef({ x: 0, y: 0 }); const handleCommanderDamageChange = ( @@ -109,16 +108,16 @@ export const CommanderDamage = ({ const handleDownInput = ({ opponentIndex, isPartner, event }: InputProps) => { downPositionRef.current = { x: event.clientX, y: event.clientY }; - setTimeoutFinished(false); - setHasPressedDown(true); + setDownLongPressed(false); + timeoutRef.current = setTimeout(() => { - setTimeoutFinished(true); + setDownLongPressed(true); handleCommanderDamageChange(opponentIndex, -1, isPartner); }, decrementTimeoutMs); }; const handleUpInput = ({ opponentIndex, isPartner, event }: InputProps) => { - if (!(hasPressedDown && !timeoutFinished)) { + if (downLongPressed) { return; } @@ -134,14 +133,14 @@ export const CommanderDamage = ({ return; } + clearTimeout(timeoutRef.current); + handleCommanderDamageChange(opponentIndex, 1, isPartner); - setHasPressedDown(false); }; const handleLeaveInput = () => { - setTimeoutFinished(true); + setDownLongPressed(true); clearTimeout(timeoutRef.current); - setHasPressedDown(false); }; const opponentIndex = opponent.index;