Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): show hours in embed player duration

Paddy4bfe18ea3ce8
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 000000000..d48f20457
--- /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 4339c337f..07e8784b8 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<
<>
<span className="text-xs text-gray-400">•</span>
<p className="text-xs text-gray-300 sm:text-sm">
- {formatTime(longestDuration)}
+ {formatPlayerTime(longestDuration)}
</p>
</>
)}
diff --git a/apps/web/lib/player-time.ts b/apps/web/lib/player-time.ts
new file mode 100644
index 000000000..481296867
--- /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}`;
+}