Skip to content

Commit

Permalink
refactor(web/skillcheck): move visibility into it's own state
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Oct 18, 2022
1 parent d5437e0 commit 61092d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
8 changes: 4 additions & 4 deletions web/src/features/skillcheck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface CustomGameDifficulty {
type GameDifficulty = 'easy' | 'medium' | 'hard' | CustomGameDifficulty;

export interface SkillCheckProps {
visible: boolean;
angle: number;
difficultyOffset: number;
difficulty: GameDifficulty;
Expand All @@ -34,8 +33,8 @@ debugData([
]);

const SkillCheck: React.FC = () => {
const [visible, setVisible] = useState(false);
const [skillCheck, setSkillCheck] = useState<SkillCheckProps>({
visible: false,
angle: 0,
difficultyOffset: 315,
difficulty: 'easy',
Expand All @@ -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 (
<Center height="100%" width="100%">
{skillCheck.visible && (
{visible && (
<>
<svg width={500} height={500}>
{/*Circle track*/}
Expand Down Expand Up @@ -91,6 +90,7 @@ const SkillCheck: React.FC = () => {
: skillCheck.difficulty.speedMultiplier
}
setSkillCheck={setSkillCheck}
setVisible={setVisible}
/>
</svg>
<Box
Expand Down
7 changes: 4 additions & 3 deletions web/src/features/skillcheck/indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ interface Props {
angle: number;
offset: number;
multiplier: number;
setVisible: React.Dispatch<React.SetStateAction<boolean>>;
setSkillCheck: React.Dispatch<React.SetStateAction<SkillCheckProps>>;
}

const Indicator: React.FC<Props> = ({ angle, offset, multiplier, setSkillCheck }) => {
const Indicator: React.FC<Props> = ({ angle, offset, multiplier, setSkillCheck, setVisible }) => {
const [indicatorAngle, setIndicatorAngle] = useState(-90);
const intervalRef = useRef<NodeJS.Timer | null>(null);
const isKeyPressed = useKeyPress('e');
Expand All @@ -30,7 +31,7 @@ const Indicator: React.FC<Props> = ({ 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) {
Expand All @@ -40,7 +41,7 @@ const Indicator: React.FC<Props> = ({ angle, offset, multiplier, setSkillCheck }
} else {
fetchNui('skillCheckOver', { success: true });
}
setSkillCheck((prevState) => ({ ...prevState, visible: false }));
setVisible(false);
}
}, [indicatorAngle, isKeyPressed]);

Expand Down

0 comments on commit 61092d7

Please sign in to comment.