Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/stopwatch/time.tsx
1691 views
1
import { CSSProperties } from "react";
2
3
import { Icon } from "@cocalc/frontend/components/icon";
4
5
function zpad(n: number): string {
6
let s = `${n}`;
7
if (s.length === 1) {
8
s = `0${s}`;
9
}
10
return s;
11
}
12
13
interface TimeProps {
14
amount: number;
15
compact?: boolean;
16
style?: CSSProperties;
17
showIcon?: boolean;
18
countdown?: number;
19
}
20
21
export function TimeAmount(props: TimeProps) {
22
let t = Math.round(props.amount / 1000);
23
const hours = Math.floor(t / 3600);
24
t -= 3600 * hours;
25
const minutes = Math.floor(t / 60);
26
t -= 60 * minutes;
27
const seconds = t;
28
return (
29
<span
30
style={{
31
fontSize: !props.compact ? "50pt" : undefined,
32
fontFamily: "courier",
33
...props.style,
34
}}
35
>
36
{props.showIcon && (
37
<TimerIcon countdown={props.countdown} style={{ marginRight: "5px" }} />
38
)}
39
{zpad(hours)}:{zpad(minutes)}:{zpad(seconds)}
40
</span>
41
);
42
}
43
44
export function TimerIcon({
45
countdown,
46
style,
47
}: {
48
countdown?: number;
49
style?: CSSProperties;
50
}) {
51
return (
52
<Icon name={countdown ? "hourglass-half" : "stopwatch"} style={style} />
53
);
54
}
55
56