From 61092d7c3c78fb5dc55098e358ba819863765379 Mon Sep 17 00:00:00 2001 From: Luke Date: Sat, 15 Oct 2022 21:39:46 +0200 Subject: [PATCH] refactor(web/skillcheck): move visibility into it's own state --- web/src/features/skillcheck/index.tsx | 8 ++++---- web/src/features/skillcheck/indicator.tsx | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/web/src/features/skillcheck/index.tsx b/web/src/features/skillcheck/index.tsx index d79cfd14e..d4b9a9965 100644 --- a/web/src/features/skillcheck/index.tsx +++ b/web/src/features/skillcheck/index.tsx @@ -12,7 +12,6 @@ interface CustomGameDifficulty { type GameDifficulty = 'easy' | 'medium' | 'hard' | CustomGameDifficulty; export interface SkillCheckProps { - visible: boolean; angle: number; difficultyOffset: number; difficulty: GameDifficulty; @@ -34,8 +33,8 @@ debugData([ ]); const SkillCheck: React.FC = () => { + const [visible, setVisible] = useState(false); const [skillCheck, setSkillCheck] = useState({ - visible: false, angle: 0, difficultyOffset: 315, difficulty: 'easy', @@ -44,16 +43,16 @@ const SkillCheck: React.FC = () => { useNuiEvent('startSkillCheck', (data: GameDifficulty) => { const offset = typeof data === 'object' ? data.areaSize : difficultyOffsets[data]; setSkillCheck({ - visible: true, angle: -90 + getRandomAngle(120, 360 - (315 - offset)), difficultyOffset: offset, difficulty: data, }); + setVisible(true); }); return (
- {skillCheck.visible && ( + {visible && ( <> {/*Circle track*/} @@ -91,6 +90,7 @@ const SkillCheck: React.FC = () => { : skillCheck.difficulty.speedMultiplier } setSkillCheck={setSkillCheck} + setVisible={setVisible} /> >; setSkillCheck: React.Dispatch>; } -const Indicator: React.FC = ({ angle, offset, multiplier, setSkillCheck }) => { +const Indicator: React.FC = ({ angle, offset, multiplier, setSkillCheck, setVisible }) => { const [indicatorAngle, setIndicatorAngle] = useState(-90); const intervalRef = useRef(null); const isKeyPressed = useKeyPress('e'); @@ -30,7 +31,7 @@ const Indicator: React.FC = ({ angle, offset, multiplier, setSkillCheck } if (intervalRef.current && indicatorAngle + 90 >= 360) { clearInterval(intervalRef.current); fetchNui('skillCheckOver', { success: false }); - setSkillCheck((prevState) => ({ ...prevState, visible: false })); + setVisible(false); } if (isKeyPressed && intervalRef.current) { @@ -40,7 +41,7 @@ const Indicator: React.FC = ({ angle, offset, multiplier, setSkillCheck } } else { fetchNui('skillCheckOver', { success: true }); } - setSkillCheck((prevState) => ({ ...prevState, visible: false })); + setVisible(false); } }, [indicatorAngle, isKeyPressed]);