From 55f7adf4e84ebfb161d658605194ad7159e6cbf8 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Tue, 21 Jul 2026 16:19:43 -0500 Subject: [PATCH] perf: fix unresponsive collapse in lobby --- src/app/features/lobby/Lobby.tsx | 64 +++------ src/app/features/lobby/SpaceHierarchyItem.tsx | 135 +++++++++--------- src/app/hooks/useRenderableMediaUrl.test.tsx | 5 +- src/app/pages/client/direct/Direct.tsx | 7 +- src/app/pages/client/home/Home.tsx | 7 +- src/app/pages/client/space/Space.tsx | 61 +------- 6 files changed, 98 insertions(+), 181 deletions(-) diff --git a/src/app/features/lobby/Lobby.tsx b/src/app/features/lobby/Lobby.tsx index e48316814..b3664e856 100644 --- a/src/app/features/lobby/Lobby.tsx +++ b/src/app/features/lobby/Lobby.tsx @@ -1,5 +1,5 @@ import type { MouseEventHandler, ReactElement } from 'react'; -import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { useCallback, useMemo, useRef, useState } from 'react'; import { Box, Chip, IconButton, Line, Scroll, Spinner, Text, color, config } from 'folds'; import type { VirtualItem } from '@tanstack/react-virtual'; import { useVirtualizer } from '@tanstack/react-virtual'; @@ -32,7 +32,7 @@ import { useRoomsPowerLevels, } from '$hooks/usePowerLevels'; import { mDirectAtom } from '$state/mDirectList'; -import { makeLobbyCategoryId, getLobbyCategoryIdParts } from '$state/closedLobbyCategories'; +import { makeLobbyCategoryId } from '$state/closedLobbyCategories'; import { useCategoryHandler } from '$hooks/useCategoryHandler'; import { useMatrixClient } from '$hooks/useMatrixClient'; import { allRoomsAtom } from '$state/room-list/roomList'; @@ -203,9 +203,7 @@ export function Lobby() { const getRoom = useGetRoom(allJoinedRooms); const closedCategoriesCache = useRef(new Map()); - useEffect(() => { - closedCategoriesCache.current.clear(); - }, [closedCategories, roomToParents, getRoom]); + closedCategoriesCache.current.clear(); /** * Recursively checks if a given parentId (or all its ancestors) is in a closed category. @@ -264,22 +262,22 @@ export function Lobby() { [closedCategories, getRoom, roomToParents, spaceRooms] ); - /** - * Determines whether all parent categories are collapsed. - * - * @param spaceId - The root space ID. - * @param roomId - The room ID to start the check from. - * @returns True if every parent category is collapsed; false otherwise. - */ - const getAllAncestorsCollapsed = (spaceId: string, roomId: string): boolean => { - const parentIds = roomToParents.get(roomId); + const handleSpacesFound = useCallback( + (sItems: IHierarchyRoom[]) => { + setSpaceRooms({ type: 'PUT', roomIds: sItems.map((i) => i.room_id) }); + setSpacesItems((current) => { + const newItems = produce(current, (draft) => { + sItems.forEach((item) => draft.set(item.room_id, item)); + }); + return current.size === newItems.size ? current : newItems; + }); + }, + [setSpaceRooms] + ); - if (!parentIds || parentIds.size === 0) { - return false; - } - - return !Array.from(parentIds).some((id) => !getInClosedCategories(spaceId, id, roomId)); - }; + const handleCategoryClick = useCategoryHandler(setClosedCategories, (categoryId) => + closedCategories.has(categoryId) + ); const [subspaceHierarchyLimit] = useSetting(settingsAtom, 'subspaceHierarchyLimit'); const [draggingItem, setDraggingItem] = useState(); @@ -507,32 +505,6 @@ export function Lobby() { ) ); - const handleSpacesFound = useCallback( - (sItems: IHierarchyRoom[]) => { - setSpaceRooms({ type: 'PUT', roomIds: sItems.map((i) => i.room_id) }); - setSpacesItems((current) => { - const newItems = produce(current, (draft) => { - sItems.forEach((item) => draft.set(item.room_id, item)); - }); - return current.size === newItems.size ? current : newItems; - }); - }, - [setSpaceRooms] - ); - - const handleCategoryClick = useCategoryHandler(setClosedCategories, (categoryId) => { - const collapsed = closedCategories.has(categoryId); - const [spaceId, roomId] = getLobbyCategoryIdParts(categoryId); - - // Prevent collapsing if all parents are collapsed - const toggleable = !getAllAncestorsCollapsed(spaceId ?? '', roomId ?? ''); - - if (toggleable) { - return collapsed; - } - return !collapsed; - }); - const handleOpenRoom: MouseEventHandler = (evt) => { const rId = evt.currentTarget.getAttribute('data-room-id'); if (!rId) return; diff --git a/src/app/features/lobby/SpaceHierarchyItem.tsx b/src/app/features/lobby/SpaceHierarchyItem.tsx index 8297dadee..d1ba269da 100644 --- a/src/app/features/lobby/SpaceHierarchyItem.tsx +++ b/src/app/features/lobby/SpaceHierarchyItem.tsx @@ -156,77 +156,78 @@ export const SpaceHierarchyItem = forwardRef - {childItems && childItems.length > 0 ? ( - - {childItems.map((roomItem, index) => { - const roomSummary = rooms.get(roomItem.roomId); + {!closed && + (childItems && childItems.length > 0 ? ( + + {childItems.map((roomItem, index) => { + const roomSummary = rooms.get(roomItem.roomId); - const roomPowerLevels = roomsPowerLevels.get(roomItem.roomId) ?? {}; + const roomPowerLevels = roomsPowerLevels.get(roomItem.roomId) ?? {}; - const lastItem = index === childItems.length; - const nextRoomId = lastItem ? nextSpaceId : childItems[index + 1]?.roomId; + const lastItem = index === childItems.length; + const nextRoomId = lastItem ? nextSpaceId : childItems[index + 1]?.roomId; - const roomDragging = - draggingItem?.roomId === roomItem.roomId && - draggingItem.parentId === roomItem.parentId; + const roomDragging = + draggingItem?.roomId === roomItem.roomId && + draggingItem.parentId === roomItem.parentId; - return ( - - } - after={ - - } - data-dragging={roomDragging} - onDragging={onDragging} - /> - ); - })} - - ) : ( - childItems && ( - - - - This space does not contain any rooms. - - - - ) - )} + return ( + + } + after={ + + } + data-dragging={roomDragging} + onDragging={onDragging} + /> + ); + })} + + ) : ( + childItems && ( + + + + This space does not contain any rooms. + + + + ) + ))} ); } diff --git a/src/app/hooks/useRenderableMediaUrl.test.tsx b/src/app/hooks/useRenderableMediaUrl.test.tsx index 4a036a018..58342ab7a 100644 --- a/src/app/hooks/useRenderableMediaUrl.test.tsx +++ b/src/app/hooks/useRenderableMediaUrl.test.tsx @@ -189,9 +189,8 @@ describe('useRenderableMediaUrl', () => { platform.hasServiceWorker.mockReturnValue(false); platform.hasControllingServiceWorker.mockReturnValue(false); mediaTransport.fetchMediaBlob.mockResolvedValue(new Blob(['media'], { type: 'image/png' })); - const { useRenderableMediaUrl, clearRenderableMediaUrlCache } = await import( - './useRenderableMediaUrl' - ); + const { useRenderableMediaUrl, clearRenderableMediaUrlCache } = + await import('./useRenderableMediaUrl'); const first = renderHook(() => useRenderableMediaUrl('https://example.org/media.png')); const second = renderHook(() => useRenderableMediaUrl('https://example.org/media.png')); diff --git a/src/app/pages/client/direct/Direct.tsx b/src/app/pages/client/direct/Direct.tsx index aa8ab39cc..7cdefd6ef 100644 --- a/src/app/pages/client/direct/Direct.tsx +++ b/src/app/pages/client/direct/Direct.tsx @@ -1,5 +1,5 @@ import type { MouseEventHandler } from 'react'; -import { forwardRef, useEffect, useMemo, useRef, useState } from 'react'; +import { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useAtom, useAtomValue, useSetAtom } from 'jotai'; import type { RectCords } from 'folds'; import { @@ -257,10 +257,7 @@ export function Direct() { return items; }, [mx, directs, closedCategories, roomToUnread, selectedRoomId, activityCounter]); - const getItemKey = useCallback( - (index: number) => sortedDirects[index] ?? index, - [sortedDirects] - ); + const getItemKey = useCallback((index: number) => sortedDirects[index] ?? index, [sortedDirects]); const virtualizer = useVirtualizer({ count: sortedDirects.length, diff --git a/src/app/pages/client/home/Home.tsx b/src/app/pages/client/home/Home.tsx index a80ddac0f..d40b0f576 100644 --- a/src/app/pages/client/home/Home.tsx +++ b/src/app/pages/client/home/Home.tsx @@ -1,5 +1,5 @@ import type { MouseEventHandler } from 'react'; -import { forwardRef, useEffect, useMemo, useRef, useState } from 'react'; +import { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import type { RectCords } from 'folds'; import { @@ -287,10 +287,7 @@ export function Home() { return items; }, [mx, rooms, closedCategories, roomToUnread, selectedRoomId, isShowingAllRoomsInHome]); - const getItemKey = useCallback( - (index: number) => sortedRooms[index] ?? index, - [sortedRooms] - ); + const getItemKey = useCallback((index: number) => sortedRooms[index] ?? index, [sortedRooms]); const virtualizer = useVirtualizer({ count: sortedRooms.length, diff --git a/src/app/pages/client/space/Space.tsx b/src/app/pages/client/space/Space.tsx index 58613786f..0f64df116 100644 --- a/src/app/pages/client/space/Space.tsx +++ b/src/app/pages/client/space/Space.tsx @@ -38,7 +38,7 @@ import { VirtualTile } from '$components/virtualizer'; import { spaceRoomsAtom } from '$state/spaceRooms'; import { RoomNavCategoryButton, RoomNavItem } from '$features/room-nav'; import { SpaceNavItem } from '$features/space-nav'; -import { makeNavCategoryId, getNavCategoryIdParts } from '$state/closedNavCategories'; +import { makeNavCategoryId } from '$state/closedNavCategories'; import { roomToUnreadAtom } from '$state/room/roomToUnread'; import { useCategoryHandler } from '$hooks/useCategoryHandler'; import { useNavToActivePathMapper } from '$hooks/useNavToActivePathMapper'; @@ -580,17 +580,7 @@ export function Space() { ); const closedCategoriesCache = useRef(new Map()); - const ancestorsCollapsedCache = useRef(new Map()); - const prevSpaceIdRef = useRef(space.roomId); - if (prevSpaceIdRef.current !== space.roomId) { - prevSpaceIdRef.current = space.roomId; - closedCategoriesCache.current.clear(); - ancestorsCollapsedCache.current.clear(); - } - useEffect(() => { - closedCategoriesCache.current.clear(); - ancestorsCollapsedCache.current.clear(); - }, [closedCategories, roomToParents, getRoom, space.roomId]); + closedCategoriesCache.current.clear(); /** * Recursively checks if a given parentId (or all its ancestors) is in a closed category. @@ -676,32 +666,6 @@ export function Space() { [roomToUnread, selectedRoomId, roomToChildren] ); - /** - * Determines whether all parent categories are collapsed. - * - * @param spaceId - The root space ID. - * @param roomId - The room ID to start the check from. - * @returns True if every parent category is collapsed; false otherwise. - */ - const getAllAncestorsCollapsed = (spaceId: string, roomId: string): boolean => { - const categoryId = makeNavCategoryId(spaceId, roomId); - if (ancestorsCollapsedCache.current.has(categoryId)) { - return ancestorsCollapsedCache.current.get(categoryId); - } - - const parentIds = roomToParents.get(roomId); - if (!parentIds || parentIds.size === 0) { - ancestorsCollapsedCache.current.set(categoryId, false); - return false; - } - - const allCollapsed = !Array.from(parentIds).some( - (id) => !getInClosedCategories(spaceId, id, roomId) - ); - ancestorsCollapsedCache.current.set(categoryId, allCollapsed); - return allCollapsed; - }; - /** * Determines the depth limit for the joined space hierarchy and the SpaceNavItems to start appearing */ @@ -866,18 +830,9 @@ export function Space() { const virtualizedItems = virtualizer.getVirtualItems(); - const handleCategoryClick = useCategoryHandler(setClosedCategories, (categoryId) => { - const collapsed = closedCategories.has(categoryId); - const [spaceId, roomId] = getNavCategoryIdParts(categoryId); - - // Only prevent collapsing if all parents are collapsed - const toggleable = !getAllAncestorsCollapsed(spaceId, roomId); - - if (toggleable) { - return collapsed; - } - return !collapsed; - }); + const handleCategoryClick = useCategoryHandler(setClosedCategories, (categoryId) => + closedCategories.has(categoryId) + ); const getToLink = (roomId: string) => getSpaceRoomPath(spaceIdOrAlias, getCanonicalAliasOrRoomId(mx, roomId)); @@ -1024,11 +979,7 @@ export function Space() { } return ( - +