Skip to content

Open source CapCut alternative.

TypeScript

fix(notes): retain notes created at Library root [rebuild]

David Le90b77f5382db
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index c9b78d32..832ae2d6 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 && target ? { folderId, residency: target } : null })));
+ const saved = await Promise.all(movingNotes.map((n) => saveNote({ ...n, libraryLocation: { folderId, residency: target ?? n.libraryLocation?.residency ?? landing(null).residency } })));
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.some((note) => !!note.libraryLocation?.folderId) ?? false) ||
+ (notes.data?.notes.some((note) => !!note.libraryLocation) ?? false) ||
all.length > 0 ||
folders.length > 0 ||
templates.length > 0 ||
diff --git a/site/src/cut/components/NotesView.tsx b/site/src/cut/components/NotesView.tsx
index e26e109d..bb5c8f58 100644
--- a/site/src/cut/components/NotesView.tsx
+++ b/site/src/cut/components/NotesView.tsx
@@ -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.residency)
+ ? noteInLibraryFolder(n, library.folderId, library.folderId === null ? undefined : 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 no
@@ -206,7 +206,7 @@ export function NotesView({ library, ref }: Props = {}) {
colorIndex: 0,
// A note written inside a folder is filed there.
folderId: openFolder,
- ...(library ? { libraryLocation: library.folderId ? { folderId: library.folderId, residency: library.residency } satisfies NoteLocation : null } : {}),
+ ...(library ? { libraryLocation: { folderId: library.folderId, residency: library.residency } satisfies NoteLocation } : {}),
labelIds: [],
isNew: true,
saved: { title: "", body: "", colorIndex: 0, labelIds: [] },
diff --git a/site/src/cut/lib/noteReference.test.ts b/site/src/cut/lib/noteReference.test.ts
index 72f2b654..74c3cba3 100644
--- a/site/src/cut/lib/noteReference.test.ts
+++ b/site/src/cut/lib/noteReference.test.ts
@@ -9,10 +9,14 @@ 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("root notes stay in Notes and Library notes require a folder", () => {
+ test("Library root notes retain explicit placement; existing Notes stay in Notes", () => {
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 } };
+ expect(noteInLibraryFolder(root, null)).toBe(true);
+ expect(noteInLibraryFolder(root, null, residency)).toBe(true);
+ expect(noteInLibraryFolder(root, "assets", residency)).toBe(false);
const filed = { libraryLocation: { folderId: "assets", residency } };
expect(noteInLibraryFolder(filed, null)).toBe(false);
expect(noteInLibraryFolder(filed, "assets", residency)).toBe(true);
diff --git a/site/src/cut/lib/noteReference.ts b/site/src/cut/lib/noteReference.ts
index 1338269a..33b6fe15 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),
+ folderId: z.string().min(1).max(128).nullable(),
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 a folderId. libraryLocation:null keeps the note in the Notes tab. Library shows notes inside folders only.";
+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 function noteInLibraryFolder(note: Pick<CutNote, "libraryLocation">, folderId: string | null, residency?: NoteLocation["residency"]): boolean {
const location = note.libraryLocation;
- return folderId !== null && !!location && location.folderId === folderId && (!residency || location.residency === residency);
+ return !!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.ts
index f70cc675..fe80db1c 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 return the folder’s notes to Notes.");
+ if (!res.ok) throw new Error("Could not move the folder’s notes to the Library root.");
}
/** 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.ts
index 74f0dde1..2568ecaf 100644
--- a/site/src/cut/server/cloud/notes.test.ts
+++ b/site/src/cut/server/cloud/notes.test.ts
@@ -51,14 +51,22 @@ 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 placement requires a folder on every shelf", async () => {
+ test("new Library root notes persist on every shelf", async () => {
for (const residency of ["browser", "local", "cloud"] as const) {
const { api, db } = harness();
- 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);
+ db.cutNote.findFirst.mockResolvedValue(null);
+ const libraryLocation = { residency, folderId: null };
+ expect((await api.put("owner", "n1", request({ title: "Library note", updatedAt: 2, libraryLocation }))).status).toBe(200);
+ expect(db.cutNote.create.mock.calls[0][0]).toMatchObject({ data: { userId: "owner", libraryLocation } });
expect(db.cutFolder.findFirst.mock.calls.length).toBe(0);
}
});
+ test("editing a note can file it at the Library root", async () => {
+ const { api, db } = harness();
+ const libraryLocation = { residency: "cloud", 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 } });
+ });
test("old phone writes cannot replace newer text or placement", async () => {
const { api, db } = harness();
const res = await api.put("owner", "n1", request({ title: "Old", updatedAt: 0 }));
@@ -71,6 +79,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: Prisma.DbNull } });
+ ] }, data: { libraryLocation: { residency: "local", folderId: null } } });
});
});
diff --git a/site/src/cut/server/cloud/notes.ts b/site/src/cut/server/cloud/notes.ts
index 2fb7731a..fd34c75f 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: Prisma.DbNull },
+ data: { libraryLocation: { residency, folderId: null } },
});
}
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" && !await db.cutFolder.findFirst({ where: { id: location.folderId, userId, scope: "library" }, select: { id: true } })) {
+ if (location?.residency === "cloud" && location.folderId !== null && !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