diff --git a/src/features/network-modification-table/network-modifications-table.tsx b/src/features/network-modification-table/network-modifications-table.tsx index 593285e2f..53617c222 100644 --- a/src/features/network-modification-table/network-modifications-table.tsx +++ b/src/features/network-modification-table/network-modifications-table.tsx @@ -37,6 +37,7 @@ import { useModificationsSelection } from './use-modifications-selection'; import { fetchSubModificationsForExpandedRows, findAllLoadedCompositeModifications, + findAllLoadedReferenceModifications, findDepth, formatToComposedModification, isCompositeModification, @@ -155,19 +156,19 @@ export function NetworkModificationsTable({ ); setComposedModifications(nextMods); - // Re-fetch authoritative children for every composite that already had loaded children, - // correcting anything stale that was temporarily preserved above. + // Re-fetch authoritative children for every composite AND reference that already had + // loaded children, correcting anything stale that was temporarily preserved above. + // References matter here because a shared-element update changes the referenced + // composite's content while the reference row stays expanded. // Source of truth: prevMods — nextMods children may have been filtered just above. // The rowKeys collected here are still valid in nextMods since the merge above preserved them. const loadedComposites: ComposedModificationMetadata[] = []; findAllLoadedCompositeModifications(prevMods, loadedComposites); - if (loadedComposites.length > 0) { - fetchSubModificationsForExpandedRows( - loadedComposites.map((m) => m.rowKey), - nextMods, - setComposedModifications, - true - ); + const loadedReferences: ComposedModificationMetadata[] = []; + findAllLoadedReferenceModifications(prevMods, loadedReferences); + const expandedRowKeysToRefresh = [...loadedComposites, ...loadedReferences].map((m) => m.rowKey); + if (expandedRowKeysToRefresh.length > 0) { + fetchSubModificationsForExpandedRows(expandedRowKeysToRefresh, nextMods, setComposedModifications, true); } }, [modifications]); diff --git a/src/features/network-modification-table/utils.ts b/src/features/network-modification-table/utils.ts index dce402bf6..2aa2751a6 100644 --- a/src/features/network-modification-table/utils.ts +++ b/src/features/network-modification-table/utils.ts @@ -116,6 +116,28 @@ export function findAllLoadedCompositeModifications( }); } +/** + * Collects every reference modification whose children are already loaded, at any depth + * (a reference can be nested inside an expanded composite). Unlike + * {@link findAllLoadedCompositeModifications}, recursion does not stop on non-matching + * nodes since a reference's ancestors are usually composites. + * @param modifications source where the reference modifications are looked for + * @param references result : all the loaded reference modifications found + */ +export function findAllLoadedReferenceModifications( + modifications: ComposedModificationMetadata[], + references: ComposedModificationMetadata[] +) { + modifications.forEach((modification) => { + if (modification.subModifications.length > 0) { + if (isReferenceModification(modification)) { + references.push(modification); + } + findAllLoadedReferenceModifications(modification.subModifications, references); + } + }); +} + export function findModificationInTree( rowKey: UUID, mods: ComposedModificationMetadata[]