Skip to content

Commit

Permalink
[useLazyTree]: fixed updating itemsMap after setItems.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov authored and AlekseyManetov committed Jun 21, 2024
1 parent 8ea0e83 commit efbe71b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ export function useLazyTree<TItem, TId, TFilter = any>(
showSelectedOnly,
}, [...deps]);

const treeWithDataActual = useActualItemsMap({
tree: treeWithData,
itemsMap,
});

useEffect(() => {
if (showSelectedOnly && isSelectedOrCheckedChanged(dataSourceState, prevDataSourceState)) {
loadMissing({
tree: treeWithData,
tree: treeWithDataActual,
using: 'full',
abortInProgress: shouldRefetch,
dataSourceState: {
Expand All @@ -108,7 +113,7 @@ export function useLazyTree<TItem, TId, TFilter = any>(
},
})
.then(({ isUpdated, isOutdated, tree: newTree }) => {
if (!isOutdated && (isUpdated || newTree !== treeWithData)) {
if (!isOutdated && (isUpdated || newTree !== treeWithDataActual)) {
setTreeWithData(newTree);
}
});
Expand All @@ -120,14 +125,14 @@ export function useLazyTree<TItem, TId, TFilter = any>(
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);
Expand All @@ -140,7 +145,7 @@ export function useLazyTree<TItem, TId, TFilter = any>(
abortInProgress: shouldRefetch,
})
.then(({ isUpdated, isOutdated, tree: newTree }) => {
if (!isOutdated && (isUpdated || newTree !== treeWithData)) {
if (!isOutdated && (isUpdated || newTree !== treeWithDataActual)) {
setTreeWithData(newTree);
}
}).finally(() => {
Expand All @@ -159,10 +164,10 @@ export function useLazyTree<TItem, TId, TFilter = any>(
]);

const treeWithSelectedOnly = useSelectedOnlyTree({
tree: treeWithData,
tree: treeWithDataActual,
dataSourceState,
isLoading: isLoading || isFetching,
}, [treeWithData]);
}, [treeWithDataActual]);

const treeWithNewItemsMap = useActualItemsMap({
tree: treeWithSelectedOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ export class TreeState<TItem, TId> {
}

public updateItemsMap(itemsMap: ItemsMap<TId, TItem>) {
if (itemsMap === this.itemsMap) {
return this;
}

const itemsAccessor = ItemsAccessor.toItemsAccessor(itemsMap);
return new TreeState(
TreeStructure.withNewItemsAccessor(itemsAccessor, this.full),
Expand Down

0 comments on commit efbe71b

Please sign in to comment.