Fix possible soft crash from a race condition in space hierarchies (#9254)

* Fix possible soft crash from a race condition in space hierarchies

* Improve typing
This commit is contained in:
Michael Telatynski 2022-09-08 09:17:55 +01:00 committed by GitHub
parent 851606c7ed
commit 9050ae4bb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -499,12 +499,12 @@ const INITIAL_PAGE_SIZE = 20;
export const useRoomHierarchy = (space: Room): { export const useRoomHierarchy = (space: Room): {
loading: boolean; loading: boolean;
rooms?: IHierarchyRoom[]; rooms?: IHierarchyRoom[];
hierarchy: RoomHierarchy; hierarchy?: RoomHierarchy;
error: Error; error?: Error;
loadMore(pageSize?: number): Promise<void>; loadMore(pageSize?: number): Promise<void>;
} => { } => {
const [rooms, setRooms] = useState<IHierarchyRoom[]>([]); const [rooms, setRooms] = useState<IHierarchyRoom[]>([]);
const [roomHierarchy, setHierarchy] = useState<RoomHierarchy>(); const [hierarchy, setHierarchy] = useState<RoomHierarchy>();
const [error, setError] = useState<Error | undefined>(); const [error, setError] = useState<Error | undefined>();
const resetHierarchy = useCallback(() => { const resetHierarchy = useCallback(() => {
@ -526,19 +526,21 @@ export const useRoomHierarchy = (space: Room): {
})); }));
const loadMore = useCallback(async (pageSize?: number) => { const loadMore = useCallback(async (pageSize?: number) => {
if (roomHierarchy.loading || !roomHierarchy.canLoadMore || roomHierarchy.noSupport || error) return; if (hierarchy.loading || !hierarchy.canLoadMore || hierarchy.noSupport || error) return;
await roomHierarchy.load(pageSize).catch(setError); await hierarchy.load(pageSize).catch(setError);
setRooms(roomHierarchy.rooms); setRooms(hierarchy.rooms);
}, [error, roomHierarchy]); }, [error, hierarchy]);
// Only return the hierarchy if it is for the space requested // Only return the hierarchy if it is for the space requested
let hierarchy = roomHierarchy;
if (hierarchy?.root !== space) { if (hierarchy?.root !== space) {
hierarchy = undefined; return {
loading: true,
loadMore,
};
} }
return { return {
loading: hierarchy?.loading ?? true, loading: hierarchy.loading,
rooms, rooms,
hierarchy, hierarchy,
loadMore, loadMore,
@ -689,7 +691,7 @@ const SpaceHierarchy = ({
const { loading, rooms, hierarchy, loadMore, error: hierarchyError } = useRoomHierarchy(space); const { loading, rooms, hierarchy, loadMore, error: hierarchyError } = useRoomHierarchy(space);
const filteredRoomSet = useMemo<Set<IHierarchyRoom>>(() => { const filteredRoomSet = useMemo<Set<IHierarchyRoom>>(() => {
if (!rooms?.length) return new Set(); if (!rooms?.length || !hierarchy) return new Set();
const lcQuery = query.toLowerCase().trim(); const lcQuery = query.toLowerCase().trim();
if (!lcQuery) return new Set(rooms); if (!lcQuery) return new Set(rooms);
@ -721,7 +723,7 @@ const SpaceHierarchy = ({
const loaderRef = useIntersectionObserver(loadMore); const loaderRef = useIntersectionObserver(loadMore);
if (!loading && hierarchy.noSupport) { if (!loading && hierarchy!.noSupport) {
return <p>{ _t("Your server does not support showing space hierarchies.") }</p>; return <p>{ _t("Your server does not support showing space hierarchies.") }</p>;
} }
@ -755,7 +757,7 @@ const SpaceHierarchy = ({
return <RovingTabIndexProvider onKeyDown={onKeyDown} handleHomeEnd handleUpDown> return <RovingTabIndexProvider onKeyDown={onKeyDown} handleHomeEnd handleUpDown>
{ ({ onKeyDownHandler }) => { { ({ onKeyDownHandler }) => {
let content: JSX.Element; let content: JSX.Element;
if (loading && !rooms?.length) { if (!hierarchy || (loading && !rooms?.length)) {
content = <Spinner />; content = <Spinner />;
} else { } else {
const hasPermissions = space?.getMyMembership() === "join" && const hasPermissions = space?.getMyMembership() === "join" &&