fix: Prevent duplicate personal document in sidebar on first create (#14107)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-16 11:04:57 -04:00
1 parent 0514c5ba4f
commit 6df3fa18d3
3 files changed
+25 -4

No files matched your search

+19
View File
@@ -20,4 +20,23 @@ describe("Collection model", () => {
const shared = stores.collections.add({ id: "789", name: "Design" });
expect(shared.displayName).toBe("Design");
});
it("should not add a document that is already in the collection", () => {
const collection = stores.collections.add({
id: "abc",
name: "Personal",
sort: { field: "title", direction: "asc" },
documents: [],
});
const document = stores.documents.add({
id: "doc-1",
title: "Doc",
url: "/doc/doc-1",
collectionId: collection.id,
});
collection.addDocument(document);
collection.addDocument(document);
expect(collection.documents?.map((node) => node.id)).toEqual(["doc-1"]);
});
});
+3 -2
View File
@@ -344,14 +344,15 @@ export default class Collection extends ParanoidModel {
/**
* Adds the document identified by the given id to the collection in
* memory. Does not add the document to the database or store.
* memory, if it is not already present. Does not add the document to the
* database or store.
*
* @param document The document to add.
* @param parentDocumentId The id of the document to add the new document to.
*/
@action
addDocument(document: Document, parentDocumentId?: string) {
if (!this.documents) {
if (!this.documents || this.childrenByDocumentId.has(document.id)) {
return;
}
+3 -2
View File
@@ -128,14 +128,15 @@ export default abstract class NavigableModel extends Model {
/**
* Adds the document identified by the given id to the model in
* memory. Does not add the document to the database or store.
* memory, if it is not already present. Does not add the document to the
* database or store.
*
* @param document The document to add.
* @param parentDocumentId The id of the document to add the new document to.
*/
@action
addDocument(document: Document, parentDocumentId: string) {
if (!this.documents || !document || !parentDocumentId?.trim()) {
if (!this.documents || this.childrenByDocumentId.has(document.id)) {
return;
}