Open source CapCut alternative.
TypeScript
fix(notes): keep root notes in the Notes tab [rebuild]
David Le45f743a2d425
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex faff4fc7..9e98eea5 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -436,7 +436,7 @@ export function LibraryView() {const movingNotes = (notes.data?.notes ?? []).filter((n) => ids.includes(n.id));if (movingNotes.length) {try {- const saved = await Promise.all(movingNotes.map((n) => saveNote({ ...n, libraryLocation: { folderId, residency: target ?? n.libraryLocation?.residency ?? landing(null).residency } })));+ const saved = await Promise.all(movingNotes.map((n) => saveNote({ ...n, libraryLocation: folderId && target ? { folderId, residency: target } : null })));const byId = new Map(saved.map((n) => [n.id, n]));patchNotes(client, (data) => ({ ...data, notes: data.notes.map((n) => byId.get(n.id) ?? n) }));} catch {@@ -656,7 +656,7 @@ export function LibraryView() {if (staleFolder) router.replace(homeHref(base, "library"));}, [staleFolder, router, base]);const hasContent =- (notes.data?.notes.length ?? 0) > 0 ||+ (notes.data?.notes.some((note) => !!note.libraryLocation?.folderId) ?? false) ||all.length > 0 ||folders.length > 0 ||templates.length > 0 ||@@ -795,7 +795,7 @@ export function LibraryView() {/>) : null}- <NotesView ref={noteView} library={{ folderId: openFolder, residency: openOwner ?? target, name: trail.at(-1)?.name ?? "Library" }} />+ {openFolder && <NotesView ref={noteView} library={{ folderId: openFolder, residency: openOwner ?? target, name: trail.at(-1)?.name ?? "Library" }} />}{!library.data && library.isPending && shownPending.length === 0 ? (<div className="grid place-items-center py-24 text-muted-foreground">@@ -947,7 +947,7 @@ export function LibraryView() {<SelectionMenu menu={ctx.menu} onClose={ctx.close}>{ctx.menu?.ids.length === 0 ? <>- <DropdownMenuItem onClick={() => noteView.current?.create()}><StickyNote /> Add note</DropdownMenuItem>+ {openFolder && <DropdownMenuItem onClick={() => noteView.current?.create()}><StickyNote /> Add note</DropdownMenuItem>}<DropdownMenuItem onClick={() => setAddOpen(true)}><Upload /> Add media</DropdownMenuItem></> : ctxFolder ? (<FolderMenuItemsdiff --git a/site/src/cut/components/NotesView.tsx b/site/src/cut/components/NotesView.tsxindex da7d7745..b0a8624b 100644--- a/site/src/cut/components/NotesView.tsx+++ b/site/src/cut/components/NotesView.tsx@@ -127,7 +127,7 @@ const draftOf = (n: CutNote): NoteDraft => ({* top level has. */export type NotesViewHandle = { create: () => void };type Props = {- library?: { folderId: string | null; residency: Residency; name: string };+ library?: { folderId: string; residency: Residency; name: string };ref?: Ref<NotesViewHandle>;};export function NotesView({ library, ref }: Props = {}) {@@ -162,7 +162,7 @@ export function NotesView({ library, ref }: Props = {}) {const trail = folderTrail(folders, openFolder);const shownFolders = folders.filter((f) => f.parentId === openFolder);const shown = list.filter((n) => library- ? noteInLibraryFolder(n, library.folderId, library.folderId === null ? undefined : library.residency)+ ? noteInLibraryFolder(n, library.folderId, library.residency): (n.folderId ?? null) === openFolder);// What the composer shows: the buffer while it belongs to the note the URL// names, and the stored note otherwise. A note nobody has typed into has nodiff --git a/site/src/cut/lib/noteReference.test.ts b/site/src/cut/lib/noteReference.test.tsindex 504aafb3..72f2b654 100644--- a/site/src/cut/lib/noteReference.test.ts+++ b/site/src/cut/lib/noteReference.test.ts@@ -9,16 +9,13 @@ import { attachedAssetsBlock, AI_TOOLS } from "@/cut/server/ai/catalog";import { READ_COMMANDS, COMMAND_NAMES } from "@/clients/chatgpt/server/catalog";const note: CutNote = { id: "script", title: 'Brand "Voice"', body: "First draft", colorIndex: 2, folderId: "phone-folder", labelIds: ["brand"], createdAt: 1, updatedAt: 1, deletedAt: null, libraryLocation: { folderId: "assets", residency: "browser" } };describe("note references", () => {- test("existing Notes stay outside the Library; explicit roots and folders retain placement", () => {+ test("root notes stay in Notes and Library notes require a folder", () => {expect(noteInLibraryFolder({ libraryLocation: null }, null)).toBe(false);expect(noteInLibraryFolder({}, null)).toBe(false);for (const residency of ["browser", "local", "cloud"] as const) {- const root = { libraryLocation: { folderId: null, residency } };const filed = { libraryLocation: { folderId: "assets", residency } };- expect(noteInLibraryFolder(root, null)).toBe(true);expect(noteInLibraryFolder(filed, null)).toBe(false);expect(noteInLibraryFolder(filed, "assets", residency)).toBe(true);- expect(noteInLibraryFolder(root, "assets", residency)).toBe(false);expect(noteInLibraryFolder(filed, "assets", residency === "cloud" ? "browser" : "cloud")).toBe(false);}});diff --git a/site/src/cut/lib/noteReference.ts b/site/src/cut/lib/noteReference.tsindex 33b6fe15..1338269a 100644--- a/site/src/cut/lib/noteReference.ts+++ b/site/src/cut/lib/noteReference.ts@@ -6,14 +6,14 @@ import { normalizeLink } from "@/cut/lib/link";export const noteIdSchema = z.string().regex(/^[\w-]{1,64}$/);export const noteLocationSchema = z.object({- folderId: z.string().min(1).max(128).nullable(),+ folderId: z.string().min(1).max(128),residency: z.enum(["browser", "local", "cloud"]),});export type NoteLocation = z.infer<typeof noteLocationSchema>;-export const NOTE_LIBRARY_LOCATION_DESCRIPTION = "Library placement requires a residency and folderId; folderId:null selects that shelf's Library root. libraryLocation:null keeps the note in Notes without Library placement.";+export const NOTE_LIBRARY_LOCATION_DESCRIPTION = "Library placement requires a residency and a folderId. libraryLocation:null keeps the note in the Notes tab. Library shows notes inside folders only.";export function noteInLibraryFolder(note: Pick<CutNote, "libraryLocation">, folderId: string | null, residency?: NoteLocation["residency"]): boolean {const location = note.libraryLocation;- return !!location && location.folderId === folderId && (!residency || location.residency === residency);+ return folderId !== null && !!location && location.folderId === folderId && (!residency || location.residency === residency);}export const noteUnfileSchema = z.object({ residency: z.enum(["browser", "local", "cloud"]), folderIds: z.array(z.string().min(1).max(128)).max(10000) });export const noteReadSchema = z.object({ id: noteIdSchema.optional(), link: z.string().optional() });diff --git a/site/src/cut/lib/notes.ts b/site/src/cut/lib/notes.tsindex fe80db1c..f70cc675 100644--- a/site/src/cut/lib/notes.ts+++ b/site/src/cut/lib/notes.ts@@ -155,7 +155,7 @@ export async function saveNoteFolder(export async function unfileLibraryNotes(residency: "browser" | "local", folderIds: string[]): Promise<void> {const res = await notesFetch("/api/cut/notes/library-location", json({ residency, folderIds }));- if (!res.ok) throw new Error("Could not move the folder’s notes to the Library root.");+ if (!res.ok) throw new Error("Could not return the folder’s notes to Notes.");}/** Delete a folder. What it held comes up one level. */diff --git a/site/src/cut/server/cloud/notes.test.ts b/site/src/cut/server/cloud/notes.test.tsindex a453f0a8..74f0dde1 100644--- a/site/src/cut/server/cloud/notes.test.ts+++ b/site/src/cut/server/cloud/notes.test.ts@@ -51,12 +51,11 @@ describe("synced Library notes", () => {expect((await api.put("owner", "n1", request({ updatedAt: 2, libraryLocation: null }))).status).toBe(200);expect(db.cutNote.update.mock.calls[0][0]).toMatchObject({ data: { libraryLocation: Prisma.DbNull } });});- test("Library roots use explicit placement on every shelf", async () => {+ test("Library placement requires a folder on every shelf", async () => {for (const residency of ["browser", "local", "cloud"] as const) {const { api, db } = harness();- const libraryLocation = { residency, folderId: null };- expect((await api.put("owner", "n1", request({ updatedAt: 2, libraryLocation }))).status).toBe(200);- expect(db.cutNote.update.mock.calls[0][0]).toMatchObject({ data: { libraryLocation } });+ expect((await api.put("owner", "n1", request({ updatedAt: 2, libraryLocation: { residency, folderId: null } }))).status).toBe(400);+ expect(db.cutNote.update.mock.calls.length).toBe(0);expect(db.cutFolder.findFirst.mock.calls.length).toBe(0);}});@@ -72,6 +71,6 @@ describe("synced Library notes", () => {expect(db.cutNote.updateMany.mock.calls[0][0]).toEqual({ where: { userId: "owner", AND: [{ libraryLocation: { path: ["residency"], equals: "local" } },{ OR: ["brand", "child"].map((id) => ({ libraryLocation: { path: ["folderId"], equals: id } })) },- ] }, data: { libraryLocation: { residency: "local", folderId: null } } });+ ] }, data: { libraryLocation: Prisma.DbNull } });});});diff --git a/site/src/cut/server/cloud/notes.ts b/site/src/cut/server/cloud/notes.tsindex fd34c75f..2fb7731a 100644--- a/site/src/cut/server/cloud/notes.ts+++ b/site/src/cut/server/cloud/notes.ts@@ -145,7 +145,7 @@ export function createNotesCloud(db: typeof prisma = prisma) {{ libraryLocation: { path: ["residency"], equals: residency } },{ OR: folderIds.map((id) => ({ libraryLocation: { path: ["folderId"], equals: id } })) },] },- data: { libraryLocation: { residency, folderId: null } },+ data: { libraryLocation: Prisma.DbNull },});}return {@@ -202,7 +202,7 @@ export function createNotesCloud(db: typeof prisma = prisma) {if (parsedLocation && !parsedLocation.success) return err("Invalid Library location.", 400);const location = body.libraryLocation === undefined ? undefined: parsedLocation?.success ? parsedLocation.data : null;- if (location?.residency === "cloud" && location.folderId !== null && !await db.cutFolder.findFirst({ where: { id: location.folderId, userId, scope: "library" }, select: { id: true } })) {+ if (location?.residency === "cloud" && !await db.cutFolder.findFirst({ where: { id: location.folderId, userId, scope: "library" }, select: { id: true } })) {return err("Library folder not found.", 404);}// Over the cap the write is refused outright: truncating here would