-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOnYourMarkScreen.tsx
More file actions
64 lines (54 loc) · 2.06 KB
/
OnYourMarkScreen.tsx
File metadata and controls
64 lines (54 loc) · 2.06 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { useState, useEffect, useContext, useRef } from "react";
import { useNavigate } from "react-router-dom";
// context
import { GlobalStateContext, GlobalDispatchContext } from "@context/GlobalContext";
// utils
import { startRace } from "@utils";
export const OnYourMarkScreen = () => {
const navigate = useNavigate();
const dispatch = useContext(GlobalDispatchContext);
const { isAdmin } = useContext(GlobalStateContext);
const countdown = ["3...", "2...", "1..."];
const [currentMessage, setCurrentMessage] = useState(0);
const initialMessages = ["On Your Mark...", "Get Set..."];
const [raceInitiated, setRaceInitiated] = useState(false);
const beepAudioRef = useRef(null);
const beep2AudioRef = useRef(null);
useEffect(() => {
beepAudioRef.current = new Audio("https://sdk-race.s3.amazonaws.com/audio/beep1.mp3");
beep2AudioRef.current = new Audio("https://sdk-race.s3.amazonaws.com/audio/beep2.mp3");
beepAudioRef.current.addEventListener("canplaythrough", () => {
beepAudioRef.current.play();
});
}, []);
useEffect(() => {
if (currentMessage < countdown.length) {
if (currentMessage === 0) {
beepAudioRef.current.load();
} else {
beepAudioRef.current.play();
}
const timerId = setTimeout(() => {
setCurrentMessage(currentMessage + 1);
}, 1000);
return () => clearTimeout(timerId);
} else if (currentMessage === countdown.length && !raceInitiated) {
beep2AudioRef.current.play();
startRace({ dispatch, navigate });
setRaceInitiated(true);
}
}, [currentMessage, raceInitiated, dispatch, navigate]);
return (
<div className="container text-center pt-20" style={{ height: isAdmin ? "90vh" : "100vh" }}>
{initialMessages?.map((message, index) => (
<h2 key={index} className="pb-10">
{message}
</h2>
))}
<h2 key={currentMessage} className="py-8 small-to-large">
{currentMessage < countdown.length ? countdown[currentMessage] : "Go!"}
</h2>
</div>
);
};
export default OnYourMarkScreen;