Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): stop the share page upload tracker re-rendering in a loop

Richie McIlroybbc0c4f0680e
diff --git a/apps/web/__tests__/unit/upload-progress-key.test.ts b/apps/web/__tests__/unit/upload-progress-key.test.ts
new file mode 100644
index 000000000..17ba9ff22
--- /dev/null
+++ b/apps/web/__tests__/unit/upload-progress-key.test.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it } from "vitest";
+import { uploadProgressKey } from "@/app/s/[videoId]/_components/upload-progress";
+
+describe("uploadProgressKey", () => {
+ it("matches separate objects with the same value", () => {
+ const at = 1_790_000_000_000;
+ expect(
+ uploadProgressKey({
+ status: "uploading",
+ lastUpdated: new Date(at),
+ progress: 40,
+ }),
+ ).toBe(
+ uploadProgressKey({
+ status: "uploading",
+ lastUpdated: new Date(at),
+ progress: 40,
+ }),
+ );
+ expect(uploadProgressKey({ status: "fetching" })).toBe(
+ uploadProgressKey({ status: "fetching" }),
+ );
+ });
+
+ it("changes with the progress, the time or the state", () => {
+ const base = {
+ status: "processing" as const,
+ lastUpdated: new Date(1_790_000_000_000),
+ progress: 10,
+ message: null,
+ };
+ const key = uploadProgressKey(base);
+ expect(uploadProgressKey({ ...base, progress: 11 })).not.toBe(key);
+ expect(
+ uploadProgressKey({ ...base, lastUpdated: new Date(1_790_000_001_000) }),
+ ).not.toBe(key);
+ expect(uploadProgressKey({ ...base, message: "Encoding" })).not.toBe(key);
+ expect(uploadProgressKey(null)).not.toBe(key);
+ });
+});
diff --git a/apps/web/app/s/[videoId]/_components/UploadProgressTracker.tsx b/apps/web/app/s/[videoId]/_components/UploadProgressTracker.tsx
index 3f28b9b1c..ea760a18c 100644
--- a/apps/web/app/s/[videoId]/_components/UploadProgressTracker.tsx
+++ b/apps/web/app/s/[videoId]/_components/UploadProgressTracker.tsx
@@ -1,9 +1,9 @@
"use client";
import type { Video } from "@cap/web-domain";
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
import { useUploadProgress } from "./ProgressCircle";
-import type { UploadProgress } from "./upload-progress";
+import { type UploadProgress, uploadProgressKey } from "./upload-progress";
/**
* Headless bridge around `useUploadProgress`. The hook's RPC client pulls the
@@ -19,10 +19,16 @@ export default function UploadProgressTracker({
onChange: (progress: UploadProgress | null) => void;
}) {
const progress = useUploadProgress(videoId, true);
+ const latest = useRef(progress);
+ latest.current = progress;
+ // The hook returns a new object every render; passing each one on would
+ // re-render the player, and with it this tracker, forever.
+ const key = uploadProgressKey(progress);
useEffect(() => {
- onChange(progress);
- }, [progress, onChange]);
+ void key;
+ onChange(latest.current);
+ }, [key, onChange]);
return null;
}
diff --git a/apps/web/app/s/[videoId]/_components/upload-progress.ts b/apps/web/app/s/[videoId]/_components/upload-progress.ts
index 8b2238678..594042c24 100644
--- a/apps/web/app/s/[videoId]/_components/upload-progress.ts
+++ b/apps/web/app/s/[videoId]/_components/upload-progress.ts
@@ -35,6 +35,17 @@ export type UploadProgress =
lastUpdated: Date;
};
+/**
+ * Identifies an upload state by value. `useUploadProgress` builds a new object
+ * on every render, so anything that stores it has to compare this instead.
+ */
+export function uploadProgressKey(progress: UploadProgress | null): string {
+ if (!progress) return "none";
+ return JSON.stringify(progress, (_key, value: unknown) =>
+ value instanceof Date ? value.getTime() : value,
+ );
+}
+
export function shouldDeferPlaybackSource(
uploadProgress: UploadProgress | null,
): boolean {