sudo-archive/src/components/video/controls/TimeControl.tsx

49 lines
1.2 KiB
TypeScript
Raw Normal View History

import { useVideoPlayerState } from "../VideoContext";
function durationExceedsHour(secs: number): boolean {
return secs > 60 * 60;
}
function formatSeconds(secs: number, showHours = false): string {
2023-01-10 18:53:55 +00:00
if (Number.isNaN(secs)) {
if (showHours) return "0:00:00";
return "0:00";
}
let time = secs;
const seconds = time % 60;
time /= 60;
const minutes = time % 60;
time /= 60;
const hours = minutes % 60;
2023-01-10 18:53:55 +00:00
if (!showHours)
return `${Math.round(minutes).toString()}:${Math.round(seconds)
.toString()
.padStart(2, "0")}`;
return `${Math.round(hours).toString()}:${Math.round(minutes)
.toString()
2023-01-10 18:53:55 +00:00
.padStart(2, "0")}:${Math.round(seconds).toString().padStart(2, "0")}`;
}
2023-01-09 20:51:24 +00:00
interface Props {
className?: string;
}
export function TimeControl(props: Props) {
const { videoState } = useVideoPlayerState();
const hasHours = durationExceedsHour(videoState.duration);
const time = formatSeconds(videoState.time, hasHours);
const duration = formatSeconds(videoState.duration, hasHours);
return (
2023-01-09 20:51:24 +00:00
<div className={props.className}>
<p className="select-none text-white">
{time} / {duration}
</p>
</div>
);
}