From 0249f9dc707c1bbec9806978402b03b705d00f7f Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 7 Sep 2022 10:50:20 +0100 Subject: [PATCH 1/2] Fix possible soft crash from a race condition in space hierarchies --- src/components/structures/SpaceHierarchy.tsx | 22 +++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/components/structures/SpaceHierarchy.tsx b/src/components/structures/SpaceHierarchy.tsx index c22f1585f24..6d1bf20a9c1 100644 --- a/src/components/structures/SpaceHierarchy.tsx +++ b/src/components/structures/SpaceHierarchy.tsx @@ -499,12 +499,12 @@ const INITIAL_PAGE_SIZE = 20; export const useRoomHierarchy = (space: Room): { loading: boolean; rooms?: IHierarchyRoom[]; - hierarchy: RoomHierarchy; - error: Error; + hierarchy?: RoomHierarchy; + error?: Error; loadMore(pageSize?: number): Promise; } => { const [rooms, setRooms] = useState([]); - const [roomHierarchy, setHierarchy] = useState(); + const [hierarchy, setHierarchy] = useState(); const [error, setError] = useState(); const resetHierarchy = useCallback(() => { @@ -526,19 +526,21 @@ export const useRoomHierarchy = (space: Room): { })); const loadMore = useCallback(async (pageSize?: number) => { - if (roomHierarchy.loading || !roomHierarchy.canLoadMore || roomHierarchy.noSupport || error) return; - await roomHierarchy.load(pageSize).catch(setError); - setRooms(roomHierarchy.rooms); - }, [error, roomHierarchy]); + if (hierarchy.loading || !hierarchy.canLoadMore || hierarchy.noSupport || error) return; + await hierarchy.load(pageSize).catch(setError); + setRooms(hierarchy.rooms); + }, [error, hierarchy]); // Only return the hierarchy if it is for the space requested - let hierarchy = roomHierarchy; if (hierarchy?.root !== space) { - hierarchy = undefined; + return { + loading: true, + loadMore, + }; } return { - loading: hierarchy?.loading ?? true, + loading: hierarchy.loading, rooms, hierarchy, loadMore, From 26d518b08d007c95c175af2e64723f1390e1c72a Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 8 Sep 2022 09:02:15 +0100 Subject: [PATCH 2/2] Improve typing --- src/components/structures/SpaceHierarchy.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/structures/SpaceHierarchy.tsx b/src/components/structures/SpaceHierarchy.tsx index 6d1bf20a9c1..7336dfeb0cf 100644 --- a/src/components/structures/SpaceHierarchy.tsx +++ b/src/components/structures/SpaceHierarchy.tsx @@ -691,7 +691,7 @@ const SpaceHierarchy = ({ const { loading, rooms, hierarchy, loadMore, error: hierarchyError } = useRoomHierarchy(space); const filteredRoomSet = useMemo>(() => { - if (!rooms?.length) return new Set(); + if (!rooms?.length || !hierarchy) return new Set(); const lcQuery = query.toLowerCase().trim(); if (!lcQuery) return new Set(rooms); @@ -723,7 +723,7 @@ const SpaceHierarchy = ({ const loaderRef = useIntersectionObserver(loadMore); - if (!loading && hierarchy.noSupport) { + if (!loading && hierarchy!.noSupport) { return

{ _t("Your server does not support showing space hierarchies.") }

; } @@ -757,7 +757,7 @@ const SpaceHierarchy = ({ return { ({ onKeyDownHandler }) => { let content: JSX.Element; - if (loading && !rooms?.length) { + if (!hierarchy || (loading && !rooms?.length)) { content = ; } else { const hasPermissions = space?.getMyMembership() === "join" &&