Open source CapCut alternative.
TypeScript
fix(cut): show local Library imports immediately [rebuild]
David Leb45ad2a99c73
diff --git a/docs/guides/cut/local-compute.md b/docs/guides/cut/local-compute.mdindex 3c69e5dc..6534c06e 100644--- a/docs/guides/cut/local-compute.md+++ b/docs/guides/cut/local-compute.md@@ -55,7 +55,7 @@ Imports and the assistant's conversion tool prepare playable H.264/AAC media thrThe Library uploads originals even when browser probing fails; the worker prepares playback and retains the source for download. A browser-local project or shelf without the required codec borrows preparation, saves the result into its own storage, and removes the temporary cloud copy. The engine shelf uses the same worker when its tools fail. Hosted preparation requires a signed-in account with available storage and worker capacity.-Retries resume the same uploaded object and job. The Library publishes the prepared asset and its storage accounting together; failed preparation keeps the upload available for retry until its staging claim expires. The pending tile reports uploading, queued, preparing, and failure states.+Library drops share local previews and background storage across folders and panels; cards show “Importing”, and retries reuse uploaded bytes and jobs.Project conversion keeps the asset id, so existing clips continue to reference it. The same tools run in the editor and headless clients.diff --git a/site/src/cut/components/ImportVideoPoster.tsx b/site/src/cut/components/ImportVideoPoster.tsxnew file mode 100644index 00000000..1ef1efce--- /dev/null+++ b/site/src/cut/components/ImportVideoPoster.tsx@@ -0,0 +1,57 @@+"use client";++import { useCallback, useEffect, useState } from "react";+import { Film } from "lucide-react";+import { Skeleton } from "@/components/ui/skeleton";+import { importPoster } from "@/cut/lib/importPoster";+import { holdMemory } from "@/cut/lib/memoryBudget";++type Props = { file: File; size: number };++/** A local frame while the original file is being imported to any shelf. */+export function ImportVideoPoster({ file, size }: Props) {+ const [visible, setVisible] = useState(false);+ const [poster, setPoster] = useState<string>();+ const [unreadable, setUnreadable] = useState(false);+ const ref = useCallback((node: HTMLDivElement | null) => {+ if (!node) return;+ const observer = new IntersectionObserver(([entry]) => setVisible(entry.isIntersecting));+ observer.observe(node);+ return () => observer.disconnect();+ }, []);++ useEffect(() => {+ if (!visible) return;+ const abort = new AbortController();+ let url: string | undefined;+ let release: (() => void) | undefined;+ void importPoster(file, size, abort.signal).then((blob) => {+ if (abort.signal.aborted) return;+ url = URL.createObjectURL(blob);+ release = holdMemory("libraryPictures", () => blob.size + size * size * 4);+ setPoster(url);+ }).catch(() => {+ // A missing local decoder leaves the import running on its chosen shelf.+ if (!abort.signal.aborted) setUnreadable(true);+ });+ return () => {+ abort.abort();+ if (url) URL.revokeObjectURL(url);+ release?.();+ setPoster(undefined);+ };+ }, [file, size, visible]);++ return (+ <div ref={ref} className="size-full">+ {poster ? (+ // eslint-disable-next-line @next/next/no-img-element -- local decoded frame+ <img src={poster} alt={file.name} className="size-full object-cover" />+ ) : unreadable ? (+ <div className="flex size-full items-center justify-center bg-muted text-muted-foreground">+ <Film className="size-8" aria-label="Video" />+ </div>+ ) : <Skeleton className="size-full rounded-none" />}+ </div>+ );+}diff --git a/site/src/cut/components/LibraryImportCard.tsx b/site/src/cut/components/LibraryImportCard.tsxnew file mode 100644index 00000000..7cf8d9d2--- /dev/null+++ b/site/src/cut/components/LibraryImportCard.tsx@@ -0,0 +1,187 @@+"use client";++import { useCallback, useState } from "react";+import { File as FileIcon, Plus, RotateCcw, X } from "lucide-react";+import { Button } from "@/components/ui/button";+import { Skeleton } from "@/components/ui/skeleton";+import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";+import { useElapsed } from "@/cut/hooks/useElapsed";+import { AudioCardFace } from "@/cut/components/AudioPanel";+import { FontSpecimen } from "@/cut/components/FontSpecimen";+import { ImportVideoPoster } from "@/cut/components/ImportVideoPoster";+import { openLocalImport } from "@/cut/lib/localImportPreview";+import type { LibraryArrival } from "@/cut/lib/libraryIntake";+import { cn } from "@/lib/utils";++/** Local previews stay available while storage runs; failed imports keep their retry. */+export function LibraryImportCard({+ item,+ area,+ onRetry,+ onDismiss,+ onUse,+}: {+ item: LibraryArrival;+ area: number;+ onRetry: () => void;+ onDismiss: () => void;+ onUse?: () => void;+}) {+ const elapsed = useElapsed(item.error ? null : item.startedAt);+ const [previewUrl, setPreviewUrl] = useState<string>();+ const file = item.file;+ const mediaType = item.mediaType;+ const previewKind = mediaType === "video" || mediaType === "image" || mediaType === "audio" ? mediaType : null;+ const canPreview = !!file && !!previewKind && !item.error;+ const openPreview = () => {+ if (file && previewKind) openLocalImport(file, previewKind);+ };+ const tileRef = useCallback(+ (node: HTMLDivElement | null) => {+ if (!node || !file || (mediaType !== "image" && mediaType !== "audio" && mediaType !== "font")) return;+ let url: string | undefined;+ const observer = new IntersectionObserver(+ (entries) => {+ const visible = entries.some((entry) => entry.isIntersecting);+ if (visible && !url) {+ url = URL.createObjectURL(file);+ setPreviewUrl(url);+ } else if (!visible && url) {+ URL.revokeObjectURL(url);+ url = undefined;+ setPreviewUrl(undefined);+ }+ },+ { rootMargin: "150px" },+ );+ observer.observe(node);+ return () => {+ observer.disconnect();+ if (url) URL.revokeObjectURL(url);+ };+ },+ [file, mediaType],+ );+ const frame = item.shape ?? { width: 1, height: 1 };+ return (+ <div+ ref={tileRef}+ role={canPreview ? "button" : undefined}+ tabIndex={canPreview ? 0 : undefined}+ aria-label={canPreview ? `Preview ${item.name}` : undefined}+ onDoubleClick={canPreview ? openPreview : undefined}+ onKeyDown={canPreview ? (event) => {+ if (event.target !== event.currentTarget) return;+ if (event.key !== "Enter" && event.key !== " ") return;+ event.preventDefault();+ openPreview();+ } : undefined}+ draggable={canPreview}+ onDragStart={canPreview ? (event) => {+ event.dataTransfer.items.clear();+ event.dataTransfer.items.add(file!);+ event.dataTransfer.effectAllowed = "copy";+ } : undefined}+ className={cn(+ "relative max-w-full overflow-hidden rounded-xl border",+ canPreview && "cursor-pointer",+ item.error ? "border-destructive/50 bg-muted" : "border-border",+ )}+ style={{+ width: Math.round(Math.sqrt((area * frame.width) / frame.height)),+ aspectRatio: `${frame.width} / ${frame.height}`,+ }}+ >+ {item.error ? (+ // Clear of the strip the link and the reason share along the top.+ <div className="flex size-full flex-col items-center justify-center gap-1.5 pt-8">+ <Button+ variant="outline"+ size="sm"+ className="w-24 justify-start"+ onClick={onRetry}+ >+ <RotateCcw data-icon="inline-start" /> Retry+ </Button>+ <Button+ variant="outline"+ size="sm"+ className="w-24 justify-start text-destructive hover:text-destructive"+ onClick={onDismiss}+ >+ <X data-icon="inline-start" /> Cancel+ </Button>+ </div>+ ) : previewUrl && mediaType === "audio" ? (+ <AudioCardFace url={previewUrl} duration={0} durationClassName="hidden" />+ ) : previewUrl && mediaType === "image" ? (+ // eslint-disable-next-line @next/next/no-img-element -- local file preview+ <img src={previewUrl} alt={item.name} className="size-full object-cover" />+ ) : previewUrl && mediaType === "font" ? (+ <FontSpecimen assetId={item.id} src={previewUrl} fitHeight className="size-full" />+ ) : file && mediaType === "video" ? (+ <ImportVideoPoster file={file} size={Math.ceil(Math.sqrt(area))} />+ ) : file && mediaType !== "image" && mediaType !== "audio" ? (+ <div className="flex size-full flex-col items-center justify-center gap-2 bg-muted text-muted-foreground">+ <FileIcon className="size-8" />+ <span className="text-xs uppercase">{mediaType ?? "Archive"}</span>+ </div>+ ) : (+ <Skeleton className="size-full rounded-none" />+ )}+ <TooltipProvider>+ <Tooltip>+ <TooltipTrigger+ render={+ <span+ className={cn(+ "absolute top-1.5 left-1.5 truncate rounded-lg bg-black/55 px-2 py-1 text-[11px] font-medium text-white backdrop-blur-sm",+ item.error ? "max-w-[calc(100%-4rem)]" : "max-w-[70%]",+ )}+ />+ }+ >+ {item.name}+ </TooltipTrigger>+ <TooltipContent className="max-w-xs break-all">+ {item.source ?? item.name}+ </TooltipContent>+ </Tooltip>+ </TooltipProvider>+ {canPreview && onUse && (+ <Button size="icon-sm" variant="secondary" className="absolute top-1.5 right-1.5" aria-label={`Add ${item.name} to project`}+ onClick={(event) => { event.stopPropagation(); onUse(); }}>+ <Plus />+ </Button>+ )}+ {item.error ? (+ // The reason rides in the tooltip: on a tile this size the message+ // itself would cover the media it stands in for.+ <TooltipProvider>+ <Tooltip>+ <TooltipTrigger+ render={+ <span className="absolute top-1.5 right-1.5 rounded-md bg-destructive/90 px-1.5 py-0.5 text-[10px] text-white" />+ }+ >+ Failed+ </TooltipTrigger>+ <TooltipContent className="max-w-xs">{item.error}</TooltipContent>+ </Tooltip>+ </TooltipProvider>+ ) : (+ <span+ className={cn(+ "absolute bottom-1.5 flex items-center gap-1 rounded-md bg-black/65 px-1.5 py-0.5 text-[10px] text-white",+ mediaType === "audio"+ ? "right-1.5 max-w-[calc(100%-3.5rem)]"+ : "left-1.5 max-w-[calc(100%-0.75rem)]",+ )}+ >+ <span className="truncate">Importing</span>+ <span className="shrink-0 font-mono tabular-nums">{elapsed}</span>+ </span>+ )}+ </div>+ );+}diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex 81f28191..faff4fc7 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -1,6 +1,7 @@"use client";-import type { LibraryUploadState } from "@/cut/lib/libraryUpload";+import { dismissLibraryImport, importLibraryFiles, useLibraryFileImports, type LibraryArrival as Pending } from "@/cut/lib/libraryIntake";+import { LibraryImportCard } from "@/cut/components/LibraryImportCard";import { libraryFolderRef } from "@/cut/lib/folderReference";import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";import { useQueryClient } from "@tanstack/react-query";@@ -11,16 +12,13 @@ import {FolderPlus,Link as LinkIcon,Loader2,- RotateCcw,Share2,Trash2,Upload,- X,} from "lucide-react";import { Button } from "@/components/ui/button";import { DropdownMenuItem } from "@/components/ui/dropdown-menu";import { useDeleteKey } from "@/cut/hooks/useDeleteKey";-import { useElapsed } from "@/cut/hooks/useElapsed";import {Dialog,DialogContent,@@ -28,21 +26,13 @@ import {DialogTitle,} from "@/components/ui/dialog";import { Input } from "@/components/ui/input";-import {- Tooltip,- TooltipContent,- TooltipProvider,- TooltipTrigger,-} from "@/components/ui/tooltip";-import { Skeleton } from "@/components/ui/skeleton";import { setObjectDragImage } from "@/cut/lib/assetDrag";import { LibraryCard, LIBRARY_TILE_AREA, LIBRARY_AUDIO_TILE_AREA } from "@/cut/components/LibraryCard";import { ShelfBadge } from "@/cut/components/ShelfBadge";-import { assetTypeOf, isMediaFile, MEDIA_ACCEPT } from "@/cut/lib/media";-import { isFontArchive } from "@/cut/lib/fontArchive";+import { MEDIA_ACCEPT } from "@/cut/lib/media";import { saveNote } from "@/cut/lib/notes";import { notesKey, patchNotes, useNotes } from "@/cut/lib/queries";-import { libraryKey, libraryScope, patchLibrary, refetchLibrary, useLibrary } from "@/cut/lib/queries";+import { patchLibrary, refetchLibrary, useLibrary } from "@/cut/lib/queries";import {createLibraryFolder,deleteFromLibrary,@@ -57,8 +47,6 @@ import {moveLibraryItem,updateLibraryFolder,renameTemplate,- uploadToLibrary,- type ImportStage,type LibraryAsset,type LibraryFolder,type LibraryTemplateItem,@@ -82,16 +70,11 @@ import { TemplateCard } from "./TemplateCard";import { homeHref, useCutBase } from "@/cut/lib/nav";import {forgetLinkedCopies,- expandLinkedFiles,- isLinkedFile,linkedAccept,- isLinkedType,- shelfForNewItem,syncLinkedLibrary,} from "@/cut/lib/linkedLibrary";import { shapeBand } from "@/cut/lib/types";import { cn } from "@/lib/utils";-import { AudioCardFace } from "./AudioPanel";import { childrenOf, folderTrail, folderWithin, parentOf } from "@/cut/lib/folderTree";import {FolderCrumb,@@ -125,39 +108,6 @@ const isTemplate = (x: LibraryAsset | LibraryTemplateItem): x is LibraryTemplatetype DeleteSet = { folders: LibraryFolder[]; items: (LibraryAsset | LibraryTemplateItem)[] };const NO_PICK: DeleteSet = { folders: [], items: [] };-/** What an arriving item is doing right now. An upload is one push from this- * browser; a link is fetched by whichever shelf is taking it, and comes down- * in stages. */-type PendingStage = "preparing" | "uploading" | ImportStage;--/** Media on its way into the library: the tile it will occupy, standing in- * for it while the work runs. */-type Pending = {- id: string;- /** The whole link, for the tile's tooltip: the label on the face is cut to- * the site and the post's id. */- source?: string;- /** The file name being uploaded, or the link being imported. */- name: string;- folderId: string | null;- file?: File;- mediaType?: LibraryAsset["type"];- stage: PendingStage;- startedAt: number;- /** A guess at the media's shape, for a link, so the tile opens near the size- * the clip will take. Uploads sit square until the file lands. */- shape?: { width: number; height: number };- /** Set when the arrival failed: the tile holds the reason, and the way to- * try again or give up, until the user picks one. */- error?: string;- /** The stage the work opens in, so a retry reads from the start again. */- startStage: PendingStage;- /** The whole arrival, ready to run once more: a failed tile retries by- * calling this, with the file or link and where it was headed still in- * hand. */- run: () => Promise<void>;-};-/** Routing words a path spends on the way to the thing: they say nothing about* which post this is, so a label built from one ("youtube.com/watch") reads the* same for every import from that site. */@@ -197,153 +147,6 @@ function linkLabel(url: string): string {}}-const STAGE_LABEL: Record<PendingStage, string> = {- preparing: "Preparing",- uploading: "Uploading",- queued: "Queued",- downloading: "Downloading",- saving: "Saving",-};--/** The tile an arriving item occupies: a card the size and shape the media- * will take, its face a skeleton until the file lands, with the link it came- * from and what the work is doing riding on top. A failed arrival holds the- * reason until the user clears it. */-function PendingTile({- item,- area,- onRetry,- onDismiss,-}: {- item: Pending;- area: number;- onRetry: () => void;- onDismiss: () => void;-}) {- const elapsed = useElapsed(item.error ? null : item.startedAt);- const [previewUrl, setPreviewUrl] = useState<string>();- const file = item.file;- const mediaType = item.mediaType;- const tileRef = useCallback(- (node: HTMLDivElement | null) => {- if (!node || !file || !mediaType || mediaType === "font") return;- let url: string | undefined;- const observer = new IntersectionObserver(- (entries) => {- if (!entries.some((entry) => entry.isIntersecting)) return;- observer.disconnect();- url = URL.createObjectURL(file);- setPreviewUrl(url);- },- { rootMargin: "150px" },- );- observer.observe(node);- return () => {- observer.disconnect();- if (url) URL.revokeObjectURL(url);- };- },- [file, mediaType],- );- const frame = item.shape ?? { width: 1, height: 1 };- return (- <div- ref={tileRef}- className={cn(- "relative max-w-full overflow-hidden rounded-xl border",- item.error ? "border-destructive/50 bg-muted" : "border-border",- )}- style={{- width: Math.round(Math.sqrt((area * frame.width) / frame.height)),- aspectRatio: `${frame.width} / ${frame.height}`,- }}- >- {item.error ? (- // Clear of the strip the link and the reason share along the top.- <div className="flex size-full flex-col items-center justify-center gap-1.5 pt-8">- <Button- variant="outline"- size="sm"- className="w-24 justify-start"- onClick={onRetry}- >- <RotateCcw data-icon="inline-start" /> Retry- </Button>- <Button- variant="outline"- size="sm"- className="w-24 justify-start text-destructive hover:text-destructive"- onClick={onDismiss}- >- <X data-icon="inline-start" /> Cancel- </Button>- </div>- ) : previewUrl && mediaType === "audio" ? (- <AudioCardFace url={previewUrl} duration={0} durationClassName="hidden" />- ) : previewUrl && mediaType === "image" ? (- // eslint-disable-next-line @next/next/no-img-element -- local file preview- <img src={previewUrl} alt={item.name} className="size-full object-cover" />- ) : previewUrl && mediaType === "video" ? (- <video- src={previewUrl}- muted- playsInline- preload="metadata"- className="size-full object-cover"- />- ) : (- <Skeleton className="size-full rounded-none" />- )}- <TooltipProvider>- <Tooltip>- <TooltipTrigger- render={- <span- className={cn(- "absolute top-1.5 left-1.5 truncate rounded-lg bg-black/55 px-2 py-1 text-[11px] font-medium text-white backdrop-blur-sm",- item.error ? "max-w-[calc(100%-4rem)]" : "max-w-[70%]",- )}- />- }- >- {item.name}- </TooltipTrigger>- <TooltipContent className="max-w-xs break-all">- {item.source ?? item.name}- </TooltipContent>- </Tooltip>- </TooltipProvider>- {item.error ? (- // The reason rides in the tooltip: on a tile this size the message- // itself would cover the media it stands in for.- <TooltipProvider>- <Tooltip>- <TooltipTrigger- render={- <span className="absolute top-1.5 right-1.5 rounded-md bg-destructive/90 px-1.5 py-0.5 text-[10px] text-white" />- }- >- Failed- </TooltipTrigger>- <TooltipContent className="max-w-xs">{item.error}</TooltipContent>- </Tooltip>- </TooltipProvider>- ) : (- <span- className={cn(- "absolute bottom-1.5 flex items-center gap-1 rounded-md bg-black/65 px-1.5 py-0.5 text-[10px] text-white",- mediaType === "audio"- ? "right-1.5 max-w-[calc(100%-3.5rem)]"- : "left-1.5 max-w-[calc(100%-0.75rem)]",- )}- >- <span className="truncate">{STAGE_LABEL[item.stage]}</span>- <span className="shrink-0 font-mono tabular-nums">{elapsed}</span>- </span>- )}- </div>- );-}export function LibraryView() {const noteView = useRef<NotesViewHandle>(null);@@ -398,9 +201,10 @@ export function LibraryView() {// Media on its way in — an upload or a link — each one a tile in the grid// from the moment it starts, so the library shows the work rather than the// dialog holding it.- const [pending, setPending] = useState<Pending[]>([]);+ const [linkPending, setPending] = useState<Pending[]>([]);+ const filePending = useLibraryFileImports((state) => state.items);+ const pending = useMemo(() => [...linkPending, ...filePending], [linkPending, filePending]);// One drain bounds probe, decode, and transfer work across overlapping drops.- const uploadTail = useRef(Promise.resolve());const [sharing, setSharing] = useState<(LibraryShareTarget & { residency: Residency }) | null>(null);useLayoutEffect(() => () => setSharing(null), []);const shareFolder = (id: string) => {@@ -457,12 +261,14 @@ export function LibraryView() {reportActivity("import", pending.some((p) => !p.error));}, [pending]);const addPending = (p: Pending) => setPending((q) => [...q, p]);- const setStage = (id: string, stage: PendingStage) =>+ const setStage = (id: string, stage: Pending["stage"]) =>setPending((q) => q.map((p) => (p.id === id ? { ...p, stage } : p)));const failPending = (id: string, error: string) =>setPending((q) => q.map((p) => (p.id === id ? { ...p, error } : p)));- const dropPending = (id: string) =>+ const dropPending = (id: string) => {setPending((q) => q.filter((p) => p.id !== id));+ dismissLibraryImport(id);+ };// Run a failed arrival again on the tile it already has: the reason clears,// the clock restarts, and the same work goes out once more.const retryPending = (item: Pending) => {@@ -478,97 +284,13 @@ export function LibraryView() {: p,),);- uploadTail.current = uploadTail.current.then(item.run);+ void item.run();};- // Upload a batch into `folderId` (the open folder by default — folder tiles- // pass their own id when files are dropped straight onto them).- const upload = async (- files: FileList | File[],- into: string | null = openFolder,- ) => {- const { residency, folderId } = landing(into);- if (!live(residency)) return;- const accepts = (file: File) => isMediaFile(file) || isLinkedFile(file);- const prepare = (file: File): Pending => {- const id = crypto.randomUUID();- // Filing can fail after storage succeeds. Retrying keeps that stored copy.- let asset: LibraryAsset | undefined;- const uploadState: LibraryUploadState = {};- const run = async () => {- try {- if (isFontArchive(file)) {- setStage(id, "preparing");- const children = (await expandLinkedFiles([file]))- .filter(accepts)- .map(prepare);- if (!children.length) {- throw new Error("This archive contains no supported font files.");- }- setPending((q) => q.flatMap((p) => p.id === id ? children : [p]));- for (const child of children) await child.run();- return;- }- setStage(id, residency === "browser" ? "saving" : "uploading");- if (!asset) {- const shelf = isLinkedFile(file) && !folderId- ? await shelfForNewItem(file.size)- : residency;- asset = await uploadToLibrary(file, shelf, {- state: uploadState,- folderId: shelf === "cloud" ? folderId : undefined,- onStage: (stage) => setStage(id, stage),- });- }- if (folderId) {- setStage(id, "saving");- await moveLibraryItem(asset.residency, asset.id, folderId);- asset.folderId = folderId;- }- const landed = asset;- // A drop can beat the first listing. Keep its tile until the cache- // can take the stored asset alongside the rest of the shelf.- if (!client.getQueryData(libraryKey(libraryScope()))) {- await reload();- if (!client.getQueryData(libraryKey(libraryScope()))) {- throw new Error("Could not refresh the library. Retry to show the saved file.");- }- }- patch((d) => ({- ...d,- assets: [landed, ...d.assets.filter((a) => a.id !== landed.id)],- }));- // A lent item is only usable once it is in reach of the menus.- if (isLinkedType(asset.type)) void syncLinkedLibrary();- dropPending(id);- } catch (e) {- failPending(- id,- e instanceof Error ? e.message : "Could not upload that file.",- );- }- };- return {- id,- name: file.name,- file,- mediaType: assetTypeOf(file) ?? (isLinkedFile(file) ? "font" : undefined),- folderId,- stage: "queued",- startStage: "queued",- startedAt: Date.now(),- run,- };- };- const batch = Array.from(files)- .filter((file) => accepts(file) || isFontArchive(file))- .map(prepare);- // Publish every tile before unpacking, probing, or sending the first file.- setPending((q) => [...q, ...batch]);- uploadTail.current = uploadTail.current.then(async () => {- for (const item of batch) await item.run();- });- await uploadTail.current;+ const upload = (files: FileList | File[], into: string | null = openFolder) => {+ const destination = landing(into);+ if (!live(destination.residency)) return Promise.resolve();+ return importLibraryFiles(files, destination, client);};// The link's tile goes up at once — shaped by what that kind of link usually@@ -1137,7 +859,7 @@ export function LibraryView() {{tiles.map((tile) => {if ("pending" in tile)return (- <PendingTile+ <LibraryImportCardkey={tile.pending.id}item={tile.pending}area={tile.pending.mediaType === "audio" ? audioArea : TILE_AREA}diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsxindex 09714e9b..c347c858 100644--- a/site/src/cut/components/SidePanel.tsx+++ b/site/src/cut/components/SidePanel.tsx@@ -1,5 +1,8 @@"use client";+import { dismissLibraryImport, importLibraryFiles, useLibraryFileImports } from "@/cut/lib/libraryIntake";+import { LibraryImportCard } from "@/cut/components/LibraryImportCard";+import { libraryFolderRef, projectFolderRef } from "@/cut/lib/folderReference";import { Fragment, useCallback, useEffect, useRef, useState } from "react";import { useQueryClient } from "@tanstack/react-query";@@ -75,7 +78,6 @@ import {renameTemplate,saveAssetToLibrary,saveTemplate,- uploadToLibrary,type LibraryAsset,type LibraryData,type LibraryFolder,@@ -92,14 +94,9 @@ import { isStylePresetTemplate } from "@/cut/lib/stylePresets";import { retryUpload } from "@/cut/lib/importQueue";import {forgetLinkedCopies,- expandLinkedFiles,- isLinkedFile,linkedAccept,- isLinkedType,- shelfForNewItem,- syncLinkedLibrary,} from "@/cut/lib/linkedLibrary";-import { downloadMedia, isMediaFile, MEDIA_ACCEPT, revealMedia } from "@/cut/lib/media";+import { downloadMedia, MEDIA_ACCEPT, revealMedia } from "@/cut/lib/media";import { mediaUrl } from "@/cut/lib/types";import {genPulseOverlay,@@ -152,7 +149,7 @@ import { StockVideosPanel } from "./StockVideosPanel";import { STOCK_MUSIC } from "@/cut/lib/stockMusicManifest";import { STOCK_SFX } from "@/cut/lib/stockSfxManifest";import { STOCK_VIDEOS } from "@/cut/lib/stockVideoManifest";-import { LibraryCard } from "@/cut/components/LibraryCard";+import { LibraryCard, LIBRARY_TILE_AREA } from "@/cut/components/LibraryCard";import { ShelfBadge } from "@/cut/components/ShelfBadge";import { MediaCardShell } from "./MediaCardShell";import { lightboxItemFromAsset } from "@/cut/lib/lightbox";@@ -785,7 +782,7 @@ function MediaPanel({importing={importing}/>) : (- <LibraryPanel projectId={projectId} />+ <LibraryPanel projectId={projectId} onImport={onImport} />)}</>);@@ -1632,7 +1629,10 @@ const CAMERA_ROLL_FOLDER = "camera-roll";* array on every render. */const EMPTY_CLIPS: LibraryAsset[] = [];-function LibraryPanel({ projectId }: { projectId: string }) {+function LibraryPanel({ projectId, onImport }: {+ projectId: string;+ onImport: (files: FileList | File[], opts?: { mediaOnly?: boolean; folderId?: string }) => void;+}) {const client = useQueryClient();// One cached listing serves every library surface (lib/queries.ts), and it// stays live while this panel is open: the shelf re-reads on a timer and@@ -1670,7 +1670,8 @@ function LibraryPanel({ projectId }: { projectId: string }) {const [deleting, setDeleting] = useState<LibraryDeleteSet | null>(null);// The folder whose name field is open, when the right-click menu opened it.const [renamingFolder, setRenamingFolder] = useState<string | null>(null);- const [uploading, setUploading] = useState(0);+ const arrivals = useLibraryFileImports((state) => state.items);+ const pending = arrivals.filter((item) => item.folderId === openFolder);// Where an asset shows: phone recordings gather in the derived Camera Roll// folder whatever their server folderId says.@@ -1781,37 +1782,11 @@ function LibraryPanel({ projectId }: { projectId: string }) {const shelfForNew = (folderId: string | null) =>(folderId ? folders.find((f) => f.id === folderId)?.residency : null) ?? activeResidency();- const upload = async (files: FileList | File[], into: string | null = openFolder) => {- // The Camera Roll takes nothing by hand; an upload made while it is open- // lands at the root like any other.+ const upload = (files: FileList | File[], into: string | null = openFolder) => {const folderId = into === CAMERA_ROLL_FOLDER ? null : into;- const list = (await expandLinkedFiles(Array.from(files))).filter(- (f) => isMediaFile(f) || isLinkedFile(f)- );- if (list.length === 0) return;const residency = shelfForNew(folderId);- setUploading((n) => n + list.length);- for (const file of list) {- try {- // Something the Library lends rather than copies, dropped outside a- // folder, takes the shelf every surface can read; dropped into one, the- // folder still decides.- const shelf =- isLinkedFile(file) && !folderId ? await shelfForNewItem(file.size) : residency;- const asset = await uploadToLibrary(file, shelf);- if (folderId) {- await moveLibraryItem(shelf, asset.id, folderId).catch(() => {});- asset.folderId = folderId;- }- patch((d) => ({ ...d, assets: [asset, ...d.assets] }));- // A lent item is only usable once it is in reach of the menus.- if (isLinkedType(asset.type)) void syncLinkedLibrary();- } catch {- // Skip unreadable files; the rest of the batch still uploads.- } finally {- setUploading((n) => n - 1);- }- }+ if (!reachable(residency)) return Promise.resolve();+ return importLibraryFiles(files, { residency, folderId }, client);};// The whole panel takes drops: OS files upload to the shared library (into@@ -2060,7 +2035,7 @@ function LibraryPanel({ projectId }: { projectId: string }) {</div></div>)}- {order.length === 0 && uploading === 0 ? (+ {order.length === 0 && pending.length === 0 ? (// At the root, filed-away assets, folders, and templates all count as// content — the invitation is only for a truly empty library.openFolder !== null ? (@@ -2097,14 +2072,13 @@ function LibraryPanel({ projectId }: { projectId: string }) {onDragStartExtra={(e) => onCardDragExtra(e, a)}/>))}- {uploading > 0 && (- <div className="flex aspect-square flex-col items-center justify-center gap-2 rounded-lg border border-dashed border-input text-[11px] text-muted-foreground">- <Loader2 className="size-4 animate-spin" />- <span>- Uploading… <LiveElapsed />- </span>- </div>- )}+ {pending.map((item) => (+ <LibraryImportCard key={item.id} item={item} area={LIBRARY_TILE_AREA}+ onRetry={() => void item.run()}+ onDismiss={() => dismissLibraryImport(item.id)}+ onUse={item.file ? () => onImport([item.file!], { mediaOnly: true }) : undefined}+ />+ ))}</Marquee>)}</ScrollArea>diff --git a/site/src/cut/lib/importPoster.ts b/site/src/cut/lib/importPoster.tsnew file mode 100644index 00000000..0794dee0--- /dev/null+++ b/site/src/cut/lib/importPoster.ts@@ -0,0 +1,38 @@+import { frameSink, openMedia, videoTrackOf } from "@/cut/lib/mediaRead";+import { rasterCanvasToBlob } from "@/cut/lib/raster";++// Visible import cards share one decoder slot, separate from the upload queue.+let tail: Promise<unknown> = Promise.resolve();++export function importPoster(file: Blob, size: number, signal: AbortSignal): Promise<Blob> {+ const result = tail.then(async () => {+ signal.throwIfAborted();+ const input = openMedia(file);+ let disposed = false;+ const dispose = () => {+ if (disposed) return;+ disposed = true;+ input.dispose();+ };+ signal.addEventListener("abort", dispose, { once: true });+ try {+ const track = await videoTrackOf(input);+ signal.throwIfAborted();+ if (!track) throw new Error("No readable video frame.");+ const frame = await frameSink(track, { width: size, height: size, fit: "cover" }, { poolSize: 1 }).getCanvas(0);+ signal.throwIfAborted();+ if (!frame) throw new Error("No readable video frame.");+ try {+ return await rasterCanvasToBlob(frame.canvas, "image/jpeg");+ } finally {+ frame.canvas.width = 0;+ frame.canvas.height = 0;+ }+ } finally {+ signal.removeEventListener("abort", dispose);+ dispose();+ }+ });+ tail = result.catch(() => {});+ return result;+}diff --git a/site/src/cut/lib/libraryIntake.test.ts b/site/src/cut/lib/libraryIntake.test.tsnew file mode 100644index 00000000..80ebcafb--- /dev/null+++ b/site/src/cut/lib/libraryIntake.test.ts@@ -0,0 +1,89 @@+import { afterEach, describe, expect, spyOn, test } from "bun:test";+import { QueryClient } from "@tanstack/react-query";+import * as library from "@/cut/lib/library";+import * as linked from "@/cut/lib/linkedLibrary";+import { libraryKey, libraryScope } from "@/cut/lib/queries";+import { importLibraryFiles, useLibraryFileImports } from "@/cut/lib/libraryIntake";+import type { Residency } from "@/cut/lib/residency";++const restores: (() => void)[] = [];+afterEach(() => {+ for (const restore of restores.splice(0)) restore();+ useLibraryFileImports.setState({ items: [] });+});++function setup() {+ const client = new QueryClient();+ client.setQueryData(libraryKey(libraryScope()), { assets: [], folders: [], templates: [] });+ const upload = spyOn(library, "uploadToLibrary").mockImplementation(async (file, residency) => ({+ id: file.name, fileName: file.name, name: file.name, type: "video", duration: 1, addedAt: 0, residency: residency!,+ }));+ const move = spyOn(library, "moveLibraryItem").mockResolvedValue(undefined);+ const sync = spyOn(linked, "syncLinkedLibrary").mockResolvedValue(undefined);+ restores.push(() => { upload.mockRestore(); move.mockRestore(); sync.mockRestore(); client.clear(); });+ return { client, upload, move };+}++describe("shared Library file intake", () => {+ for (const residency of ["browser", "local", "cloud"] as Residency[]) {+ test(`${residency} publishes every supported file before storage starts and keeps its folder`, async () => {+ const h = setup();+ const lut = new File(["LUT_3D_SIZE 2"], "look.cube");+ const files = [+ new File(["video"], "phone.mov", { type: "video/quicktime" }),+ new File(["image"], "photo.jpg", { type: "image/jpeg" }),+ new File(["audio"], "song.wav", { type: "audio/wav" }),+ new File(["font"], "face.woff2", { type: "font/woff2" }),+ ...(linked.linkedTypeOfFile(lut) ? [lut] : []),+ ];+ const done = importLibraryFiles(files, { residency, folderId: "folder" }, h.client);+ expect(useLibraryFileImports.getState().items.map((item) => item.file)).toEqual(files);+ expect(h.upload).not.toHaveBeenCalled();+ await done;+ expect(h.upload.mock.calls.map((call) => call[1])).toEqual(files.map(() => residency));+ expect(h.move.mock.calls.map((call) => call[2])).toEqual(files.map(() => "folder"));+ expect(useLibraryFileImports.getState().items).toEqual([]);+ const data = h.client.getQueryData<library.LibraryData>(libraryKey(libraryScope()))!;+ expect(data.assets).toHaveLength(files.length);+ expect(data.assets.every((asset) => asset.folderId === "folder")).toBe(true);+ });+ }++ test("a failed folder write retries the saved asset without another upload", async () => {+ const h = setup();+ let first = true;+ h.move.mockImplementation(async () => { if (first) { first = false; throw new Error("Folder write failed"); } });+ await importLibraryFiles([new File(["video"], "phone.mov")], { residency: "cloud", folderId: "folder" }, h.client);+ const item = useLibraryFileImports.getState().items[0];+ expect(item.error).toBe("Folder write failed");+ await Promise.all([item.run(), item.run()]);+ expect(h.upload.mock.calls.length).toBe(1);+ expect(h.move.mock.calls.length).toBe(2);+ expect(useLibraryFileImports.getState().items).toEqual([]);+ });++ test("a failed file stays retryable while the rest of the drop lands", async () => {+ const h = setup();+ h.upload.mockImplementation(async (file, residency) => {+ if (file.name === "a.mp4") throw new Error("Storage unavailable");+ return { id: file.name, fileName: file.name, name: file.name, type: "video", duration: 1, addedAt: 0, residency: residency! };+ });+ await importLibraryFiles([new File(["a"], "a.mp4"), new File(["b"], "b.mp4")], { residency: "cloud", folderId: null }, h.client);+ expect(useLibraryFileImports.getState().items.map((item) => item.name)).toEqual(["a.mp4"]);+ expect(useLibraryFileImports.getState().items[0].error).toBe("Storage unavailable");+ expect(h.upload.mock.calls.length).toBe(2);+ });++ test("an archive appears immediately and its children keep the drop's folder", async () => {+ const h = setup();+ const archive = new File(["archive"], "fonts.zip", { type: "application/zip" });+ const font = new File(["font"], "face.ttf");+ const expand = spyOn(linked, "expandLinkedFiles").mockImplementation(async ([file]) => file === archive ? [font] : [file]);+ restores.push(() => expand.mockRestore());+ const done = importLibraryFiles([archive], { residency: "browser", folderId: "fonts" }, h.client);+ expect(useLibraryFileImports.getState().items[0].file).toBe(archive);+ await done;+ expect(h.upload.mock.calls[0]?.[0]).toBe(font);+ expect(h.move.mock.calls[0]).toEqual(["browser", "face.ttf", "fonts"]);+ });+});diff --git a/site/src/cut/lib/libraryIntake.ts b/site/src/cut/lib/libraryIntake.tsnew file mode 100644index 00000000..dd063e73--- /dev/null+++ b/site/src/cut/lib/libraryIntake.ts@@ -0,0 +1,101 @@+"use client";++import { create } from "zustand";+import type { QueryClient } from "@tanstack/react-query";+import { currentEngineUser, subscribeEngineUser } from "@/cut/lib/api";+import { assetTypeOf, isMediaFile } from "@/cut/lib/media";+import { isFontArchive } from "@/cut/lib/fontArchive";+import { expandLinkedFiles, isLinkedFile, isLinkedType, linkedTypeOfFile, shelfForNewItem, syncLinkedLibrary } from "@/cut/lib/linkedLibrary";+import { moveLibraryItem, uploadToLibrary, type ImportStage, type LibraryAsset } from "@/cut/lib/library";+import type { LibraryUploadState } from "@/cut/lib/libraryUpload";+import { libraryKey, libraryScope, patchLibrary, refetchLibrary } from "@/cut/lib/queries";+import type { Residency } from "@/cut/lib/residency";++export type LibraryArrival = {+ id: string;+ name: string;+ folderId: string | null;+ residency?: Residency;+ file?: File;+ source?: string;+ mediaType?: LibraryAsset["type"];+ startedAt: number;+ stage: "preparing" | "uploading" | ImportStage;+ startStage: "preparing" | "uploading" | ImportStage;+ shape?: { width: number; height: number };+ error?: string;+ run: () => Promise<void>;+};++export const useLibraryFileImports = create<{ items: LibraryArrival[] }>(() => ({ items: [] }));+const update = (fn: (items: LibraryArrival[]) => LibraryArrival[]) =>+ useLibraryFileImports.setState((state) => ({ items: fn(state.items) }));+export const dismissLibraryImport = (id: string) => update((items) => items.filter((item) => item.id !== id));++// Navigation changes the view of these jobs; the files and retries belong to the account.+subscribeEngineUser(() => useLibraryFileImports.setState({ items: [] }));+let tail = Promise.resolve();+const accepts = (file: File) => isMediaFile(file) || isLinkedFile(file);++/** Publish the whole drop synchronously, then save it through the selected backend. */+export function importLibraryFiles(+ files: FileList | File[],+ target: { residency: Residency; folderId: string | null },+ client: QueryClient,+): Promise<void> {+ const owner = currentEngineUser();+ const wanted = (id: string) => currentEngineUser() === owner && useLibraryFileImports.getState().items.some((item) => item.id === id);+ const prepare = (file: File): LibraryArrival => {+ const id = crypto.randomUUID();+ const state: LibraryUploadState = {};+ let asset: LibraryAsset | undefined;+ let running: Promise<void> | undefined;+ const work = async () => {+ if (!wanted(id)) return;+ update((items) => items.map((item) => item.id === id ? { ...item, error: undefined, startedAt: Date.now() } : item));+ try {+ const expanded = await expandLinkedFiles([file]);+ if (!wanted(id)) return;+ if (expanded.length !== 1 || expanded[0] !== file) {+ const children = expanded.filter(accepts).map(prepare);+ if (!children.length) throw new Error("This archive contains no supported files.");+ update((items) => items.flatMap((item) => item.id === id ? children : [item]));+ for (const child of children) await child.run();+ return;+ }+ if (!accepts(file)) throw new Error("This file contains no supported media.");+ if (!asset) {+ const residency = isLinkedFile(file) && !target.folderId ? await shelfForNewItem(file.size) : target.residency;+ if (!wanted(id)) return;+ asset = await uploadToLibrary(file, residency, { state, folderId: residency === "cloud" ? target.folderId : undefined });+ }+ if (!wanted(id)) return;+ if (target.folderId && asset.folderId !== target.folderId) {+ await moveLibraryItem(asset.residency, asset.id, target.folderId);+ asset.folderId = target.folderId;+ }+ if (!wanted(id)) return;+ if (!client.getQueryData(libraryKey(libraryScope()))) {+ await refetchLibrary(client);+ if (!client.getQueryData(libraryKey(libraryScope()))) throw new Error("Could not refresh the library. Retry to show the saved file.");+ }+ const landed = asset;+ patchLibrary(client, (data) => ({ ...data, assets: [landed, ...data.assets.filter((item) => item.id !== landed.id)] }));+ if (isLinkedType(asset.type)) void syncLinkedLibrary();+ dismissLibraryImport(id);+ } catch (error) {+ if (wanted(id)) update((items) => items.map((item) => item.id === id ? { ...item, error: error instanceof Error ? error.message : "Could not import this file." } : item));+ }+ };+ return {+ id, name: file.name, file, ...target,+ mediaType: assetTypeOf(file) ?? linkedTypeOfFile(file) ?? undefined,+ stage: "queued", startStage: "queued", startedAt: Date.now(),+ run: () => running ??= work().finally(() => { running = undefined; }),+ };+ };+ const batch = Array.from(files).filter((file) => accepts(file) || isFontArchive(file)).map(prepare);+ update((items) => [...items, ...batch]);+ tail = tail.then(async () => { for (const item of batch) await item.run(); });+ return tail;+}diff --git a/site/src/cut/lib/linkedLibrary/registry.ts b/site/src/cut/lib/linkedLibrary/registry.tsindex ce74714b..0c50d3b5 100644--- a/site/src/cut/lib/linkedLibrary/registry.ts+++ b/site/src/cut/lib/linkedLibrary/registry.ts@@ -101,6 +101,11 @@ export const isLinkedFile = (file: File): boolean =>export const linkedAccept = (): string =>[...kinds.values()].flatMap((k) => (k.accept ? [k.accept] : [])).join(",");+/** The library asset type a dropped OS file would shelve as, when a linked+ * kind claims it. */+export const linkedTypeOfFile = (file: File): LibraryAsset["type"] | null =>+ [...kinds.values()].find((k) => k.matches(file))?.type ?? null;+/*** A dropped batch with its archives opened.*diff --git a/site/src/cut/lib/localImportPreview.ts b/site/src/cut/lib/localImportPreview.tsnew file mode 100644index 00000000..fbedbffe--- /dev/null+++ b/site/src/cut/lib/localImportPreview.ts@@ -0,0 +1,15 @@+import { forgetRegistered, registerBlobFile } from "@/cut/lib/backend/browser/registry";+import { useLightbox, type LightboxItem } from "@/cut/lib/lightbox";++/** The viewer owns these local bytes until it closes or opens another item. */+export function openLocalImport(file: File, kind: "video" | "image" | "audio") {+ const path = `pending-preview/${crypto.randomUUID()}`;+ const src = registerBlobFile(path, file);+ const item: LightboxItem = { kind, src, name: file.name, prompt: "", assetId: null, bare: true };+ useLightbox.getState().open(item);+ const unsubscribe = useLightbox.subscribe((state) => {+ if (state.item === item) return;+ unsubscribe();+ forgetRegistered(path);+ });+}