Open source CapCut alternative.
TypeScript
refactor(site): resolve project residency from the id, drop ?src=cloud
David Lea9892913f98c
diff --git a/site/src/app/cut/app/p/[id]/page.tsx b/site/src/app/cut/app/p/[id]/page.tsxindex 80c637eb..e2473f0f 100644--- a/site/src/app/cut/app/p/[id]/page.tsx+++ b/site/src/app/cut/app/p/[id]/page.tsx@@ -7,17 +7,12 @@ import { Editor } from "@/cut/components/Editor";// Client-only: the id comes from the URL in the browser, so the hosted shell// carries no server-rendered data. `from` and `folder` record which tab (and// folder within it) opened the project so the editor's back button returns-// there; `src` is the project's residency (cloud when set).+// there; residency is resolved from the id itself (lib/residency.ts).function ProjectEditor() {const { id } = useParams<{ id: string }>();const params = useSearchParams();return (- <Editor- projectId={id}- from={params.get("from")}- folder={params.get("folder")}- src={params.get("src")}- />+ <Editor projectId={id} from={params.get("from")} folder={params.get("folder")} />);}diff --git a/site/src/cut/components/AppSidebar.tsx b/site/src/cut/components/AppSidebar.tsxindex 55f4e81d..fa969536 100644--- a/site/src/cut/components/AppSidebar.tsx+++ b/site/src/cut/components/AppSidebar.tsx@@ -13,7 +13,7 @@ import {DialogTitle,} from "@/components/ui/dialog";import { Input } from "@/components/ui/input";-import { apiFetch, cutMode } from "@/cut/lib/backend";+import { apiFetch } from "@/cut/lib/backend";import { track } from "@/lib/analytics";import { NavUser } from "@/cut/components/NavUser";import { homeHref, projectHref, tabForPath, useCutBase, type CutTab } from "@/cut/lib/nav";@@ -45,7 +45,7 @@ export function AppSidebar() {track("project_created", { source: "sidebar" });// The sidebar creates on the globally bound backend, so the link carries// that residency.- router.push(projectHref(base, project.id, tabForPath(pathname), null, cutMode()));+ router.push(projectHref(base, project.id, tabForPath(pathname), null));} finally {setBusy(false);}diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsxindex 0cafedcc..7a546536 100644--- a/site/src/cut/components/Editor.tsx+++ b/site/src/cut/components/Editor.tsx@@ -13,6 +13,7 @@ import { enrichAsset, importFileToProject } from "@/cut/lib/media";import "@/cut/lib/genScene";import { installDevHooks } from "@/cut/lib/devHooks";import { backTarget, useCutBase } from "@/cut/lib/nav";+import { resolveProjectMode } from "@/cut/lib/residency";import { projectDuration, serializeDoc, storedAssets, useEditor } from "@/cut/lib/store";import type { MediaAsset } from "@/cut/lib/types";import { AiPanel } from "./AiPanel";@@ -28,12 +29,10 @@ export function Editor({projectId,from,folder,- src,}: {projectId: string;from?: string | null;folder?: string | null;- src?: string | null;}) {useEffect(() => installDevHooks(), []);const back = backTarget(useCutBase(), from, folder);@@ -54,20 +53,27 @@ export function Editor({const dragDepth = useRef(0);// Load the project document, then enrich assets (thumbs/waveforms) lazily.- // The link's ?src=cloud names a cloud-resident project; bind the backend- // before anything in the editor fetches.+ // Residency is a fact about the project, not the link: resolve where the id+ // lives (residency.ts) and bind the backend before anything else fetches.useEffect(() => {- setCutMode(src === "cloud" ? "cloud" : "local");- void useEditor- .getState()- .loadProject(projectId)- .then(() => {- for (const asset of useEditor.getState().assets) void enrichAsset(asset);- });+ let alive = true;+ void resolveProjectMode(projectId).then((mode) => {+ if (!alive) return;+ setCutMode(mode);+ void useEditor+ .getState()+ .loadProject(projectId)+ .then(() => {+ for (const asset of useEditor.getState().assets) void enrichAsset(asset);+ });+ });// An export still rendering after a reload rejoins on its own: the app-wide// exports dock polls the engine's job feed, so it reappears with no per-// project reconnect here.- }, [projectId, src]);+ return () => {+ alive = false;+ };+ }, [projectId]);// A cloud save hit a newer stored version (another session's edits won).// Take the newer doc through the ordinary load path — the GET returns it anddiff --git a/site/src/cut/components/ExportsDock.tsx b/site/src/cut/components/ExportsDock.tsxindex 987671d6..537092e8 100644--- a/site/src/cut/components/ExportsDock.tsx+++ b/site/src/cut/components/ExportsDock.tsx@@ -14,7 +14,6 @@ import {} from "lucide-react";import { Button } from "@/components/ui/button";import { useElapsed } from "@/cut/hooks/useElapsed";-import type { CutMode } from "@/cut/lib/backend";import { downloadExport, revealExport } from "@/cut/lib/exportClient";import {beginExportPolling,@@ -129,19 +128,11 @@ function StatusIcon({ status }: { status: string }) {return <CircleAlert className="size-4 shrink-0 text-destructive" />;}-function ProjectName({- projectId,- name,- residency,-}: {- projectId: string;- name?: string;- residency: CutMode;-}) {+function ProjectName({ projectId, name }: { projectId: string; name?: string }) {const base = useCutBase();return (<Link- href={projectHref(base, projectId, "projects", undefined, residency)}+ href={projectHref(base, projectId, "projects")}className="block truncate text-xs font-medium hover:underline"title={name || "Untitled"}>@@ -171,7 +162,7 @@ function ExportRow({ job }: { job: ExportJob }) {<div className="relative flex items-center gap-2.5 px-3 py-2"><StatusIcon status={job.status} /><div className="min-w-0 flex-1">- <ProjectName projectId={job.projectId} name={job.projectName} residency={job.residency} />+ <ProjectName projectId={job.projectId} name={job.projectName} /><divclassName={cn("truncate text-[11px] tabular-nums",@@ -258,7 +249,7 @@ function LocalRowView({ row }: { row: LocalRow }) {<div className="flex items-center gap-2.5 px-3 py-2"><StatusIcon status={row.status} /><div className="min-w-0 flex-1">- <ProjectName projectId={row.projectId} name={row.projectName} residency={row.residency} />+ <ProjectName projectId={row.projectId} name={row.projectName} /><divclassName={cn("truncate text-[11px]",diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsxindex da766287..261998b6 100644--- a/site/src/cut/components/ProjectsHome.tsx+++ b/site/src/cut/components/ProjectsHome.tsx@@ -298,7 +298,7 @@ export function ProjectsHome() {});const project = (await res.json()) as ProjectSummary;track("project_created", { source: "projects_home" });- router.push(projectHref(base, project.id, "projects", openFolder, r0));+ router.push(projectHref(base, project.id, "projects", openFolder));} finally {setBusy(false);}@@ -314,7 +314,7 @@ export function ProjectsHome() {});const project = (await res.json()) as ProjectSummary;track("project_created", { source: "projects_home" });- router.push(projectHref(base, project.id, "projects", folderId, r));+ router.push(projectHref(base, project.id, "projects", folderId));};// Turn a batch of desktop files into projects filed under `folderId`. Each@@ -490,7 +490,7 @@ export function ProjectsHome() {toggleSelect(p.id);return;}- router.push(projectHref(base, p.id, "projects", openFolder, r));+ router.push(projectHref(base, p.id, "projects", openFolder));}}>{/* Vertical 9:16 tile — the project is mobile video, show it that way. */}@@ -567,7 +567,7 @@ export function ProjectsHome() {toggleSelect(p.id);return;}- router.push(projectHref(base, p.id, "projects", openFolder, r));+ router.push(projectHref(base, p.id, "projects", openFolder));}}><span className="flex min-w-0 items-center gap-2.5">diff --git a/site/src/cut/components/TopBar.tsx b/site/src/cut/components/TopBar.tsxindex 28596a73..defd9bd2 100644--- a/site/src/cut/components/TopBar.tsx+++ b/site/src/cut/components/TopBar.tsx@@ -74,7 +74,7 @@ export function TopBar({await localBackend.fetch(`/api/cut/projects/${projectId}`, { method: "DELETE" }).catch(() => {});- window.location.href = projectHref(base, newId, "projects", null, "cloud");+ window.location.href = projectHref(base, newId, "projects", null);} catch (e) {setMoveError(e instanceof Error && e.message ? e.message : "Could not move the project."diff --git a/site/src/cut/lib/nav.tsx b/site/src/cut/lib/nav.tsxindex 4ef6a455..45c7d44c 100644--- a/site/src/cut/lib/nav.tsx+++ b/site/src/cut/lib/nav.tsx@@ -40,19 +40,17 @@ export function homeHref(base: string, tab: CutTab, folder?: string | null): str/** Project editor URL that remembers the tab (and folder, when open) it was* opened from. The default origin carries no query so the common URL stays- * clean. Cloud-resident projects carry ?src=cloud so the editor binds the- * cloud backend before it loads; local links are unchanged. */+ * clean. Residency never rides the URL — the editor resolves where the id+ * lives (lib/residency.ts). */export function projectHref(base: string,id: string,from: CutTab,- folder?: string | null,- residency?: "local" | "cloud"+ folder?: string | null): string {const params = new URLSearchParams();if (from === "library") params.set("from", "library");if (folder) params.set("folder", folder);- if (residency === "cloud") params.set("src", "cloud");const qs = params.toString();return `${base}/p/${id}${qs ? `?${qs}` : ""}`;}diff --git a/site/src/cut/lib/residency.ts b/site/src/cut/lib/residency.tsnew file mode 100644index 00000000..f5baea58--- /dev/null+++ b/site/src/cut/lib/residency.ts@@ -0,0 +1,23 @@+// Where a project lives is a fact about the project, not the link that opened+// it: the editor resolves residency by asking, so URLs stay clean. With the+// web-mode flag off everything is local (today's behavior). Otherwise the+// engine — when one is already connected; loopback is never probed here, so+// the browser's local-network ask can't fire — is asked whether it owns the+// id, and a miss means the project is cloud-resident.+import { engineOrigin, servedFromEngine } from "./api";+import { localBackend } from "./backend/local";+import type { CutMode } from "./backend/types";+import { webModeEnabled } from "./flags";++export async function resolveProjectMode(projectId: string): Promise<CutMode> {+ if (!webModeEnabled()) return "local";+ if (!servedFromEngine() && !engineOrigin()) return "cloud";+ try {+ const res = await localBackend.fetch(`/api/cut/projects/${projectId}`);+ if (res.ok) return "local";+ } catch {+ // The engine dropped since the gate probed it; the cloud copy (if any)+ // is the only reachable one.+ }+ return "cloud";+}