From 4bfe18ea3ce8799ed8ab56b86efa9c6f8ba17ef4 Mon Sep 17 00:00:00 2001 From: Paddy Date: Fri, 16 Oct 2026 22:07:09 +0100 Subject: [PATCH] fix(web): show hours in embed player duration The embed player formatted the duration with its own MM:SS helper, so anything an hour or longer rendered as a bare minute count: a 2h 3m 4s video showed "123:04", and a missing duration showed "NaN:NaN". The share page already rolls over into hours via media-chrome's time utils, so the two surfaces disagreed on the same video. Move the formatter to lib/player-time.ts with hour rollover and a non-finite guard. Output below an hour is byte-identical to before. --- apps/web/__tests__/unit/player-time.test.ts | 30 +++++++++++++++++++ .../[videoId]/_components/EmbedVideo.tsx | 11 ++----- apps/web/lib/player-time.ts | 15 ++++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 apps/web/__tests__/unit/player-time.test.ts create mode 100644 apps/web/lib/player-time.ts diff --git a/apps/web/__tests__/unit/player-time.test.ts b/apps/web/__tests__/unit/player-time.test.ts new file mode 100644 index 00000000000..d48f2045729 --- /dev/null +++ b/apps/web/__tests__/unit/player-time.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; +import { formatPlayerTime } from "@/lib/player-time"; + +describe("formatPlayerTime", () => { + it("pads minutes and seconds under an hour", () => { + expect(formatPlayerTime(0.4)).toBe("00:00"); + expect(formatPlayerTime(9)).toBe("00:09"); + expect(formatPlayerTime(63)).toBe("01:03"); + expect(formatPlayerTime(3599)).toBe("59:59"); + }); + + it("rolls over into hours instead of counting past 59 minutes", () => { + expect(formatPlayerTime(3600)).toBe("1:00:00"); + expect(formatPlayerTime(3825)).toBe("1:03:45"); + expect(formatPlayerTime(7384)).toBe("2:03:04"); + expect(formatPlayerTime(36000)).toBe("10:00:00"); + }); + + it("truncates fractional seconds rather than rounding up", () => { + expect(formatPlayerTime(59.9)).toBe("00:59"); + expect(formatPlayerTime(3599.9)).toBe("59:59"); + }); + + it("falls back to zero for non-finite or non-positive input", () => { + expect(formatPlayerTime(0)).toBe("00:00"); + expect(formatPlayerTime(-5)).toBe("00:00"); + expect(formatPlayerTime(Number.NaN)).toBe("00:00"); + expect(formatPlayerTime(Number.POSITIVE_INFINITY)).toBe("00:00"); + }); +}); diff --git a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx index 4339c337f2a..07e8784b8d3 100644 --- a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx +++ b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx @@ -28,6 +28,7 @@ import { parseVTT, type TranscriptEntry, } from "@/app/s/[videoId]/_components/utils/transcript-utils"; +import { formatPlayerTime } from "@/lib/player-time"; import type { SharePageBranding } from "@/lib/share-branding"; import type { ShareCallToAction } from "@/lib/share-call-to-action"; import { usePlayerJsReceiver } from "./use-player-js-receiver"; @@ -38,14 +39,6 @@ declare global { } } -const formatTime = (time: number) => { - const minutes = Math.floor(time / 60); - const seconds = Math.floor(time % 60); - return `${minutes.toString().padStart(2, "0")}:${seconds - .toString() - .padStart(2, "0")}`; -}; - type CommentWithAuthor = typeof commentsSchema.$inferSelect & { authorName: string | null; }; @@ -347,7 +340,7 @@ export const EmbedVideo = forwardRef< <> •

- {formatTime(longestDuration)} + {formatPlayerTime(longestDuration)}

)} diff --git a/apps/web/lib/player-time.ts b/apps/web/lib/player-time.ts new file mode 100644 index 00000000000..48129686756 --- /dev/null +++ b/apps/web/lib/player-time.ts @@ -0,0 +1,15 @@ +export function formatPlayerTime(totalSeconds: number): string { + if (!Number.isFinite(totalSeconds) || totalSeconds <= 0) return "00:00"; + + const rounded = Math.floor(totalSeconds); + const hours = Math.floor(rounded / 3600); + const minutes = Math.floor((rounded % 3600) / 60); + const seconds = rounded % 60; + const paddedSeconds = seconds.toString().padStart(2, "0"); + + if (hours > 0) { + return `${hours}:${minutes.toString().padStart(2, "0")}:${paddedSeconds}`; + } + + return `${minutes.toString().padStart(2, "0")}:${paddedSeconds}`; +}