Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): seek embed player from start time query

Richie McIlroy67152b065065
diff --git a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx
index e2a9a2dfe..cba5858b5 100644
--- a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx
+++ b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx
@@ -58,6 +58,8 @@ export const EmbedVideo = forwardRef<
chapters?: { title: string; start: number }[];
ownerName?: string | null;
autoplay?: boolean;
+ /** Seconds to open at, from the embed URL's `?t=`. */
+ startTime?: number | null;
minimal?: boolean;
viewerSettings?: ViewerSettings | null;
showPlaybackStatusBadge?: boolean;
@@ -71,6 +73,7 @@ export const EmbedVideo = forwardRef<
chapters = [],
ownerName,
autoplay = false,
+ startTime = null,
minimal = false,
viewerSettings,
showPlaybackStatusBadge = false,
@@ -78,6 +81,7 @@ export const EmbedVideo = forwardRef<
ref,
) => {
const videoRef = useRef<HTMLVideoElement>(null);
+ const seekedToStart = useRef(false);
const playerContainerRef = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => videoRef.current as HTMLVideoElement);
usePlayerJsReceiver({ playerContainerRef, videoRef });
@@ -210,10 +214,24 @@ export const EmbedVideo = forwardRef<
const player = videoRef.current;
const handleLoadedMetadata = () => {
setLongestDuration(player.duration);
+
+ // Once only. HLS level switches and source swaps fire this again, and
+ // yanking a viewer who has since scrubbed back to the start offset
+ // would be worse than ignoring the deep link.
+ if (startTime === null || seekedToStart.current) return;
+ seekedToStart.current = true;
+ const limit = Number.isFinite(player.duration)
+ ? Math.max(player.duration - 0.001, 0)
+ : startTime;
+ try {
+ player.currentTime = Math.min(startTime, limit);
+ } catch (error) {
+ console.warn("Failed to seek embed to start time", error);
+ }
};
if (player.readyState >= 1) {
- setLongestDuration(player.duration);
+ handleLoadedMetadata();
} else {
player.addEventListener("loadedmetadata", handleLoadedMetadata);
}
@@ -221,7 +239,7 @@ export const EmbedVideo = forwardRef<
return () => {
player.removeEventListener("loadedmetadata", handleLoadedMetadata);
};
- }, []);
+ }, [startTime]);
return (
<>
diff --git a/apps/web/app/embed/[videoId]/page.tsx b/apps/web/app/embed/[videoId]/page.tsx
index e6bb947e8..28eb58172 100644
--- a/apps/web/app/embed/[videoId]/page.tsx
+++ b/apps/web/app/embed/[videoId]/page.tsx
@@ -92,6 +92,16 @@ export default async function EmbedVideoPage(
const videoId = params.videoId as Video.VideoId;
const autoplay = searchParams.autoplay === "true";
const minimal = searchParams.slack === "true";
+ // `?t=` mirrors the share page, so an embed snippet copied "at 1:23" opens
+ // there too. Repeated params arrive as an array; take the first.
+ const startTimeParam = Array.isArray(searchParams.t)
+ ? searchParams.t[0]
+ : searchParams.t;
+ const parsedStartTime = Number.parseInt(startTimeParam ?? "", 10);
+ const startTime =
+ Number.isFinite(parsedStartTime) && parsedStartTime > 0
+ ? parsedStartTime
+ : null;
return Effect.gen(function* () {
const videosPolicy = yield* VideosPolicy;
@@ -166,6 +176,7 @@ export default async function EmbedVideoPage(
<EmbedContent
video={data.video}
autoplay={autoplay}
+ startTime={startTime}
minimal={minimal}
/>
)}
@@ -183,6 +194,7 @@ export default async function EmbedVideoPage(
async function EmbedContent({
video,
autoplay,
+ startTime,
minimal,
}: {
video: Omit<typeof videos.$inferSelect, "password"> & {
@@ -191,6 +203,7 @@ async function EmbedContent({
orgSettings?: (typeof organizations.$inferSelect)["settings"] | null;
};
autoplay: boolean;
+ startTime: number | null;
minimal: boolean;
}) {
const user = await getCurrentUser();
@@ -273,6 +286,9 @@ async function EmbedContent({
createdAt: comments.createdAt,
updatedAt: comments.updatedAt,
parentCommentId: comments.parentCommentId,
+ mediaKey: comments.mediaKey,
+ mediaDuration: comments.mediaDuration,
+ mediaMeta: comments.mediaMeta,
authorName: users.name,
})
.from(comments)
@@ -297,6 +313,7 @@ async function EmbedContent({
}
ownerName={videoOwner[0]?.name || null}
autoplay={autoplay}
+ startTime={startTime}
minimal={minimal}
viewerSettings={rules.settings}
showPlaybackStatusBadge={user?.id === video.ownerId}