Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 5481546

Browse files
authored
Merge pull request #6910 from matrix-org/t3chguy/cp/space-hierarchy
2 parents 425eead + a0c0eea commit 5481546

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

src/components/structures/SpaceHierarchy.tsx

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,50 +410,39 @@ export const HierarchyLevel = ({
410410

411411
const INITIAL_PAGE_SIZE = 20;
412412

413-
export const useSpaceSummary = (space: Room): {
413+
export const useRoomHierarchy = (space: Room): {
414414
loading: boolean;
415415
rooms: IHierarchyRoom[];
416416
hierarchy: RoomHierarchy;
417417
loadMore(pageSize?: number): Promise <void>;
418418
} => {
419419
const [rooms, setRooms] = useState<IHierarchyRoom[]>([]);
420-
const [loading, setLoading] = useState(true);
421420
const [hierarchy, setHierarchy] = useState<RoomHierarchy>();
422421

423422
const resetHierarchy = useCallback(() => {
424423
const hierarchy = new RoomHierarchy(space, INITIAL_PAGE_SIZE);
425-
setHierarchy(hierarchy);
426-
427-
let discard = false;
428424
hierarchy.load().then(() => {
429-
if (discard) return;
425+
if (space !== hierarchy.root) return; // discard stale results
430426
setRooms(hierarchy.rooms);
431-
setLoading(false);
432427
});
433-
434-
return () => {
435-
discard = true;
436-
};
428+
setHierarchy(hierarchy);
437429
}, [space]);
438430
useEffect(resetHierarchy, [resetHierarchy]);
439431

440432
useDispatcher(defaultDispatcher, (payload => {
441433
if (payload.action === Action.UpdateSpaceHierarchy) {
442-
setLoading(true);
443434
setRooms([]); // TODO
444435
resetHierarchy();
445436
}
446437
}));
447438

448439
const loadMore = useCallback(async (pageSize?: number) => {
449-
if (loading || !hierarchy.canLoadMore || hierarchy.noSupport) return;
450-
451-
setLoading(true);
440+
if (hierarchy.loading || !hierarchy.canLoadMore || hierarchy.noSupport) return;
452441
await hierarchy.load(pageSize);
453442
setRooms(hierarchy.rooms);
454-
setLoading(false);
455-
}, [loading, hierarchy]);
443+
}, [hierarchy]);
456444

445+
const loading = hierarchy?.loading ?? true;
457446
return { loading, rooms, hierarchy, loadMore };
458447
};
459448

@@ -587,7 +576,7 @@ const SpaceHierarchy = ({
587576

588577
const [selected, setSelected] = useState(new Map<string, Set<string>>()); // Map<parentId, Set<childId>>
589578

590-
const { loading, rooms, hierarchy, loadMore } = useSpaceSummary(space);
579+
const { loading, rooms, hierarchy, loadMore } = useRoomHierarchy(space);
591580

592581
const filteredRoomSet = useMemo<Set<IHierarchyRoom>>(() => {
593582
if (!rooms?.length) return new Set();

0 commit comments

Comments
 (0)