-
Notifications
You must be signed in to change notification settings - Fork 782
Expand file tree
/
Copy pathtimer.js
More file actions
34 lines (25 loc) · 750 Bytes
/
timer.js
File metadata and controls
34 lines (25 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React, { useEffect } from 'react';
import { Text } from 'ink';
import * as Actions from '../gamestate/action-types';
import { useGameStateContext } from '../components/gamestate-context';
export default function GameTimer() {
const { state, dispatch } = useGameStateContext();
useEffect(() => {
const tick = () => {
dispatch({ type: Actions.TICK });
};
const timer = setInterval(tick, 1000);
return () => {
clearInterval(timer);
};
}, [dispatch]);
let color = 'green';
if (state.gameTimer <= 10 && state.gameTimer > 5) {
color = 'yellow';
} else if (state.gameTimer <= 5) {
color = 'red';
}
return (
<Text>Time: <Text color={ color }>{ state.gameTimer }</Text></Text>
)
}