Skip to content

Open source CapCut alternative.

TypeScript

fix(site): new projects carry the last-used aspect from creation [rebuild]

David Lee48277575cce
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index c80230aa..e1ab2b2b 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -60,6 +60,7 @@ import {
useLocalCompute,
} from "@/cut/lib/backend/hooks";
import { dropCachedDoc, seedNewProjectDoc } from "@/cut/lib/docCache";
+import { lastChosenAspect } from "@/cut/lib/store";
import {
backendFor,
patchProjects,
@@ -380,14 +381,17 @@ export function ProjectsHome() {
// for a document we can describe in full.
const openNewProject = async (r: Residency, pname: string, folderId: string | null) => {
if (!live(r)) return;
+ // The frame the editor would open this project at is written into the doc
+ // at creation, so the card shows that shape before the editor's first save.
+ const aspect = lastChosenAspect() ?? undefined;
const res = await backendFor(r).fetch("/api/cut/projects", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ name: pname, folderId }),
+ body: JSON.stringify({ name: pname, folderId, aspect }),
});
const project = (await res.json()) as ProjectSummary;
patch(r, (s) => ({ ...s, projects: [project, ...s.projects] }));
- seedNewProjectDoc(project.id, project.name, r);
+ seedNewProjectDoc(project.id, project.name, r, aspect);
track("project_created", { source: "projects_home" });
router.push(projectHref(base, project.id, "projects", folderId));
};
diff --git a/site/src/cut/lib/backend/browser/router.ts b/site/src/cut/lib/backend/browser/router.ts
index 622c5b9d..a55a9206 100644
--- a/site/src/cut/lib/backend/browser/router.ts
+++ b/site/src/cut/lib/backend/browser/router.ts
@@ -37,11 +37,16 @@ async function listSummaries(): Promise<Response> {
async function createProject(req: Request): Promise<Response> {
try {
- const { name, folderId } = (await req.json()) as { name?: string; folderId?: string | null };
+ const { name, folderId, aspect } = (await req.json()) as {
+ name?: string;
+ folderId?: string | null;
+ aspect?: string;
+ };
const id = crypto.randomUUID().slice(0, 10);
const displayName = (name ?? "Untitled").trim() || "Untitled";
const folders = (await store.readIndex()).folders;
const folder = folderId && folders.some((f) => f.id === folderId) ? folderId : null;
+ const frame = normalizeAspect(aspect);
const now = Date.now();
const doc: ProjectDoc = {
version: 1,
@@ -53,6 +58,7 @@ async function createProject(req: Request): Promise<Response> {
clips: [],
audioClips: [],
overlays: [],
+ ...(frame ? { aspect: frame } : {}),
...(folder ? { folderId: folder } : {}),
};
await store.writeDoc(id, doc);
diff --git a/site/src/cut/lib/docCache.ts b/site/src/cut/lib/docCache.ts
index 90a922c8..39d11db0 100644
--- a/site/src/cut/lib/docCache.ts
+++ b/site/src/cut/lib/docCache.ts
@@ -21,7 +21,7 @@
import { getBackend } from "./backend";
import type { CutMode } from "./backend/types";
import { dropSnapshot, readSnapshot, snapshotKey, writeSnapshot } from "./cache";
-import type { ProjectDoc } from "./types";
+import type { Aspect, ProjectDoc } from "./types";
/** Reuse a stored batch only with this much life left, so a project opened on
* a snapshot is never handed links that expire while it is being edited. */
@@ -147,11 +147,17 @@ export function writeCachedMediaLinks(
* else. Seeding it lets the editor the user is about to land in open on the
* first frame instead of waiting out a round trip for a document we can
* already describe in full. */
-export function seedNewProjectDoc(projectId: string, name: string, kind: CutMode) {
+export function seedNewProjectDoc(
+ projectId: string,
+ name: string,
+ kind: CutMode,
+ aspect?: Aspect
+) {
writeCachedDoc(
projectId,
{
name,
+ ...(aspect ? { aspect } : {}),
assets: [],
clips: [],
audioClips: [],
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 8542bf7b..e7f0d938 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -1331,7 +1331,7 @@ export function cutTranscribeSpec(
* deliberate, so the first-footage guess leaves it alone. */
const LAST_ASPECT_KEY = "cut-last-aspect";
-function lastChosenAspect(): Aspect | null {
+export function lastChosenAspect(): Aspect | null {
try {
if (typeof localStorage === "undefined") return null;
return normalizeAspect(localStorage.getItem(LAST_ASPECT_KEY));
diff --git a/site/src/cut/server/cloud/projects.ts b/site/src/cut/server/cloud/projects.ts
index 1d4305f9..82b2a8b7 100644
--- a/site/src/cut/server/cloud/projects.ts
+++ b/site/src/cut/server/cloud/projects.ts
@@ -138,10 +138,15 @@ export const projectsCloud = {
async create(userId: string, req: Request) {
try {
- const { name, folderId } = (await req.json()) as { name?: string; folderId?: string | null };
+ const { name, folderId, aspect } = (await req.json()) as {
+ name?: string;
+ folderId?: string | null;
+ aspect?: string;
+ };
const folder = folderId
? await prisma.cutFolder.findFirst({ where: { id: folderId, userId, scope: "project" } })
: null;
+ const frame = normalizeAspect(aspect);
const now = Date.now();
const doc: ProjectDoc = {
version: 1,
@@ -152,6 +157,7 @@ export const projectsCloud = {
clips: [],
audioClips: [],
overlays: [],
+ ...(frame ? { aspect: frame } : {}),
};
const row = await prisma.cutProject.create({
data: { userId, name: doc.name, doc: asJson(doc), folderId: folder?.id ?? null },
diff --git a/site/src/cut/server/http/projects.ts b/site/src/cut/server/http/projects.ts
index 8f75c1a7..aa069bda 100644
--- a/site/src/cut/server/http/projects.ts
+++ b/site/src/cut/server/http/projects.ts
@@ -83,11 +83,14 @@ export const projectsApi = {
async create(req: Request) {
try {
- const { name, folderId } = (await req.json()) as {
+ const { name, folderId, aspect } = (await req.json()) as {
name?: string;
folderId?: string | null;
+ aspect?: string;
};
- return Response.json(await createProject(name ?? "Untitled", folderId ?? null));
+ return Response.json(
+ await createProject(name ?? "Untitled", folderId ?? null, normalizeAspect(aspect))
+ );
} catch (e) {
return caught(e, "Could not create project.");
}
diff --git a/site/src/cut/server/projects.ts b/site/src/cut/server/projects.ts
index 4fc80735..f916ca24 100644
--- a/site/src/cut/server/projects.ts
+++ b/site/src/cut/server/projects.ts
@@ -1,7 +1,7 @@
import fsSync from "node:fs";
import { cp, mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
import path from "node:path";
-import type { ProjectDoc, ProjectFolder, ProjectSummary } from "@/cut/lib/types";
+import type { Aspect, ProjectDoc, ProjectFolder, ProjectSummary } from "@/cut/lib/types";
import { cutDataRoot } from "./dataDir";
import { assertLocalRuntime } from "./local-only";
import { exists, uniqueName, writeJsonAtomic } from "./util";
@@ -347,7 +347,8 @@ export async function writeProject(id: string, doc: ProjectDoc) {
export async function createProject(
name: string,
- folderId: string | null = null
+ folderId: string | null = null,
+ aspect: Aspect | null = null
): Promise<ProjectSummary> {
const id = crypto.randomUUID().slice(0, 10);
const displayName = name.trim() || "Untitled";
@@ -370,6 +371,7 @@ export async function createProject(
clips: [],
audioClips: [],
overlays: [],
+ ...(aspect ? { aspect } : {}),
...(folder ? { folderId: folder } : {}),
};
await writeJsonAtomic(path.join(dir, "project.json"), doc);