Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add upload progress store for media comments

Richie McIlroya61fff8ed826
diff --git a/apps/web/app/s/[videoId]/_components/media-comment/UploadProgressIndicator.tsx b/apps/web/app/s/[videoId]/_components/media-comment/UploadProgressIndicator.tsx
new file mode 100644
index 000000000..1f7bc0e18
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/media-comment/UploadProgressIndicator.tsx
@@ -0,0 +1,86 @@
+"use client";
+
+import { useUploadProgress } from "./upload-progress";
+
+const RADIUS = 5;
+const CIRCUMFERENCE = 2 * Math.PI * RADIUS;
+
+function Ring({
+ fraction,
+ className,
+}: {
+ fraction: number;
+ className?: string;
+}) {
+ return (
+ <svg viewBox="0 0 14 14" className={className} aria-hidden="true">
+ <circle
+ cx="7"
+ cy="7"
+ r={RADIUS}
+ fill="none"
+ stroke="currentColor"
+ strokeOpacity="0.25"
+ strokeWidth="2"
+ />
+ <circle
+ cx="7"
+ cy="7"
+ r={RADIUS}
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeDasharray={CIRCUMFERENCE}
+ strokeDashoffset={CIRCUMFERENCE * (1 - fraction)}
+ transform="rotate(-90 7 7)"
+ className="transition-[stroke-dashoffset] duration-300 ease-out"
+ />
+ </svg>
+ );
+}
+
+/**
+ * Ring + percent for a media comment that is still uploading. Subscribes per
+ * comment id and renders nothing once the entry clears, so a settled card
+ * sheds it without the card itself re-rendering for progress ticks.
+ *
+ * "overlay": dark pill for on-media placement (video card corner).
+ * "inline": quiet text-toned row for the voice pill.
+ */
+export function UploadProgressIndicator({
+ commentId,
+ variant,
+}: {
+ commentId: string;
+ variant: "overlay" | "inline";
+}) {
+ const fraction = useUploadProgress(commentId);
+ if (fraction === undefined) return null;
+
+ // Below 1% the pipeline is still converting/thumbnailing — no bytes moving.
+ const label =
+ fraction < 0.01 ? "Preparing" : `${Math.round(fraction * 100)}%`;
+
+ if (variant === "overlay") {
+ return (
+ <span
+ data-upload-progress
+ className="pointer-events-none absolute bottom-1.5 left-1.5 flex items-center gap-1 rounded bg-black/60 px-1.5 py-0.5 text-[10px] tabular-nums text-white"
+ >
+ <Ring fraction={fraction} className="size-3 shrink-0" />
+ {label}
+ </span>
+ );
+ }
+
+ return (
+ <span
+ data-upload-progress
+ className="flex shrink-0 items-center gap-1 text-[11px] tabular-nums text-gray-10"
+ >
+ <Ring fraction={fraction} className="size-3 shrink-0" />
+ {label}
+ </span>
+ );
+}
diff --git a/apps/web/app/s/[videoId]/_components/media-comment/upload-progress.ts b/apps/web/app/s/[videoId]/_components/media-comment/upload-progress.ts
new file mode 100644
index 000000000..46d8fef53
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/media-comment/upload-progress.ts
@@ -0,0 +1,52 @@
+"use client";
+
+import { useSyncExternalStore } from "react";
+
+/**
+ * Upload progress for in-flight media comments, keyed by comment id (the temp
+ * id while optimistic). Module-level for the same reason as the blob-url
+ * registry: the sending card renders in trees (sidebar, timeline hover card,
+ * mobile sheet) far from the recorder that owns the upload.
+ *
+ * Values are 0..1 fractions. An entry exists for the whole pipeline — a card
+ * can therefore distinguish "preparing" (entry at 0, conversion/thumbnail
+ * still running) from actual byte progress without extra state.
+ */
+const fractions = new Map<string, number>();
+const listeners = new Set<() => void>();
+
+const notify = () => {
+ for (const listener of listeners) listener();
+};
+
+export function setUploadProgress(commentId: string, fraction: number) {
+ const clamped = Math.max(0, Math.min(1, fraction));
+ // Progress events fire per uploaded chunk; skip no-op notifications so
+ // subscribed cards only render when the visible percent actually moves.
+ if (
+ Math.round((fractions.get(commentId) ?? -1) * 100) ===
+ Math.round(clamped * 100)
+ )
+ return;
+ fractions.set(commentId, clamped);
+ notify();
+}
+
+export function clearUploadProgress(commentId: string) {
+ if (!fractions.delete(commentId)) return;
+ notify();
+}
+
+const subscribe = (listener: () => void) => {
+ listeners.add(listener);
+ return () => listeners.delete(listener);
+};
+
+/** Fraction 0..1 while the comment's media is uploading, undefined otherwise. */
+export function useUploadProgress(commentId: string): number | undefined {
+ return useSyncExternalStore(
+ subscribe,
+ () => fractions.get(commentId),
+ () => undefined,
+ );
+}