From 34e6c3037f201524f42fc01516ea78aec29b7fab Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Fri, 12 Dec 2025 02:11:24 +0100 Subject: [PATCH] feat: Add document update functionality to frontend --- frontend/src/documents/DocumentsManager.ts | 13 +++++++++++++ frontend/src/documents/data/useDocumentMutations.ts | 6 ++---- .../src/documents/data/useDocumentsWorkspace.ts | 5 +---- frontend/src/documents/types/workspaceTypes.ts | 1 + 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/frontend/src/documents/DocumentsManager.ts b/frontend/src/documents/DocumentsManager.ts index aaa5b8f..aaef178 100644 --- a/frontend/src/documents/DocumentsManager.ts +++ b/frontend/src/documents/DocumentsManager.ts @@ -162,6 +162,19 @@ class DocumentsManager { return request; } + update(id: DocumentId, updater: (doc: T) => Partial | T | undefined): boolean { + const doc = this.byId.get(id); + if (!doc) { + return false; + } + const changes = updater(doc); + if (!changes) { + return false; + } + const { changed } = this.ingest([{ ...doc, ...changes }]); + return changed; + } + map(mapper: (doc: T) => T | undefined): boolean { if (!this.byId.size) { return false; diff --git a/frontend/src/documents/data/useDocumentMutations.ts b/frontend/src/documents/data/useDocumentMutations.ts index 7ed1fe4..9d13607 100644 --- a/frontend/src/documents/data/useDocumentMutations.ts +++ b/frontend/src/documents/data/useDocumentMutations.ts @@ -195,8 +195,7 @@ const useDocumentMutations = ({ if (updatedDocument && documentsState.ingestDocuments) { documentsState.ingestDocuments([updatedDocument]); } else { - documentsState.documentsManager.map((doc) => { - if (doc.id !== documentId) return undefined; + documentsState.documentsManager.update(documentId, (doc) => { if (updatedDocument) { return { ...doc, ...updatedDocument }; } @@ -229,8 +228,7 @@ const useDocumentMutations = ({ if (updatedDocument && documentsState.ingestDocuments) { documentsState.ingestDocuments([updatedDocument]); } else { - documentsState.documentsManager.map((doc) => { - if (doc.id !== documentId) return undefined; + documentsState.documentsManager.update(documentId, (doc) => { if (updatedDocument) { return { ...doc, ...updatedDocument }; } diff --git a/frontend/src/documents/data/useDocumentsWorkspace.ts b/frontend/src/documents/data/useDocumentsWorkspace.ts index a14c104..93d4f7d 100644 --- a/frontend/src/documents/data/useDocumentsWorkspace.ts +++ b/frontend/src/documents/data/useDocumentsWorkspace.ts @@ -780,10 +780,7 @@ const useDocumentsWorkspace = ({ return null; } - documentsManager.map((doc) => { - if (doc.id !== documentId) return undefined; - return mergeAssetIntoDocument(doc, entry); - }); + documentsManager.update(documentId, (doc) => mergeAssetIntoDocument(doc, entry)); return entry; } catch (error) { diff --git a/frontend/src/documents/types/workspaceTypes.ts b/frontend/src/documents/types/workspaceTypes.ts index a78ca64..b4d9cfc 100644 --- a/frontend/src/documents/types/workspaceTypes.ts +++ b/frontend/src/documents/types/workspaceTypes.ts @@ -7,6 +7,7 @@ import type CorrespondentManager from '../../lib/assets/CorrespondentManager'; interface DocumentsManagerInterface { map(mapper: (doc: Document) => Document | undefined): boolean; + update(id: DocumentId, updater: (doc: Document) => Partial | Document | undefined): boolean; ingest(rawDocs: unknown[]): { canonical: Document[]; changed: boolean }; remove(ids: Array): boolean; }