feat: Add document update functionality to frontend

This commit is contained in:
2025-12-12 02:11:24 +01:00
parent b10404f0f3
commit 34e6c3037f
4 changed files with 17 additions and 8 deletions
@@ -162,6 +162,19 @@ class DocumentsManager<T extends ManagedDocument = ManagedDocument> {
return request;
}
update(id: DocumentId, updater: (doc: T) => Partial<T> | 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;
@@ -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 };
}
@@ -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) {
@@ -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> | Document | undefined): boolean;
ingest(rawDocs: unknown[]): { canonical: Document[]; changed: boolean };
remove(ids: Array<DocumentId>): boolean;
}