A small web app for watching movies and shows easily
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

63 lines
1.9 KiB

import { useTranslation } from "react-i18next";
import { VideoPlayerButton } from "@/components/player/internals/Button";
import { VideoPlayerTimeFormat } from "@/stores/player/slices/interface";
import { usePlayerStore } from "@/stores/player/store";
import { durationExceedsHour, formatSeconds } from "@/utils/formatSeconds";
export function Time(props: { short?: boolean }) {
const timeFormat = usePlayerStore((s) => s.interface.timeFormat);
const setTimeFormat = usePlayerStore((s) => s.setTimeFormat);
const {
duration: timeDuration,
time,
draggingTime,
} = usePlayerStore((s) => s.progress);
const { isSeeking } = usePlayerStore((s) => s.interface);
const { t } = useTranslation();
const hasHours = durationExceedsHour(timeDuration);
function toggleMode() {
setTimeFormat(
timeFormat === VideoPlayerTimeFormat.REGULAR
? VideoPlayerTimeFormat.REMAINING
: VideoPlayerTimeFormat.REGULAR
);
}
const currentTime = Math.min(
Math.max(isSeeking ? draggingTime : time, 0),
timeDuration
);
const secondsRemaining = Math.abs(currentTime - timeDuration);
const timeLeft = formatSeconds(
secondsRemaining,
durationExceedsHour(secondsRemaining)
);
const timeWatched = formatSeconds(currentTime, hasHours);
const timeFinished = new Date(Date.now() + secondsRemaining * 1e3);
const duration = formatSeconds(timeDuration, hasHours);
let localizationKey = "regular";
if (props.short) localizationKey = "short";
else if (timeFormat === VideoPlayerTimeFormat.REMAINING)
localizationKey = "remaining";
return (
<VideoPlayerButton onClick={() => toggleMode()}>
<span>
{t(`player.time.${localizationKey}`, {
timeFinished,
timeWatched,
timeLeft,
duration,
formatParams: {
timeFinished: { hour: "numeric", minute: "numeric" },
},
})}
</span>
</VideoPlayerButton>
);
}