From efbe71b28fd8b35a62ca625e1365523989449cc5 Mon Sep 17 00:00:00 2001 From: Yaroslav Kuznietsov Date: Wed, 19 Jun 2024 14:03:49 +0300 Subject: [PATCH] [useLazyTree]: fixed updating itemsMap after setItems. --- .../hooks/strategies/lazyTree/useLazyTree.ts | 21 ++++++++++++------- .../views/tree/treeState/TreeState.ts | 4 ++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/uui-core/src/data/processing/views/tree/hooks/strategies/lazyTree/useLazyTree.ts b/uui-core/src/data/processing/views/tree/hooks/strategies/lazyTree/useLazyTree.ts index f407296332..cfeb8a68ba 100644 --- a/uui-core/src/data/processing/views/tree/hooks/strategies/lazyTree/useLazyTree.ts +++ b/uui-core/src/data/processing/views/tree/hooks/strategies/lazyTree/useLazyTree.ts @@ -96,10 +96,15 @@ export function useLazyTree( showSelectedOnly, }, [...deps]); + const treeWithDataActual = useActualItemsMap({ + tree: treeWithData, + itemsMap, + }); + useEffect(() => { if (showSelectedOnly && isSelectedOrCheckedChanged(dataSourceState, prevDataSourceState)) { loadMissing({ - tree: treeWithData, + tree: treeWithDataActual, using: 'full', abortInProgress: shouldRefetch, dataSourceState: { @@ -108,7 +113,7 @@ export function useLazyTree( }, }) .then(({ isUpdated, isOutdated, tree: newTree }) => { - if (!isOutdated && (isUpdated || newTree !== treeWithData)) { + if (!isOutdated && (isUpdated || newTree !== treeWithDataActual)) { setTreeWithData(newTree); } }); @@ -120,14 +125,14 @@ export function useLazyTree( return; } - let currentTree = treeWithData; + let currentTree = treeWithDataActual; if (shouldRefetch) { setIsFetching(true); - currentTree = treeWithData.clearStructure(); + currentTree = treeWithDataActual.clearStructure(); } if (shouldLoad) { - if (currentTree !== treeWithData) { + if (currentTree !== treeWithDataActual) { setTreeWithData(currentTree); } setIsLoading(true); @@ -140,7 +145,7 @@ export function useLazyTree( abortInProgress: shouldRefetch, }) .then(({ isUpdated, isOutdated, tree: newTree }) => { - if (!isOutdated && (isUpdated || newTree !== treeWithData)) { + if (!isOutdated && (isUpdated || newTree !== treeWithDataActual)) { setTreeWithData(newTree); } }).finally(() => { @@ -159,10 +164,10 @@ export function useLazyTree( ]); const treeWithSelectedOnly = useSelectedOnlyTree({ - tree: treeWithData, + tree: treeWithDataActual, dataSourceState, isLoading: isLoading || isFetching, - }, [treeWithData]); + }, [treeWithDataActual]); const treeWithNewItemsMap = useActualItemsMap({ tree: treeWithSelectedOnly, diff --git a/uui-core/src/data/processing/views/tree/treeState/TreeState.ts b/uui-core/src/data/processing/views/tree/treeState/TreeState.ts index f0113407ad..54227736ed 100644 --- a/uui-core/src/data/processing/views/tree/treeState/TreeState.ts +++ b/uui-core/src/data/processing/views/tree/treeState/TreeState.ts @@ -280,6 +280,10 @@ export class TreeState { } public updateItemsMap(itemsMap: ItemsMap) { + if (itemsMap === this.itemsMap) { + return this; + } + const itemsAccessor = ItemsAccessor.toItemsAccessor(itemsMap); return new TreeState( TreeStructure.withNewItemsAccessor(itemsAccessor, this.full),