Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): show live transcript on share page while recording uploads

Richie McIlroy2e3d59f1b574
diff --git a/apps/web/__tests__/unit/live-transcribe-core.test.ts b/apps/web/__tests__/unit/live-transcribe-core.test.ts
index 76967bc58..4df930ca2 100644
--- a/apps/web/__tests__/unit/live-transcribe-core.test.ts
+++ b/apps/web/__tests__/unit/live-transcribe-core.test.ts
@@ -11,8 +11,8 @@ const baseManifest = {
version: 5,
video_init_uploaded: true,
audio_init_uploaded: true,
- video_segments: [] as unknown[],
- audio_segments: [] as unknown[],
+ video_segments: [] as (number | { index: number; duration: number })[],
+ audio_segments: [] as (number | { index: number; duration: number })[],
is_complete: false,
};
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx b/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
index a198215ca..7020cf4c1 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
@@ -2,6 +2,7 @@
import { Button } from "@cap/ui";
import { useMutation, useQueryClient } from "@tanstack/react-query";
+import { useLiveTranscript } from "hooks/use-live-transcript";
import { useInvalidateTranscript, useTranscript } from "hooks/use-transcript";
import {
Check,
@@ -231,6 +232,24 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
error: transcriptError,
} = useTranscript(data.id, data.transcriptionStatus);
+ const isLiveTranscriptEnabled =
+ data.source?.type === "desktopSegments" &&
+ data.metadata?.liveTranscript != null &&
+ data.transcriptionStatus !== "COMPLETE";
+
+ const { data: liveTranscript } = useLiveTranscript(
+ data.id,
+ isLiveTranscriptEnabled,
+ );
+
+ const liveTranscriptData = useMemo(
+ () => (liveTranscript?.content ? parseVTT(liveTranscript.content) : []),
+ [liveTranscript?.content],
+ );
+
+ const showLiveTranscript =
+ isLiveTranscriptEnabled && liveTranscriptData.length > 0;
+
const invalidateTranscript = useInvalidateTranscript();
const retryTranscriptionMutation = useMutation({
@@ -481,6 +500,59 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
const canEdit = user?.id === data.owner.id && selectedLanguage === "original";
if (isTranscriptionProcessing && !hasTimedOut) {
+ if (showLiveTranscript) {
+ const isLiveActive = liveTranscript?.state === "active";
+
+ return (
+ <div className="flex flex-col h-full">
+ <div className="flex flex-none items-center gap-2 border-b border-gray-3 px-4 py-3">
+ {isLiveActive ? (
+ <span className="relative flex size-2">
+ <span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-red-500 opacity-75" />
+ <span className="relative inline-flex rounded-full size-2 bg-red-500" />
+ </span>
+ ) : (
+ <LoaderCircle className="size-3 animate-spin text-gray-9" />
+ )}
+ <span className="text-[11px] font-medium text-gray-12">
+ Live transcript
+ </span>
+ <span className="text-[11px] text-gray-9">
+ {isLiveActive
+ ? "updating as the recording uploads"
+ : "processing final transcript…"}
+ </span>
+ </div>
+
+ <div className="overflow-y-auto flex-1">
+ <div className="px-3 py-3">
+ {liveTranscriptData.map((entry) => (
+ <div
+ key={entry.id}
+ className={`flex items-start gap-1 rounded-lg px-2 transition-colors ${
+ selectedEntry === entry.id ? "bg-gray-3" : "hover:bg-gray-2"
+ }`}
+ >
+ <button
+ className="flex min-w-0 flex-1 items-baseline gap-2.5 py-1.5 text-left"
+ onClick={() => handleTranscriptClick(entry)}
+ type="button"
+ >
+ <span className="w-9 shrink-0 font-mono text-[10px] leading-[22px] tabular-nums text-gray-8">
+ {entry.timestamp}
+ </span>
+ <span className="min-w-0 flex-1 text-[13px] leading-[22px] text-gray-11">
+ {entry.text}
+ </span>
+ </button>
+ </div>
+ ))}
+ </div>
+ </div>
+ </div>
+ );
+ }
+
return (
<div className="flex h-full items-center justify-center p-7">
<div className="text-center">
diff --git a/apps/web/hooks/use-live-transcript.ts b/apps/web/hooks/use-live-transcript.ts
new file mode 100644
index 000000000..04f909267
--- /dev/null
+++ b/apps/web/hooks/use-live-transcript.ts
@@ -0,0 +1,51 @@
+import type { Video } from "@cap/web-domain";
+import { useQuery } from "@tanstack/react-query";
+import { getLiveTranscript } from "@/actions/videos/get-live-transcript";
+import type { LiveTranscriptState } from "@/lib/live-transcribe-core";
+
+export interface LiveTranscriptData {
+ content: string;
+ state: LiveTranscriptState;
+ updatedAt?: string;
+}
+
+/** Live transcription is still appending; poll often enough to feel live. */
+const ACTIVE_POLL_INTERVAL = 5000;
+/** Live transcription finished; the canonical transcript supersedes it
+ * shortly, so only poll slowly for content written between renders. */
+const SETTLED_POLL_INTERVAL = 15000;
+
+/**
+ * Provisional transcript of an instant-mode recording that is still uploading.
+ * `enabled` must already account for the video being a `desktopSegments`
+ * recording with live transcription started and no canonical transcript yet —
+ * when it is false nothing is ever fetched.
+ */
+export const useLiveTranscript = (videoId: Video.VideoId, enabled: boolean) => {
+ return useQuery({
+ queryKey: ["liveTranscript", videoId],
+ queryFn: async (): Promise<LiveTranscriptData | null> => {
+ const result = await getLiveTranscript(videoId);
+
+ if (!result.success || !result.content) return null;
+
+ return {
+ content: result.content,
+ state: result.state ?? "active",
+ updatedAt: result.updatedAt,
+ };
+ },
+ enabled,
+ refetchInterval: (query) => {
+ const state = query.state.data?.state;
+ return state === "complete" || state === "stopped"
+ ? SETTLED_POLL_INTERVAL
+ : ACTIVE_POLL_INTERVAL;
+ },
+ refetchIntervalInBackground: false,
+ staleTime: 0,
+ gcTime: 5 * 60 * 1000,
+ refetchOnWindowFocus: false,
+ retry: false,
+ });
+};