Skip to content

Open source CapCut alternative.

TypeScript

feat(site): composer attachments appear the moment they are dropped

David Le01c92f0293ab
diff --git a/site/src/cut/components/AssetRefs.tsx b/site/src/cut/components/AssetRefs.tsx
index af091da8..e31bd62e 100644
--- a/site/src/cut/components/AssetRefs.tsx
+++ b/site/src/cut/components/AssetRefs.tsx
@@ -4,6 +4,7 @@ import { useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode,
import { Check, Copy, FileText, X } from "lucide-react";
import {
highlightMentions,
+ liveRefUrl,
mentionToken,
refToken,
sameRef,
@@ -336,12 +337,16 @@ export function RefChips({
<div className={cn("flex flex-wrap gap-2", className)}>
{refs.map((r) => {
// Handles are session-derived; show the live one, not a stored copy.
+ // The URL follows the live asset too: a chip attached mid-import
+ // starts on the file's local object URL and swaps to storage when the
+ // upload lands. liveRefUrl, because chat-owned attachments never
+ // become candidates.
const handle =
candidates.find((c) => c.scope === r.scope && c.id === r.id)?.handle ?? r.handle;
return (
<RefChip
key={`${r.scope}:${r.id}`}
- item={{ ...r, handle }}
+ item={{ ...r, handle, url: liveRefUrl(r.scope, r.id, r.url) }}
onRemove={onRemove}
onUpdate={onUpdate}
peekSide={peekSide}
diff --git a/site/src/cut/lib/assetRef.ts b/site/src/cut/lib/assetRef.ts
index 214930d8..ddd775b2 100644
--- a/site/src/cut/lib/assetRef.ts
+++ b/site/src/cut/lib/assetRef.ts
@@ -146,10 +146,12 @@ export const upsertRef = (list: AssetRef[], ref: AssetRef): AssetRef[] =>
/** A ref's fetchable URL, re-read from the live catalogs. Cloud asset URLs are
* short-lived signed R2 URLs, so a ref persisted in a saved chat thread
- * outlives its `url`; project and clip refs resolve through the current asset
- * instead. Library/stock/file URLs are stable routes or data: URLs, and a ref
- * whose asset is gone keeps the persisted URL (its fetch fails either way). */
-function liveRefUrl(scope: AssetRefScope, id: string, url: string): string {
+ * outlives its `url`; a ref attached mid-import holds its file's local object
+ * URL, which dies once the upload lands and the bytes are released. Project
+ * and clip refs resolve through the current asset for both. Library/stock/file
+ * URLs are stable routes or data: URLs, and a ref whose asset is gone keeps
+ * the persisted URL (its fetch fails either way). */
+export function liveRefUrl(scope: AssetRefScope, id: string, url: string): string {
const s = useEditor.getState();
if (scope === "project") return s.assets.find((a) => a.id === id)?.url ?? url;
if (scope === "clip") {
@@ -579,10 +581,17 @@ export function collectRefs(
const parsed = parseMentions(text, candidates);
// Re-resolve each ref's short handle from the live candidates — chips from
// drags predate handle assignment, and the model reads handles to talk
- // about attachments ("v2").
+ // about attachments ("v2") — and its URL from the live asset: a chip
+ // attached mid-import points at the file's local object URL until the
+ // upload lands and the asset swaps to its stored one. The URL reads through
+ // liveRefUrl because chat-owned attachments never become candidates.
const refs = parsed.refs.reduce(addRefOnce, chips).map((r) => {
const live = candidates.find((c) => sameRef(c, r));
- return live?.handle ? { ...r, handle: live.handle } : r;
+ return {
+ ...r,
+ url: liveRefUrl(r.scope, r.id, r.url),
+ ...(live?.handle ? { handle: live.handle } : {}),
+ };
});
return { refs, text: parsed.text };
}
diff --git a/site/src/cut/lib/refMedia.ts b/site/src/cut/lib/refMedia.ts
index 28404c9d..533825c0 100644
--- a/site/src/cut/lib/refMedia.ts
+++ b/site/src/cut/lib/refMedia.ts
@@ -2,7 +2,15 @@
import { refFromAsset, refFromTextFile, type AssetRef } from "./assetRef";
import { tagChatAsset } from "./chatAssets";
-import { enrichAsset, importFileToProject, isMediaFile, isTextFile, renderAudioSpanWav } from "./media";
+import { startUpload } from "./importQueue";
+import {
+ enrichAsset,
+ importFileToProject,
+ isMediaFile,
+ isTextFile,
+ prepareImport,
+ renderAudioSpanWav,
+} from "./media";
import { frameSink, openMedia, videoTrackOf } from "./mediaRead";
import { useEditor } from "./store";
import { formatTime } from "./time";
@@ -291,7 +299,14 @@ export const visualRefs = (refs: AssetRef[]): AssetRef[] =>
* skipped. `chatId` marks the imports chat-owned from creation: they live on
* the chat (its card, thread deletion), never in the Media panel — a drop on
* the chat composer is an attachment, not a filing. Panel drops omit it and
- * file into Media as user imports. */
+ * file into Media as user imports.
+ *
+ * A cloud drop shows its chip the moment the local bytes are probed: the ref
+ * plays from the file's own object URL while the upload queue sends the bytes
+ * behind the composer, and the asset swaps to its stored URL when they land
+ * (chips and the send paths read the live URL from the asset). The engine
+ * takes a file's bytes in one quick local call, so its imports go straight
+ * through. */
export async function refsFromDroppedFiles(
projectId: string,
files: File[],
@@ -304,11 +319,14 @@ export async function refsFromDroppedFiles(
refs.push(await refFromTextFile(file));
continue;
}
- const asset = await importFileToProject(projectId, file);
+ const pending = await prepareImport(projectId, file);
+ const asset = pending?.asset ?? (await importFileToProject(projectId, file));
if (!asset) continue;
useEditor.getState().addAsset(asset);
if (opts?.chatId) tagChatAsset(asset.id, opts.chatId);
- void enrichAsset(asset);
+ // A pending import still holds its bytes — enrich from those.
+ void enrichAsset(asset, pending?.localUrl);
+ if (pending) startUpload(projectId, pending);
refs.push(refFromAsset(asset));
} catch (err) {
console.error(`Attach failed for ${file.name}:`, err);