Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): one applyOwnership rule for generated media

David Le569ef6f6bf81
diff --git a/site/src/cut/lib/generate.ts b/site/src/cut/lib/generate.ts
index 6128ede2..7386e84a 100644
--- a/site/src/cut/lib/generate.ts
+++ b/site/src/cut/lib/generate.ts
@@ -160,6 +160,19 @@ export function creditsUrl(): string {
return `${apex}/app/settings`;
}
+/** Creation-time ownership for generated media, in one place so the rule can't
+ * drift per call site: chat-owned media lives on its chat card — never a
+ * generate-panel row or arrival badge — and everything else is a panel render
+ * (origin "generated", so it stays out of the Media panel's user imports). */
+export function applyOwnership(asset: MediaAsset, chatId: string | undefined | null): void {
+ if (chatId) {
+ asset.origin = "chat";
+ asset.chatId = chatId;
+ } else {
+ asset.origin = "generated";
+ }
+}
+
interface GenerationOutput {
dataBase64?: string;
contentType?: string;
@@ -378,13 +391,8 @@ export const useGenerate = create<GenerateState>((set, get) => {
throw new Error(body.error ?? "Could not add the image to the project.");
}
const asset: MediaAsset = { ...body, url: mediaUrl(projectId, body.fileName) };
- if (opts?.chatId) {
- // Chat-owned: lives on its chat card, never in the Image panel's
- // grid, and no arrival badge — the chat is already watching.
- asset.origin = "chat";
- asset.chatId = opts.chatId;
- adopt(projectId, asset);
- } else if (adopt(projectId, asset)) {
+ applyOwnership(asset, opts?.chatId);
+ if (adopt(projectId, asset) && !opts?.chatId) {
useGenNotify.getState().landed("image", asset.id);
}
update(job.id, { status: "done", assetId: asset.id });
@@ -483,14 +491,7 @@ export const useGenerate = create<GenerateState>((set, get) => {
const asset = await importFileToProject(projectId, file);
if (!asset) throw new Error("Could not import the generated video.");
asset.name = promptName(prompt);
- if (opts?.chatId) {
- // Chat-owned: lives on its chat card, never in the Video panel's
- // renders, and no arrival badge — the chat is already watching.
- asset.origin = "chat";
- asset.chatId = opts.chatId;
- } else {
- asset.origin = "generated"; // lives in the generate panel, not Media
- }
+ applyOwnership(asset, opts?.chatId);
if (adopt(projectId, asset)) {
if (!opts?.chatId) useGenNotify.getState().landed("video", asset.id);
opts?.onDone?.(asset);
diff --git a/site/src/cut/lib/genvideo/adapters/music.ts b/site/src/cut/lib/genvideo/adapters/music.ts
index a5584a15..63980d95 100644
--- a/site/src/cut/lib/genvideo/adapters/music.ts
+++ b/site/src/cut/lib/genvideo/adapters/music.ts
@@ -9,6 +9,7 @@
*/
import { bytesFromBase64 } from "../../bytes";
+import { applyOwnership } from "../../generate";
import { hostedPost } from "../../hosted";
import { enrichAsset, importFileToProject } from "../../media";
import { useEditor } from "../../store";
@@ -46,12 +47,7 @@ export function makeMusicRole(projectId: string, chatId?: string): MusicRole {
// Chat-owned from birth so the bed never surfaces in a panel; it's placed
// on the soundtrack as part of the run. Only the run's own thread id: the
// ambient chat context is whatever thread is open when this lands.
- if (chatId) {
- asset.origin = "chat";
- asset.chatId = chatId;
- } else {
- asset.origin = "generated";
- }
+ applyOwnership(asset, chatId);
// Only stock the store when this run's project is the one open — a
// background run whose project the user has since switched away from must
// never drop its media into whatever project is now on screen.