Skip to content

Commit fbc22e6

Browse files
authored
Fix TreeView unwanted expansion (#577)
Do not dispatch currentPath each time mapData change otherwise it proc a useEffect in DirectoryTreeView which expand Signed-off-by: sBouzols <[email protected]>
1 parent 9cb2125 commit fbc22e6

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/components/tree-views-container.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ export function updatedTree(
194194
return [nextRoots, nextMap];
195195
}
196196

197+
function pathHasChanged(currentPath: ElementAttributes[], newPath: ElementAttributes[]) {
198+
return (
199+
currentPath.length !== newPath.length ||
200+
!currentPath.every((elem, index) => {
201+
return elem.elementUuid === newPath[index].elementUuid && elem.elementName === newPath[index].elementName;
202+
})
203+
);
204+
}
205+
197206
export default function TreeViewsContainer() {
198207
const dispatch = useDispatch();
199208

@@ -202,6 +211,9 @@ export default function TreeViewsContainer() {
202211
const user = useSelector((state: AppState) => state.user);
203212
const selectedDirectory = useSelector((state: AppState) => state.selectedDirectory);
204213
const activeDirectory = useSelector((state: AppState) => state.activeDirectory);
214+
const currentPath = useSelector((state: AppState) => state.currentPath);
215+
const currentPathRef = useRef<ElementAttributes[]>([]);
216+
currentPathRef.current = currentPath;
205217

206218
const uploadingElements = useSelector((state: AppState) => state.uploadingElements);
207219
const uploadingElementsRef = useRef<Record<string, UploadingElement>>({});
@@ -322,7 +334,12 @@ export default function TreeViewsContainer() {
322334

323335
/* Manage current path data */
324336
useEffect(() => {
325-
dispatch(setCurrentPath(buildPathToFromMap(selectedDirectoryRef.current?.elementUuid, treeData.mapData)));
337+
// Do not change currentPath everytime mapData changed
338+
// if it's the same path (same uuids same names in order) then do not dispatch
339+
const newPath = buildPathToFromMap(selectedDirectoryRef.current?.elementUuid, treeData.mapData);
340+
if (pathHasChanged(currentPathRef.current, newPath)) {
341+
dispatch(setCurrentPath(newPath));
342+
}
326343
}, [dispatch, treeData.mapData, selectedDirectory?.elementUuid]);
327344

328345
const insertContent = useCallback(

0 commit comments

Comments
 (0)