-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.tsx
75 lines (64 loc) · 2.05 KB
/
index.tsx
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
65
66
67
68
69
70
71
72
73
74
75
import * as React from "react";
import { useEffect, useState } from "react";
import { flushSync } from "react-dom";
import { createRoot } from "react-dom/client";
import Dice from "./Dice";
import Labels from "./Labels";
import Light from "./Light";
function App() {
const [connectedClients, setConnectedClients] = useState(0);
const [diceClickCount, setDiceClickCount] = useState(0);
const [totalUptimeSeconds, setTotalUptimeSeconds] = useState(0);
const onDiceClick = () => {
setDiceClickCount((n) => n + 1);
};
const updateUptimeLabel = () => {
// Get total document uptime
// NOTE: document.timeline.currentTime reports uptime in ms
if (!document.timeline.currentTime) return;
setTotalUptimeSeconds(
Math.floor((document.timeline.currentTime as number) / 1000),
);
};
useEffect(() => {
updateUptimeLabel();
const intervalId = setInterval(updateUptimeLabel, 1000);
window.addEventListener("connected", () => {
setConnectedClients((n) => n + 1);
});
window.addEventListener("disconnected", () => {
setConnectedClients((n) => n - 1);
});
return () => {
clearInterval(intervalId);
window.removeEventListener("connected", () => {
setConnectedClients((n) => n + 1);
});
window.removeEventListener("disconnected", () => {
setConnectedClients((n) => n - 1);
});
};
}, []);
const uptimeMinutes = Math.floor(totalUptimeSeconds / 60);
const uptimeSeconds = totalUptimeSeconds - uptimeMinutes * 60;
const uptimeLabelText =
uptimeMinutes > 0
? `${uptimeMinutes}:${String(uptimeSeconds).padStart(2, "0")}`
: `${uptimeSeconds}s`;
return (
<>
<Light />
<Labels
connectedText={`Connected clients: ${connectedClients}`}
rollsText={`Dice clicks: ${diceClickCount}`}
uptimeText={`Uptime: ${uptimeLabelText}`}
/>
<Dice onClick={onDiceClick} />
</>
);
}
const container = document.getElementById("root")!;
const root = createRoot(container);
flushSync(() => {
root.render(<App />);
});