From e9d88fdce8b6061dd8910ebcff4ee6df212d90ab Mon Sep 17 00:00:00 2001 From: Drew Haven Date: Sat, 11 Oct 2025 15:12:22 -0700 Subject: [PATCH] Fixes bug in loader when a relationship is empty --- src/context/document/DocumentLoader.tsx | 5 +++-- src/context/document/reducer.ts | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/context/document/DocumentLoader.tsx b/src/context/document/DocumentLoader.tsx index 046f817..8b61a1e 100644 --- a/src/context/document/DocumentLoader.tsx +++ b/src/context/document/DocumentLoader.tsx @@ -36,8 +36,9 @@ export function DocumentLoader({ relationships: doc.expand?.relationships_via_primary || [], relatedDocuments: doc.expand?.relationships_via_primary?.flatMap( - (r: RecordModel) => r.expand?.secondary, - ) || [], + // Note: If there are no entries in the expanded secondaries there just won't be an entry instead of an empty list. + (r: RecordModel) => r.expand?.secondary ?? [], + ) ?? [], }); } diff --git a/src/context/document/reducer.ts b/src/context/document/reducer.ts index 968989d..f09bd97 100644 --- a/src/context/document/reducer.ts +++ b/src/context/document/reducer.ts @@ -150,13 +150,20 @@ export function reducer( action.doc.id, setDocument(state, action.doc), ); + const updatedRelationshipsState = action.relationships.reduce( setRelationship.bind(null, action.doc.id), updatedDocumentState, ); + + const emptyRemainingRelationships = setAllRelationshipsEmpty( + action.doc.id, + updatedRelationshipsState, + ); + return action.relatedDocuments.reduce( setDocument, - updatedRelationshipsState, + emptyRemainingRelationships, ); case "removeDocument": return removeDocument(action.docId, state);