Skip to content

Open source CapCut alternative.

TypeScript

feat(site): local-first project creation with Move to Cloud in the editor

David Lec1aecc916537
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index b12092ca..da766287 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -56,10 +56,11 @@ import { authClient } from "@/lib/auth-client";
import { clearProjectThreads } from "@/cut/lib/chatThreads";
import { useGenerate } from "@/cut/lib/generate";
import { useGenScene } from "@/cut/lib/genScene";
-import { createProjectFromFile, isMediaFile, uploadProjectMediaTo } from "@/cut/lib/media";
+import { createProjectFromFile, isMediaFile } from "@/cut/lib/media";
+import { copyProjectAcross } from "@/cut/lib/projectCopy";
import { homeHref, projectHref, useCutBase } from "@/cut/lib/nav";
import { formatTime } from "@/cut/lib/time";
-import type { ProjectDoc, ProjectFolder, ProjectSummary } from "@/cut/lib/types";
+import type { ProjectFolder, ProjectSummary } from "@/cut/lib/types";
import { cn } from "@/lib/utils";
import { buildDragGhost, FolderCrumb, FolderShelf, formatBytes, Marquee } from "./desktopFolders";
@@ -366,60 +367,18 @@ export function ProjectsHome() {
}
};
- // Copy a project into the other residency: read the source doc, create the
- // target project, move every media file's bytes across (in doc order — the
- // fresh target dedupes only against names this copy already claimed), remap
- // asset fileNames, then save the doc. A failure deletes the half-made copy.
+ // Copy a project into the other residency (projectCopy.ts does the doc +
+ // media transfer and cleans up a half-made copy itself).
const duplicateAcross = async (source: Residency, p: ProjectSummary) => {
const target: Residency = source === "cloud" ? "local" : "cloud";
- const src = backendFor(source);
- const dst = backendFor(target);
setDupError(null);
setBusy(true);
- let created: string | null = null;
try {
- const docRes = await src.fetch(`/api/cut/projects/${p.id}`);
- if (!docRes.ok) throw new Error("Could not read the project.");
- const doc = (await docRes.json()) as ProjectDoc;
- const copyName = `${doc.name || p.name} copy`;
- const createRes = await dst.fetch("/api/cut/projects", {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ name: copyName }),
+ await copyProjectAcross(backendFor(source), backendFor(target), p.id, {
+ rename: (n) => `${n || p.name} copy`,
});
- const summary = (await createRes.json()) as ProjectSummary;
- if (!createRes.ok || !summary.id) throw new Error("Could not create the copy.");
- created = summary.id;
- const assets = Array.isArray(doc.assets) ? doc.assets : [];
- const names = new Map<string, string>();
- for (const a of assets) {
- if (names.has(a.fileName)) continue;
- const bytes = await src.fetch(
- `/api/cut/projects/${p.id}/media/${encodeURIComponent(a.fileName)}`
- );
- if (!bytes.ok) throw new Error(`Could not read “${a.name}”.`);
- names.set(
- a.fileName,
- await uploadProjectMediaTo(dst, summary.id, await bytes.blob(), a.fileName)
- );
- }
- const copied: ProjectDoc = {
- ...doc,
- name: copyName,
- folderId: null,
- assets: assets.map((a) => ({ ...a, fileName: names.get(a.fileName) ?? a.fileName })),
- };
- const putRes = await dst.fetch(`/api/cut/projects/${summary.id}`, {
- method: "PUT",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify(copied),
- });
- if (!putRes.ok) throw new Error("Could not save the copy.");
await refresh(target);
} catch (e) {
- if (created) {
- void dst.fetch(`/api/cut/projects/${created}`, { method: "DELETE" }).catch(() => {});
- }
setDupError(
e instanceof Error && e.message ? e.message : "Could not duplicate the project."
);
@@ -648,14 +607,16 @@ export function ProjectsHome() {
/>
</div>
))}
- <button
- type="button"
- data-no-marquee
- onClick={() => void newProjectHere(r)}
- className="flex w-full items-center gap-2.5 px-4 py-2.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted/50 hover:text-foreground"
- >
- <Plus className="size-4" /> New project
- </button>
+ {!(dual && r === "cloud") && (
+ <button
+ type="button"
+ data-no-marquee
+ onClick={() => void newProjectHere(r)}
+ className="flex w-full items-center gap-2.5 px-4 py-2.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted/50 hover:text-foreground"
+ >
+ <Plus className="size-4" /> New project
+ </button>
+ )}
</div>
);
@@ -770,25 +731,11 @@ export function ProjectsHome() {
<FolderPlus data-icon="inline-start" /> New folder
</Button>
))}
- {dual && openFolder === null ? (
- <DropdownMenu>
- <DropdownMenuTrigger render={<Button />}>
- <Plus data-icon="inline-start" /> New project
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end">
- <DropdownMenuItem onClick={() => void newProjectHere("local")}>
- <Laptop /> {RESIDENCY_LABEL.local}
- </DropdownMenuItem>
- <DropdownMenuItem onClick={() => void newProjectHere("cloud")}>
- <Cloud /> {RESIDENCY_LABEL.cloud}
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
- ) : (
- <Button onClick={() => void newProjectHere(dual ? (folderOwner ?? "local") : r0)}>
- <Plus data-icon="inline-start" /> New project
- </Button>
- )}
+ {/* Local-first: with the engine present, creation is always local —
+ a project reaches the cloud by moving it from the editor. */}
+ <Button onClick={() => void newProjectHere(dual ? (folderOwner ?? "local") : r0)}>
+ <Plus data-icon="inline-start" /> New project
+ </Button>
{anyProjects && (
<div className="flex rounded-lg border border-border bg-card p-0.5">
<Button
diff --git a/site/src/cut/components/TopBar.tsx b/site/src/cut/components/TopBar.tsx
index b13b13f0..28596a73 100644
--- a/site/src/cut/components/TopBar.tsx
+++ b/site/src/cut/components/TopBar.tsx
@@ -2,15 +2,27 @@
import { useState } from "react";
import Link from "next/link";
-import { Check, ChevronDown, ChevronLeft, Loader2, Mic, Monitor, Smartphone, Sparkles, Upload, Video } from "lucide-react";
+import { Check, ChevronDown, ChevronLeft, Cloud, Ellipsis, Loader2, Mic, Monitor, Smartphone, Sparkles, Upload, Video } from "lucide-react";
import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
-import { backTarget, useCutBase } from "@/cut/lib/nav";
+import { cloudBackend } from "@/cut/lib/backend/cloud";
+import { useCutMode } from "@/cut/lib/backend/hooks";
+import { localBackend } from "@/cut/lib/backend/local";
+import { useWebMode } from "@/cut/lib/flags";
+import { backTarget, projectHref, useCutBase } from "@/cut/lib/nav";
+import { copyProjectAcross } from "@/cut/lib/projectCopy";
import { useEditor } from "@/cut/lib/store";
import { ASPECT_LABEL, type Aspect } from "@/cut/lib/types";
import { cn } from "@/lib/utils";
@@ -25,7 +37,8 @@ export function TopBar({
from?: string | null;
folder?: string | null;
}) {
- const back = backTarget(useCutBase(), from, folder);
+ const base = useCutBase();
+ const back = backTarget(base, from, folder);
const hasClips = useEditor((s) => s.clips.length > 0);
const aspect = useEditor((s) => s.aspect);
const projectName = useEditor((s) => s.projectName);
@@ -34,6 +47,43 @@ export function TopBar({
const [draft, setDraft] = useState("");
const [recordMode, setRecordMode] = useState<RecordMode | null>(null);
+ // "Move to Cloud" (cut-web-mode flag): copies this local project — doc and
+ // every media file — to the cloud, deletes the local original, and reopens
+ // the editor on the cloud copy.
+ const webMode = useWebMode();
+ const cutMode = useCutMode();
+ const canMoveToCloud = webMode && cutMode === "local";
+ const [moveOpen, setMoveOpen] = useState(false);
+ const [moving, setMoving] = useState(false);
+ const [moveProgress, setMoveProgress] = useState<string | null>(null);
+ const [moveError, setMoveError] = useState<string | null>(null);
+
+ const moveToCloud = async () => {
+ const projectId = useEditor.getState().projectId;
+ if (!projectId) return;
+ setMoving(true);
+ setMoveError(null);
+ try {
+ // Let a pending autosave land so the copy reads the current cut.
+ for (let i = 0; i < 40 && useEditor.getState().saveState !== "saved"; i++) {
+ await new Promise((r) => setTimeout(r, 250));
+ }
+ const newId = await copyProjectAcross(localBackend, cloudBackend, projectId, {
+ onProgress: (done, total) => setMoveProgress(`Moving media ${done}/${total}…`),
+ });
+ await localBackend
+ .fetch(`/api/cut/projects/${projectId}`, { method: "DELETE" })
+ .catch(() => {});
+ window.location.href = projectHref(base, newId, "projects", null, "cloud");
+ } catch (e) {
+ setMoveError(
+ e instanceof Error && e.message ? e.message : "Could not move the project."
+ );
+ setMoving(false);
+ setMoveProgress(null);
+ }
+ };
+
const commitName = () => {
setEditing(false);
const name = draft.trim();
@@ -174,7 +224,48 @@ export function TopBar({
>
<Upload data-icon="inline-start" /> Export
</Button>
+ {canMoveToCloud && (
+ <DropdownMenu>
+ <DropdownMenuTrigger
+ render={
+ <Button
+ variant="ghost"
+ size="icon-sm"
+ aria-label="Project options"
+ title="Project options"
+ />
+ }
+ >
+ <Ellipsis />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem onClick={() => setMoveOpen(true)}>
+ <Cloud /> Move to Cloud
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )}
</div>
+ {moveOpen && (
+ <Dialog open onOpenChange={(open) => !open && !moving && setMoveOpen(false)}>
+ <DialogContent className="sm:max-w-sm">
+ <DialogHeader>
+ <DialogTitle>Move to Cloud</DialogTitle>
+ </DialogHeader>
+ <p className="text-sm text-muted-foreground">
+ Copies this project and its media to the cloud, then removes it
+ from this Mac. Exports rendered here stay behind.
+ </p>
+ {moveError && <p className="text-sm text-red-600">{moveError}</p>}
+ <DialogFooter className="mt-2">
+ <Button disabled={moving} className="w-full" onClick={() => void moveToCloud()}>
+ {moving && <Loader2 className="animate-spin" data-icon="inline-start" />}
+ {moving ? (moveProgress ?? "Moving…") : "Move to Cloud"}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ )}
</header>
);
}
diff --git a/site/src/cut/lib/projectCopy.ts b/site/src/cut/lib/projectCopy.ts
new file mode 100644
index 00000000..f177949c
--- /dev/null
+++ b/site/src/cut/lib/projectCopy.ts
@@ -0,0 +1,69 @@
+// Copy a project between backends: read the source doc, create the target
+// project, move every media file's bytes across (in doc order — the fresh
+// target dedupes only against names this copy already claimed), remap asset
+// fileNames, then save the doc. A failure deletes the half-made copy. Shared
+// by the projects home's "Duplicate to cloud/Mac" and the editor's move.
+import type { CutBackend } from "./backend/types";
+import { uploadProjectMediaTo } from "./media";
+import type { ProjectDoc } from "./types";
+
+export async function copyProjectAcross(
+ src: CutBackend,
+ dst: CutBackend,
+ projectId: string,
+ opts: {
+ /** Target name from the source doc's name; defaults to keeping it. */
+ rename?: (docName: string) => string;
+ /** Called per media file as its bytes land on the target. */
+ onProgress?: (done: number, total: number) => void;
+ } = {}
+): Promise<string> {
+ let created: string | null = null;
+ try {
+ const docRes = await src.fetch(`/api/cut/projects/${projectId}`);
+ if (!docRes.ok) throw new Error("Could not read the project.");
+ const doc = (await docRes.json()) as ProjectDoc;
+ const name = opts.rename ? opts.rename(doc.name ?? "") : (doc.name ?? "");
+ const createRes = await dst.fetch("/api/cut/projects", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ name }),
+ });
+ const summary = (await createRes.json()) as { id?: string };
+ if (!createRes.ok || !summary.id) throw new Error("Could not create the project.");
+ created = summary.id;
+ const assets = Array.isArray(doc.assets) ? doc.assets : [];
+ const names = new Map<string, string>();
+ const unique = new Set(assets.map((a) => a.fileName)).size;
+ for (const a of assets) {
+ if (names.has(a.fileName)) continue;
+ const bytes = await src.fetch(
+ `/api/cut/projects/${projectId}/media/${encodeURIComponent(a.fileName)}`
+ );
+ if (!bytes.ok) throw new Error(`Could not read “${a.name}”.`);
+ names.set(
+ a.fileName,
+ await uploadProjectMediaTo(dst, summary.id, await bytes.blob(), a.fileName)
+ );
+ opts.onProgress?.(names.size, unique);
+ }
+ const copied: ProjectDoc = {
+ ...doc,
+ name,
+ folderId: null,
+ assets: assets.map((a) => ({ ...a, fileName: names.get(a.fileName) ?? a.fileName })),
+ };
+ const putRes = await dst.fetch(`/api/cut/projects/${summary.id}`, {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(copied),
+ });
+ if (!putRes.ok) throw new Error("Could not save the project.");
+ return summary.id;
+ } catch (e) {
+ if (created) {
+ void dst.fetch(`/api/cut/projects/${created}`, { method: "DELETE" }).catch(() => {});
+ }
+ throw e;
+ }
+}