Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): repaint paused timeline after zoom and pan

Akshar Patelb1a976984fac
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 7c6b6fa18..1cc06b6e3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -146,6 +146,7 @@ jobs:
__tests__/unit/speed-controller.test.ts
__tests__/unit/summary-editor.test.ts
__tests__/unit/sso-login-pages.test.ts
+ __tests__/unit/timeline-paused-window.test.ts
__tests__/unit/transcript-hours.test.ts
__tests__/unit/transcript-numbers.test.ts
__tests__/unit/video-analytics-access.test.ts
diff --git a/apps/web/__tests__/unit/timeline-paused-window.test.ts b/apps/web/__tests__/unit/timeline-paused-window.test.ts
new file mode 100644
index 000000000..a32f1129b
--- /dev/null
+++ b/apps/web/__tests__/unit/timeline-paused-window.test.ts
@@ -0,0 +1,146 @@
+// @vitest-environment jsdom
+
+import { act, createElement, useEffect, useRef } from "react";
+import { createRoot } from "react-dom/client";
+import { expect, it, vi } from "vitest";
+import {
+ PlaybackProvider,
+ usePlayback,
+} from "@/app/s/[videoId]/_components/playback/PlaybackContext";
+import { TimelinePlayhead } from "@/app/s/[videoId]/_components/timeline/TimelinePlayhead";
+import {
+ TimelineProvider,
+ useTimelineActions,
+ useTimelineWindow,
+} from "@/app/s/[videoId]/_components/timeline/TimelineProvider";
+import { TimelineRail } from "@/app/s/[videoId]/_components/timeline/TimelineRail";
+
+it("updates the paused playhead and progress when zooming, panning, and fitting", async () => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ const container = document.createElement("div");
+ document.body.append(container);
+ const root = createRoot(container);
+ let handles:
+ | {
+ playback: ReturnType<typeof usePlayback>;
+ actions: ReturnType<typeof useTimelineActions>;
+ }
+ | undefined;
+
+ function Timeline() {
+ const playback = usePlayback();
+ const actions = useTimelineActions();
+ const { start, end } = useTimelineWindow();
+ const railRef = useRef<HTMLDivElement | null>(null);
+ const widthRef = useRef(800);
+ useEffect(() => {
+ handles = { playback, actions };
+ }, [playback, actions]);
+ return createElement(
+ "div",
+ null,
+ createElement("output", { id: "window-range" }, `${start}..${end}`),
+ createElement(TimelineRail, {
+ duration: 100,
+ railRef,
+ filmstrip: { tiles: [] },
+ tileCount: 0,
+ envelope: null,
+ windowStart: start,
+ windowEnd: end,
+ }),
+ createElement(
+ "div",
+ { id: "needle" },
+ createElement(TimelinePlayhead, { widthRef }),
+ ),
+ );
+ }
+
+ function Fixture() {
+ const videoRef = useRef<HTMLVideoElement | null>(null);
+ const stageRef = useRef<HTMLDivElement | null>(null);
+ return createElement(PlaybackProvider, {
+ videoRef,
+ stageRef,
+ fallbackDuration: 100,
+ children: createElement(
+ "div",
+ { ref: stageRef },
+ createElement("video", { ref: videoRef, muted: true }),
+ createElement(TimelineProvider, {
+ compact: false,
+ hasHover: true,
+ duration: 100,
+ children: createElement(Timeline),
+ }),
+ ),
+ });
+ }
+
+ const updateWindow = (update: () => void) =>
+ act(async () => {
+ update();
+ await new Promise<void>((resolve) => {
+ requestAnimationFrame(() => resolve());
+ });
+ });
+
+ try {
+ act(() => {
+ root.render(createElement(Fixture));
+ });
+ if (!handles) throw new Error("Missing playback handles");
+ const { playback, actions } = handles;
+ const needle = container.querySelector<HTMLElement>("#needle>div");
+ const progress = container.querySelector<HTMLElement>(
+ "[data-timeline-rail]>div",
+ );
+ const video = container.querySelector("video");
+ if (!needle || !progress || !video) throw new Error("Missing timeline");
+ act(() => {
+ playback.seek(25);
+ });
+ expect(video.paused).toBe(true);
+ expect(needle.style.transform).toBe("translate3d(200px, 0, 0)");
+ expect(progress.style.getPropertyValue("--p")).toBe("25%");
+
+ await updateWindow(() => actions.zoomAtTime(2, 60));
+ expect(container.querySelector("#window-range")?.textContent).toBe(
+ "30..80",
+ );
+ expect.soft(getComputedStyle(needle).opacity).toBe("0");
+ expect.soft(progress.style.getPropertyValue("--p")).toBe("0%");
+ expect(video.currentTime).toBe(25);
+
+ await updateWindow(() => actions.panBy(-20));
+ expect(container.querySelector("#window-range")?.textContent).toBe(
+ "10..60",
+ );
+ expect.soft(getComputedStyle(needle).opacity).toBe("1");
+ expect.soft(needle.style.transform).toBe("translate3d(240px, 0, 0)");
+ expect.soft(progress.style.getPropertyValue("--p")).toBe("30%");
+ expect(video.currentTime).toBe(25);
+
+ act(() => {
+ playback.seek(35);
+ });
+ expect(needle.style.transform).toBe("translate3d(400px, 0, 0)");
+ expect(progress.style.getPropertyValue("--p")).toBe("50%");
+
+ await updateWindow(actions.resetZoom);
+ expect(container.querySelector("#window-range")?.textContent).toBe(
+ "0..100",
+ );
+ expect.soft(needle.style.transform).toBe("translate3d(280px, 0, 0)");
+ expect.soft(progress.style.getPropertyValue("--p")).toBe("35%");
+ expect(video.paused).toBe(true);
+ expect(video.currentTime).toBe(35);
+ } finally {
+ act(() => {
+ root.unmount();
+ });
+ container.remove();
+ vi.unstubAllGlobals();
+ }
+});
diff --git a/apps/web/app/s/[videoId]/_components/timeline/TimelinePlayhead.tsx b/apps/web/app/s/[videoId]/_components/timeline/TimelinePlayhead.tsx
index af14a6080..e4b54e9f5 100644
--- a/apps/web/app/s/[videoId]/_components/timeline/TimelinePlayhead.tsx
+++ b/apps/web/app/s/[videoId]/_components/timeline/TimelinePlayhead.tsx
@@ -2,7 +2,7 @@
import { type RefObject, useEffect, useRef } from "react";
import { usePlayback } from "../playback/PlaybackContext";
-import { useTimelineWindowRef } from "./TimelineProvider";
+import { useTimelineWindow } from "./TimelineProvider";
import {
fractionInWindow,
RAIL_PAD_X,
@@ -14,26 +14,18 @@ interface TimelinePlayheadProps {
widthRef: RefObject<number>;
}
-/**
- * Rendered exactly once and then never again: the subscriber writes a transform
- * straight to the DOM, so a playing video costs zero React work here — including
- * when a zoom pushes the playhead out of the window and the needle has to
- * disappear. Deliberately unlabelled: the transport clock is the deck's single
- * time readout, and the needle only marks where it is.
- */
export function TimelinePlayhead({ widthRef }: TimelinePlayheadProps) {
const playback = usePlayback();
- const windowRef = useTimelineWindowRef();
+ const { start, end } = useTimelineWindow();
const rootRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const root = rootRef.current;
if (!root) return;
- let visible = true;
+ let visible = root.style.opacity !== "0";
return playback.subscribe((time) => {
- const { start, end } = windowRef.current;
const fraction = fractionInWindow(time, start, end);
const inside = fraction >= 0 && fraction <= 1;
if (inside !== visible) {
@@ -50,7 +42,7 @@ export function TimelinePlayhead({ widthRef }: TimelinePlayheadProps) {
const clamped = width > 12 ? Math.min(Math.max(x, 6), width - 6) : x;
root.style.transform = `translate3d(${clamped}px, 0, 0)`;
});
- }, [playback, widthRef, windowRef]);
+ }, [playback, widthRef, start, end]);
return (
<div
diff --git a/apps/web/app/s/[videoId]/_components/timeline/TimelineRail.tsx b/apps/web/app/s/[videoId]/_components/timeline/TimelineRail.tsx
index 040205b1e..434aacb92 100644
--- a/apps/web/app/s/[videoId]/_components/timeline/TimelineRail.tsx
+++ b/apps/web/app/s/[videoId]/_components/timeline/TimelineRail.tsx
@@ -68,10 +68,9 @@ export function TimelineRail({
let lastSecond = -1;
return playback.subscribe((time) => {
- const { start, end } = windowRef.current;
const fraction = Math.min(
1,
- Math.max(0, fractionInWindow(time, start, end)),
+ Math.max(0, fractionInWindow(time, windowStart, windowEnd)),
);
progress.style.setProperty("--p", `${fraction * 100}%`);
@@ -82,7 +81,7 @@ export function TimelineRail({
rail.setAttribute("aria-valuenow", String(second));
rail.setAttribute("aria-valuetext", formatClock(time));
});
- }, [playback, railRef, windowRef]);
+ }, [playback, railRef, windowStart, windowEnd]);
// Scrubbing lands at most one seek per frame; the media element is slow to
// honour `currentTime` and a pointermove burst would queue them all up.