Simplify and improve useRoomHierarchy hook

This commit is contained in:
Michael Telatynski 2021-10-06 10:27:16 +01:00
parent b86d646fa7
commit 79f3348986

View file

@ -410,50 +410,39 @@ export const HierarchyLevel = ({
const INITIAL_PAGE_SIZE = 20; const INITIAL_PAGE_SIZE = 20;
export const useSpaceSummary = (space: Room): { export const useRoomHierarchy = (space: Room): {
loading: boolean; loading: boolean;
rooms: IHierarchyRoom[]; rooms: IHierarchyRoom[];
hierarchy: RoomHierarchy; hierarchy: RoomHierarchy;
loadMore(pageSize?: number): Promise <void>; loadMore(pageSize?: number): Promise <void>;
} => { } => {
const [rooms, setRooms] = useState<IHierarchyRoom[]>([]); const [rooms, setRooms] = useState<IHierarchyRoom[]>([]);
const [loading, setLoading] = useState(true);
const [hierarchy, setHierarchy] = useState<RoomHierarchy>(); const [hierarchy, setHierarchy] = useState<RoomHierarchy>();
const resetHierarchy = useCallback(() => { const resetHierarchy = useCallback(() => {
const hierarchy = new RoomHierarchy(space, INITIAL_PAGE_SIZE); const hierarchy = new RoomHierarchy(space, INITIAL_PAGE_SIZE);
setHierarchy(hierarchy);
let discard = false;
hierarchy.load().then(() => { hierarchy.load().then(() => {
if (discard) return; if (space !== hierarchy.root) return; // discard stale results
setRooms(hierarchy.rooms); setRooms(hierarchy.rooms);
setLoading(false);
}); });
setHierarchy(hierarchy);
return () => {
discard = true;
};
}, [space]); }, [space]);
useEffect(resetHierarchy, [resetHierarchy]); useEffect(resetHierarchy, [resetHierarchy]);
useDispatcher(defaultDispatcher, (payload => { useDispatcher(defaultDispatcher, (payload => {
if (payload.action === Action.UpdateSpaceHierarchy) { if (payload.action === Action.UpdateSpaceHierarchy) {
setLoading(true);
setRooms([]); // TODO setRooms([]); // TODO
resetHierarchy(); resetHierarchy();
} }
})); }));
const loadMore = useCallback(async (pageSize?: number) => { const loadMore = useCallback(async (pageSize?: number) => {
if (loading || !hierarchy.canLoadMore || hierarchy.noSupport) return; if (hierarchy.loading || !hierarchy.canLoadMore || hierarchy.noSupport) return;
setLoading(true);
await hierarchy.load(pageSize); await hierarchy.load(pageSize);
setRooms(hierarchy.rooms); setRooms(hierarchy.rooms);
setLoading(false); }, [hierarchy]);
}, [loading, hierarchy]);
const loading = hierarchy?.loading ?? true;
return { loading, rooms, hierarchy, loadMore }; return { loading, rooms, hierarchy, loadMore };
}; };
@ -587,7 +576,7 @@ const SpaceHierarchy = ({
const [selected, setSelected] = useState(new Map<string, Set<string>>()); // Map<parentId, Set<childId>> const [selected, setSelected] = useState(new Map<string, Set<string>>()); // Map<parentId, Set<childId>>
const { loading, rooms, hierarchy, loadMore } = useSpaceSummary(space); const { loading, rooms, hierarchy, loadMore } = useRoomHierarchy(space);
const filteredRoomSet = useMemo<Set<IHierarchyRoom>>(() => { const filteredRoomSet = useMemo<Set<IHierarchyRoom>>(() => {
if (!rooms?.length) return new Set(); if (!rooms?.length) return new Set();