-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.tsx
79 lines (64 loc) · 1.83 KB
/
countdown.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
76
77
78
79
import BigText from "ink-big-text";
import { render } from "ink";
import { keypress, KeyPressEvent } from "$cliffy/keypress/mod.ts";
import React, { useEffect, useState } from "react";
import { useApp } from "ink";
function getTimeRemainingText(timeRemaining: number): string {
const date = new Date(timeRemaining);
const seconds = date.getUTCSeconds() < 10
? `0${date.getUTCSeconds()}`
: date.getUTCSeconds();
const minutes = date.getUTCMinutes() < 10
? `0${date.getUTCMinutes()}`
: date.getUTCMinutes();
const hours = date.getUTCHours() < 10
? `0${date.getUTCHours()}`
: date.getUTCHours();
return `${hours}:${minutes}:${seconds}`;
}
type CountdownProps = {
font?: Font;
};
enum Font {
Block = "block",
Slick = "slick",
Tiny = "tiny",
Grid = "grid",
Pallet = "pallet",
Shade = "shade",
Simple = "simple",
SimpleBlock = "simpleBlock",
"3d" = "3d",
Simple3d = "simple3d",
Chrome = "chrome",
Huge = "huge",
}
const Countdown = ({ font = Font.Chrome }: CountdownProps) => {
const [timeRemaining, setTimeRemaining] = useState(60_000);
const { exit } = useApp();
useEffect(() => {
const interval = setInterval(() => {
const timeRemaining1 = timeRemaining - 1000
setTimeRemaining(timeRemaining1);
if (timeRemaining1 === 0) {
exit();
return; // maybe not needed.
}
}, 1000);
return () => {
clearInterval(interval);
};
}, [timeRemaining, setTimeRemaining]);
useEffect(() => {
keypress().addEventListener("keydown", (event: KeyPressEvent) => {
if (event.key === "c" && event.ctrlKey) {
exit();
keypress().dispose(); // maybe not needed.
}
});
}, []);
return <BigText font={font} text={getTimeRemainingText(timeRemaining)} />;
};
export function renderCountdown() {
render(<Countdown />);
}