Use 12 hour clock + add box around time (#58)

* Use 12 hour clock + box around time

* Fix ref
This commit is contained in:
zisra 2025-11-02 07:21:10 +08:00 committed by GitHub
parent 6f3437277d
commit ac5d4443b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 11 deletions

View file

@ -37,6 +37,14 @@ function ThumbnailDisplay(props: { at: number; show: boolean }) {
});
}, [props.at]);
// Keep time label width consistent and avoid recomputing
const formattedTime = useMemo(
() => formatSeconds(Math.max(props.at, 0), durationExceedsHour(props.at)),
[props.at],
);
const transformX =
offsets.offscreenLeft > 0 ? offsets.offscreenLeft : -offsets.offscreenRight;
if (!props.show) return null;
return (
@ -45,11 +53,7 @@ function ThumbnailDisplay(props: { at: number; show: boolean }) {
<div ref={ref}>
<div
style={{
transform: `translateX(${
offsets.offscreenLeft > 0
? offsets.offscreenLeft
: -offsets.offscreenRight
}px)`,
transform: `translateX(${transformX}px)`,
}}
>
{currentThumbnail && (
@ -58,11 +62,8 @@ function ThumbnailDisplay(props: { at: number; show: boolean }) {
className="h-24 border rounded-xl border-gray-800"
/>
)}
<p className="text-center mt-1">
{formatSeconds(
Math.max(props.at, 0),
durationExceedsHour(props.at),
)}
<p className="mt-1 mx-auto text-center border rounded-xl border-gray-800 px-3 py-1 backdrop-blur-lg bg-black bg-opacity-20 w-max">
{formattedTime}
</p>
</div>
</div>

View file

@ -4,6 +4,7 @@ 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";
import { uses12HourClock } from "@/utils/uses12HourClock";
export function Time(props: { short?: boolean }) {
const timeFormat = usePlayerStore((s) => s.interface.timeFormat);
@ -63,7 +64,11 @@ export function Time(props: { short?: boolean }) {
timeLeft,
duration,
formatParams: {
timeFinished: { hour: "numeric", minute: "numeric" },
timeFinished: {
hour: "numeric",
minute: "numeric",
hour12: uses12HourClock(),
},
},
})}
</span>

View file

@ -0,0 +1,7 @@
export function uses12HourClock() {
const parts = new Intl.DateTimeFormat(undefined, {
hour: "numeric",
}).formatToParts(new Date());
// If a dayPeriod ("AM"/"PM" or localized equivalent) appears, it's 12-hour
return parts.some((p) => p.type === "dayPeriod");
}