Skip to content

Open source CapCut alternative.

TypeScript
3.7 KB
Raw Blame
"use client";

import { create } from "zustand";
import type { AssetRef } from "./assetRef";
import { libraryAssetName, libraryPlaybackUrl, libraryPosterUrl, type LibraryAsset } from "./library";
import type { MediaAsset } from "./types";

// The asset lightbox: a full-screen viewer opened from stock tiles, generated
// media, chat asset cards, and library cards. Held in its own store so any
// surface can open it and a single mounted overlay renders it.

export interface LightboxItem {
  kind: "video" | "image" | "audio" | "text" | "font" | "lut";
  /** Fetchable source for the media or file. */
  src: string;
  name: string;
  /** The generation prompt, when known. */
  prompt: string;
  /** 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;
  /** For a project asset, its id (add straight to the timeline); null for a
   * stock or library item, which the lightbox imports first. */
  assetId: string | null;
  /** For a library asset, its id — "Use" copies it in through the library
   * route so it files like any other library use, not as stock. */
  libraryId?: string;
  /** Show the media alone, without the name, prompt, or "Use" button. The
   * library page opens this way: outside the editor there is no project to
   * add anything to. */
  bare?: boolean;
}

/** The lightbox view of an asset ref — how chat cards and attachment chips
 * open the big version of whatever they show. */
export const lightboxItemFromRef = (ref: AssetRef): LightboxItem => ({
  kind: ref.fileType ?? ref.kind,
  src: ref.url,
  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 } : {}),
});

/** The lightbox view of a project asset — what a Project Files card opens on
 * a double-click. It carries the asset's id, so "Use" adds it straight to the
 * timeline. */
export const lightboxItemFromAsset = (a: MediaAsset): LightboxItem => ({
  kind: a.type,
  src: a.url,
  name: a.name,
  prompt: "",
  assetId: a.id,
  ...(a.width && a.height ? { ratio: a.width / a.height } : {}),
  ...(a.duration ? { duration: a.duration } : {}),
  ...(a.thumbs?.[0] ? { poster: a.thumbs[0] } : {}),
});

/** The lightbox view of a library asset — what a library card opens on a
 * double-click. `bare` where the surface has no project to add anything to. */
export const lightboxItemFromLibrary = (
  a: LibraryAsset,
  bare = false,
): LightboxItem => ({
  kind: a.type,
  src: libraryPlaybackUrl(a),
  // The title read off the clip is what it is called wherever it is shown, so
  // the viewer captions it that way and an import from here lands under it.
  name: libraryAssetName(a),
  prompt: "",
  assetId: null,
  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) } : {}),
});

interface LightboxState {
  item: LightboxItem | null;
  open: (item: LightboxItem) => void;
  close: () => void;
}

export const useLightbox = create<LightboxState>((set) => ({
  item: null,
  open: (item) => set({ item }),
  close: () => set({ item: null }),
}));