Skip to content

Open source CapCut alternative.

TypeScript

fix(site): the lightbox opens knowing the clip's length

David Le5a5ea9c3d3f8
diff --git a/site/src/cut/components/GeneratePanel.tsx b/site/src/cut/components/GeneratePanel.tsx
index 340dbae7..dd35b285 100644
--- a/site/src/cut/components/GeneratePanel.tsx
+++ b/site/src/cut/components/GeneratePanel.tsx
@@ -399,6 +399,10 @@ function JobRow({ job, handle }: { job: GenerateJob; handle?: string }) {
useLightbox.getState().open({
kind: "video",
src: asset.url,
+ ...(asset.width && asset.height
+ ? { ratio: asset.width / asset.height }
+ : {}),
+ duration: asset.duration,
name: asset.name,
prompt: job.prompt,
assetId: asset.id,
diff --git a/site/src/cut/components/Lightbox.tsx b/site/src/cut/components/Lightbox.tsx
index 3d208b86..85681326 100644
--- a/site/src/cut/components/Lightbox.tsx
+++ b/site/src/cut/components/Lightbox.tsx
@@ -240,7 +240,7 @@ function VideoBody({
// picture. The `play` event turns it on.
const [playing, setPlaying] = useState(false);
const [time, setTime] = useState(0);
- const [duration, setDuration] = useState(0);
+ const [duration, setDuration] = useState(item.duration ?? 0);
const [muted, setMuted] = useState(false);
const toggle = () => {
@@ -279,7 +279,7 @@ function VideoBody({
onPlay={() => setPlaying(true)}
onPause={() => setPlaying(false)}
onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)}
- onLoadedMetadata={(e) => setDuration(e.currentTarget.duration || 0)}
+ onLoadedMetadata={(e) => setDuration(loadedDuration(e.currentTarget, item))}
/>
<MediaTransport
playing={playing}
@@ -299,6 +299,15 @@ function VideoBody({
);
}
+/** The length the transport runs on once the file's metadata is in: what the
+ * element reports, unless it reports nothing usable — a source streamed
+ * without a length says Infinity — in which case the catalog's figure stands. */
+function loadedDuration(el: HTMLMediaElement, item: LightboxItem) {
+ return Number.isFinite(el.duration) && el.duration > 0
+ ? el.duration
+ : (item.duration ?? 0);
+}
+
function AudioBody({ item }: { item: LightboxItem }) {
const asset = useEditor((s) =>
item.assetId ? s.assets.find((a) => a.id === item.assetId) : undefined,
@@ -306,7 +315,7 @@ function AudioBody({ item }: { item: LightboxItem }) {
const audioEl = useRef<HTMLAudioElement>(null);
const [playing, setPlaying] = useState(false);
const [time, setTime] = useState(0);
- const [duration, setDuration] = useState(0);
+ const [duration, setDuration] = useState(item.duration ?? 0);
// The big player takes over from any row/card preview, and closing the
// lightbox stops it — a detached media element can keep playing otherwise.
// The element renders with the body, so it exists by the time this runs.
@@ -329,7 +338,7 @@ function AudioBody({ item }: { item: LightboxItem }) {
onPlay={() => setPlaying(true)}
onPause={() => setPlaying(false)}
onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)}
- onLoadedMetadata={(e) => setDuration(e.currentTarget.duration || 0)}
+ onLoadedMetadata={(e) => setDuration(loadedDuration(e.currentTarget, item))}
onEnded={() => setPlaying(false)}
/>
<MediaTransport
diff --git a/site/src/cut/components/StockVideosPanel.tsx b/site/src/cut/components/StockVideosPanel.tsx
index 42856536..7f0205c9 100644
--- a/site/src/cut/components/StockVideosPanel.tsx
+++ b/site/src/cut/components/StockVideosPanel.tsx
@@ -285,6 +285,7 @@ function StockTile({ item }: { item: StockVideo }) {
kind: "video",
src: item.file,
ratio: aspectRatio(item.aspect),
+ duration: item.duration,
name: stockTitle(item.id),
prompt: item.prompt,
assetId: null,
diff --git a/site/src/cut/lib/lightbox.ts b/site/src/cut/lib/lightbox.ts
index e49ac237..e3f300be 100644
--- a/site/src/cut/lib/lightbox.ts
+++ b/site/src/cut/lib/lightbox.ts
@@ -18,6 +18,9 @@ export interface LightboxItem {
/** Width ÷ height, when known — sizes the viewer up front so it opens at the
* media's own shape instead of collapsing until the file's metadata loads. */
ratio?: number;
+ /** Length in seconds, when the catalog knows it — the transport reads it
+ * from the moment the viewer opens, before the file's metadata arrives. */
+ duration?: number;
/** A cover image to show while the media loads: the source's own thumbnail
* for an imported clip. */
poster?: string;
@@ -41,6 +44,7 @@ export const lightboxItemFromRef = (ref: AssetRef): LightboxItem => ({
name: ref.name,
prompt: "",
...(ref.width && ref.height ? { ratio: ref.width / ref.height } : {}),
+ ...(ref.duration ? { duration: ref.duration } : {}),
...(ref.thumb ? { poster: ref.thumb } : {}),
assetId: ref.scope === "project" ? ref.id : null,
...(ref.scope === "library" ? { libraryId: ref.id } : {}),
@@ -62,6 +66,7 @@ export const lightboxItemFromLibrary = (
libraryId: a.id,
...(bare ? { bare: true } : {}),
...(a.width && a.height ? { ratio: a.width / a.height } : {}),
+ ...(a.duration ? { duration: a.duration } : {}),
...(libraryPosterUrl(a) ? { poster: libraryPosterUrl(a) } : {}),
});