fix closed category of home, direct and space tabs
This commit is contained in:
parent
21bc1521c9
commit
89f6409e4e
5 changed files with 166 additions and 41 deletions
54
src/app/hooks/useRoomsLatestEvent.ts
Normal file
54
src/app/hooks/useRoomsLatestEvent.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { MessageEventWithoutRoomId } from 'neurone/lib/api/types';
|
||||
import { useClient } from './useClient';
|
||||
|
||||
const getLatestEvent = (
|
||||
rooms: string[],
|
||||
roomToLatestEvent: Map<string, MessageEventWithoutRoomId>
|
||||
) => {
|
||||
const rToE = new Map<string, MessageEventWithoutRoomId>();
|
||||
|
||||
rooms.forEach((roomId) => {
|
||||
const event = roomToLatestEvent.get(roomId);
|
||||
if (event) {
|
||||
rToE.set(roomId, event);
|
||||
}
|
||||
});
|
||||
|
||||
return rToE;
|
||||
};
|
||||
|
||||
export const useRoomsLatestEvent = (rooms: string[]) => {
|
||||
const ct = useClient();
|
||||
|
||||
const [roomToLatestEvent, setRoomToLatestEvent] = useState(() =>
|
||||
getLatestEvent(rooms, ct.roomToLatestEvent.read)
|
||||
);
|
||||
|
||||
const updateRoomToLatestEvent = useCallback((next: Map<string, MessageEventWithoutRoomId>) => {
|
||||
setRoomToLatestEvent((current) => {
|
||||
const hasChanged =
|
||||
current.size !== next.size ||
|
||||
Array.from(next.entries()).some(
|
||||
([roomId, nextEvent]) => current.get(roomId)?.event_id !== nextEvent.event_id
|
||||
);
|
||||
|
||||
return hasChanged ? next : current;
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const handleLatestEvent = () => {
|
||||
updateRoomToLatestEvent(getLatestEvent(rooms, ct.roomToLatestEvent.read));
|
||||
};
|
||||
|
||||
updateRoomToLatestEvent(getLatestEvent(rooms, ct.roomToLatestEvent.read));
|
||||
|
||||
const unsubscribe = ct.roomToLatestEvent.subscribe(handleLatestEvent);
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
}, [ct, rooms, updateRoomToLatestEvent]);
|
||||
|
||||
return roomToLatestEvent;
|
||||
};
|
||||
|
|
@ -6,13 +6,17 @@ import { getHierarchy, GetHierarchyOK } from 'neurone/lib/api/matrix/client/v1/r
|
|||
import { parseMatrixResp } from 'neurone/lib/matrix-request';
|
||||
import { SpaceChildContent } from '../../types/matrix/room';
|
||||
import { isRoomId } from '../utils/matrix';
|
||||
import { SortFunc, byOrderKey, byTsOldToNew } from '../utils/sort';
|
||||
import { SortFunc, byOrderKey, byTsOldToNew, factoryRoomIdByActivity } from '../utils/sort';
|
||||
import { ErrorCode } from '../cs-errorcode';
|
||||
import { useObservableValue } from './useObservableValue';
|
||||
import { useClient } from './useClient';
|
||||
import { useAllJoinedRoomsSet } from './useGetRoom';
|
||||
import { spaceRoomsAtom } from '../state/spaceRooms';
|
||||
import { rError } from '../../client/reader';
|
||||
import { useRoomsLatestEvent } from './useRoomsLatestEvent';
|
||||
import { RoomToNotifications } from '../state/room/roomToNotifications';
|
||||
import { makeNavCategoryId } from '../state/closedNavCategories';
|
||||
import { getRoomTotalNotifications } from '../state/hooks/roomNotifications';
|
||||
|
||||
export type HierarchyItemSpace = {
|
||||
roomId: string;
|
||||
|
|
@ -157,9 +161,8 @@ const getSpaceJoinedHierarchy = (
|
|||
rootSpaceId: string,
|
||||
joinedRooms: Set<string>,
|
||||
roomToChildEvents: Map<string, StateEventWithoutRoomId<SpaceChildContent>[]>,
|
||||
spaceRooms: Set<string>,
|
||||
sortRoomItems: (parentId: string, items: HierarchyItem[]) => HierarchyItem[]
|
||||
): HierarchyItem[] => {
|
||||
spaceRooms: Set<string>
|
||||
) => {
|
||||
const spaceItems: HierarchyItemSpace[] = getHierarchySpaces(
|
||||
rootSpaceId,
|
||||
roomToChildEvents,
|
||||
|
|
@ -167,7 +170,7 @@ const getSpaceJoinedHierarchy = (
|
|||
new Set()
|
||||
);
|
||||
|
||||
const hierarchy: HierarchyItem[] = spaceItems.flatMap((spaceItem) => {
|
||||
const hierarchy = spaceItems.flatMap((spaceItem) => {
|
||||
const childEvents = roomToChildEvents.get(spaceItem.roomId);
|
||||
|
||||
const childItems: HierarchyItemRoom[] = [];
|
||||
|
|
@ -187,38 +190,77 @@ const getSpaceJoinedHierarchy = (
|
|||
});
|
||||
if (childItems.length === 0) return [];
|
||||
|
||||
return [spaceItem, ...sortRoomItems(spaceItem.roomId, childItems)];
|
||||
return {
|
||||
spaceItem,
|
||||
childItems,
|
||||
};
|
||||
});
|
||||
|
||||
return hierarchy;
|
||||
};
|
||||
|
||||
export const useSpaceJoinedHierarchy = (spaceId: string): HierarchyItem[] => {
|
||||
export const useSpaceJoinedHierarchy = (spaceId: string) => {
|
||||
const ct = useClient();
|
||||
const roomToChildEvents = useObservableValue(ct.roomToChildEvents);
|
||||
const allJoinedRooms = useAllJoinedRoomsSet();
|
||||
const spaceRooms = useObservableValue(ct.spaceRooms);
|
||||
|
||||
const sortRoomItems = useCallback((sId: string, items: HierarchyItem[]) => {
|
||||
items.sort(hierarchyItemTs).sort(hierarchyItemByOrder);
|
||||
return items;
|
||||
}, []);
|
||||
|
||||
const hierarchy = useMemo(
|
||||
() =>
|
||||
getSpaceJoinedHierarchy(
|
||||
spaceId,
|
||||
allJoinedRooms,
|
||||
roomToChildEvents,
|
||||
spaceRooms,
|
||||
sortRoomItems
|
||||
),
|
||||
[spaceId, allJoinedRooms, roomToChildEvents, spaceRooms, sortRoomItems]
|
||||
() => getSpaceJoinedHierarchy(spaceId, allJoinedRooms, roomToChildEvents, spaceRooms),
|
||||
[spaceId, allJoinedRooms, roomToChildEvents, spaceRooms]
|
||||
);
|
||||
|
||||
return hierarchy;
|
||||
};
|
||||
|
||||
export const useJoinedHierarchyItems = (
|
||||
spaceId: string,
|
||||
hierarchy: ReturnType<typeof useSpaceJoinedHierarchy>,
|
||||
rooms: string[],
|
||||
roomToNotifications: RoomToNotifications,
|
||||
unreadRooms: Set<string>,
|
||||
closedCategories: Set<string>,
|
||||
selectedRoomId: string | undefined
|
||||
) => {
|
||||
const roomToLatestEvent = useRoomsLatestEvent(rooms);
|
||||
|
||||
const prepareItems = useCallback(
|
||||
(sId: string, items: HierarchyItem[]) => {
|
||||
if (closedCategories.has(makeNavCategoryId(spaceId, sId))) {
|
||||
return items
|
||||
.filter((item) => {
|
||||
if (item.roomId === selectedRoomId) return true;
|
||||
const notification = roomToNotifications.get(item.roomId);
|
||||
if (!notification) return false;
|
||||
const total = getRoomTotalNotifications(notification);
|
||||
return total.count > 0 || unreadRooms.has(item.roomId);
|
||||
})
|
||||
.sort((h1, h2) =>
|
||||
factoryRoomIdByActivity((rId) => roomToLatestEvent.get(rId)?.origin_server_ts)(
|
||||
h1.roomId,
|
||||
h2.roomId
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
items.sort(hierarchyItemTs).sort(hierarchyItemByOrder);
|
||||
return items;
|
||||
},
|
||||
[roomToLatestEvent, closedCategories, spaceId, roomToNotifications, unreadRooms, selectedRoomId]
|
||||
);
|
||||
|
||||
const hierarchyItems: HierarchyItem[] = useMemo(
|
||||
() =>
|
||||
hierarchy.flatMap((item) => [
|
||||
item.spaceItem,
|
||||
...prepareItems(item.spaceItem.roomId, item.childItems),
|
||||
]),
|
||||
[hierarchy, prepareItems]
|
||||
);
|
||||
|
||||
return hierarchyItems;
|
||||
};
|
||||
|
||||
// we will paginate until 5000 items
|
||||
const PER_PAGE_COUNT = 100;
|
||||
const MAX_AUTO_PAGE_COUNT = 50;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import React, { MouseEventHandler, forwardRef, useMemo, useRef, useState } from 'react';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import { useAtom } from 'jotai';
|
||||
import {
|
||||
Avatar,
|
||||
Box,
|
||||
|
|
@ -18,8 +18,7 @@ import {
|
|||
import { useVirtualizer } from '@tanstack/react-virtual';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
||||
import { factoryRoomIdByActivity, factoryRoomIdByAtoZ } from '../../../utils/sort';
|
||||
import { factoryRoomIdByActivity } from '../../../utils/sort';
|
||||
import {
|
||||
NavButton,
|
||||
NavCategory,
|
||||
|
|
@ -60,6 +59,7 @@ import { useObservableValue } from '../../../hooks/useObservableValue';
|
|||
import { useClient } from '../../../hooks/useClient';
|
||||
import { unreadRoomsAtom } from '../../../state/room/roomToUnread';
|
||||
import { useRoomsUnread } from '../../../state/hooks/unread';
|
||||
import { useRoomsLatestEvent } from '../../../hooks/useRoomsLatestEvent';
|
||||
|
||||
type DirectMenuProps = {
|
||||
requestClose: () => void;
|
||||
|
|
@ -190,6 +190,7 @@ export function Direct() {
|
|||
const roomToJoinRule = useObservableValue(ct.roomToJoinRule);
|
||||
const roomToNotifications = useRoomsNotifications(directs, roomToNotificationsAtom);
|
||||
const unreadRooms = useRoomsUnread(directs, unreadRoomsAtom);
|
||||
const roomToLatestEvent = useRoomsLatestEvent(directs);
|
||||
|
||||
const notificationPreferences = useRoomsNotificationPreferencesContext();
|
||||
const navigate = useNavigate();
|
||||
|
|
@ -201,7 +202,6 @@ export function Direct() {
|
|||
const [closedCategories, setClosedCategories] = useAtom(useClosedNavCategoriesAtom());
|
||||
|
||||
const sortedDirects = useMemo(() => {
|
||||
// TODO: sort by activity
|
||||
if (closedCategories.has(DEFAULT_CATEGORY_ID)) {
|
||||
return directs
|
||||
.filter((roomId) => {
|
||||
|
|
@ -209,12 +209,21 @@ export function Direct() {
|
|||
const notification = roomToNotifications.get(roomId);
|
||||
if (!notification) return false;
|
||||
const total = getRoomTotalNotifications(notification);
|
||||
return total.count > 0;
|
||||
return total.count > 0 || unreadRooms.has(roomId);
|
||||
})
|
||||
.sort(factoryRoomIdByAtoZ(getRoomName));
|
||||
.sort(factoryRoomIdByActivity((rId) => roomToLatestEvent.get(rId)?.origin_server_ts));
|
||||
}
|
||||
return Array.from(directs).sort(factoryRoomIdByAtoZ(getRoomName));
|
||||
}, [directs, closedCategories, roomToNotifications, selectedRoomId, getRoomName]);
|
||||
return Array.from(directs).sort(
|
||||
factoryRoomIdByActivity((rId) => roomToLatestEvent.get(rId)?.origin_server_ts)
|
||||
);
|
||||
}, [
|
||||
directs,
|
||||
closedCategories,
|
||||
roomToNotifications,
|
||||
unreadRooms,
|
||||
roomToLatestEvent,
|
||||
selectedRoomId,
|
||||
]);
|
||||
|
||||
const virtualizer = useVirtualizer({
|
||||
count: sortedDirects.length,
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ import {
|
|||
toRem,
|
||||
} from 'folds';
|
||||
import { useVirtualizer } from '@tanstack/react-virtual';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import { useAtom } from 'jotai';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { factoryRoomIdByAtoZ } from '../../../utils/sort';
|
||||
import { factoryRoomIdByActivity, factoryRoomIdByAtoZ } from '../../../utils/sort';
|
||||
import {
|
||||
NavButton,
|
||||
NavCategory,
|
||||
|
|
@ -72,6 +72,7 @@ import { roomToNotificationsAtom } from '../../../state/room/roomToNotifications
|
|||
import { useGetRoomAvatar } from '../../../hooks/storage/useGetRoomAvatar';
|
||||
import { unreadRoomsAtom } from '../../../state/room/roomToUnread';
|
||||
import { useRoomsUnread } from '../../../state/hooks/unread';
|
||||
import { useRoomsLatestEvent } from '../../../hooks/useRoomsLatestEvent';
|
||||
|
||||
type HomeMenuProps = {
|
||||
requestClose: () => void;
|
||||
|
|
@ -214,6 +215,7 @@ export function Home() {
|
|||
const roomToJoinRule = useObservableValue(ct.roomToJoinRule);
|
||||
const roomToNotifications = useRoomsNotifications(rooms, roomToNotificationsAtom);
|
||||
const unreadRooms = useRoomsUnread(rooms, unreadRoomsAtom);
|
||||
const roomToLatestEvent = useRoomsLatestEvent(rooms);
|
||||
|
||||
const notificationPreferences = useRoomsNotificationPreferencesContext();
|
||||
const navigate = useNavigate();
|
||||
|
|
@ -226,19 +228,25 @@ export function Home() {
|
|||
|
||||
const sortedRooms = useMemo(() => {
|
||||
if (closedCategories.has(DEFAULT_CATEGORY_ID)) {
|
||||
// TODO: sort by activity
|
||||
return rooms
|
||||
.filter((roomId) => {
|
||||
if (roomId === selectedRoomId) return true;
|
||||
const notification = roomToNotifications.get(roomId);
|
||||
if (!notification) return false;
|
||||
const total = getRoomTotalNotifications(notification);
|
||||
return total.count > 0;
|
||||
return total.count > 0 || unreadRooms.has(roomId);
|
||||
})
|
||||
.sort(factoryRoomIdByAtoZ(getRoomName));
|
||||
.sort(factoryRoomIdByActivity((rId) => roomToLatestEvent.get(rId)?.origin_server_ts));
|
||||
}
|
||||
return Array.from(rooms).sort(factoryRoomIdByAtoZ(getRoomName));
|
||||
}, [getRoomName, rooms, closedCategories, roomToNotifications, selectedRoomId]);
|
||||
}, [
|
||||
getRoomName,
|
||||
rooms,
|
||||
closedCategories,
|
||||
roomToNotifications,
|
||||
unreadRooms,
|
||||
roomToLatestEvent,
|
||||
selectedRoomId,
|
||||
]);
|
||||
|
||||
const virtualizer = useVirtualizer({
|
||||
count: sortedRooms.length,
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ import { RoomNavCategoryButton, RoomNavItem } from '../../../features/room-nav';
|
|||
import { makeNavCategoryId } from '../../../state/closedNavCategories';
|
||||
import { useCategoryHandler } from '../../../hooks/useCategoryHandler';
|
||||
import { useNavToActivePathMapper } from '../../../hooks/useNavToActivePathMapper';
|
||||
import { useSpaceJoinedHierarchy } from '../../../hooks/useSpaceHierarchy';
|
||||
import { useJoinedHierarchyItems, useSpaceJoinedHierarchy } from '../../../hooks/useSpaceHierarchy';
|
||||
import { allRoomsAtom } from '../../../state/room-list/roomList';
|
||||
import { PageNav, PageNavContent, PageNavHeader } from '../../../components/page';
|
||||
import { usePowerLevels, usePowerLevelsState } from '../../../hooks/usePowerLevels';
|
||||
|
|
@ -400,7 +400,10 @@ export function Space() {
|
|||
|
||||
const hierarchy = useSpaceJoinedHierarchy(spaceId);
|
||||
|
||||
const rooms = useMemo(() => hierarchy.map((item) => item.roomId), [hierarchy]);
|
||||
const rooms = useMemo(
|
||||
() => hierarchy.flatMap((item) => item.childItems.map((c) => c.roomId)),
|
||||
[hierarchy]
|
||||
);
|
||||
|
||||
const getRoomHeroes = useRoomHeroes();
|
||||
const getRoomName = useGetRoomName(getRoomHeroes);
|
||||
|
|
@ -410,8 +413,18 @@ export function Space() {
|
|||
const roomToNotifications = useRoomsNotifications(rooms, roomToNotificationsAtom);
|
||||
const unreadRooms = useRoomsUnread(rooms, unreadRoomsAtom);
|
||||
|
||||
const hierarchyItems = useJoinedHierarchyItems(
|
||||
spaceId,
|
||||
hierarchy,
|
||||
rooms,
|
||||
roomToNotifications,
|
||||
unreadRooms,
|
||||
closedCategories,
|
||||
selectedRoomId
|
||||
);
|
||||
|
||||
const virtualizer = useVirtualizer({
|
||||
count: hierarchy.length,
|
||||
count: hierarchyItems.length,
|
||||
getScrollElement: () => scrollRef.current,
|
||||
estimateSize: () => 0,
|
||||
overscan: 10,
|
||||
|
|
@ -473,7 +486,7 @@ export function Space() {
|
|||
}}
|
||||
>
|
||||
{virtualizer.getVirtualItems().map((vItem) => {
|
||||
const item = hierarchy[vItem.index] ?? {};
|
||||
const item = hierarchyItems[vItem.index] ?? {};
|
||||
if (!item) return null;
|
||||
const { roomId } = item;
|
||||
|
||||
|
|
@ -501,7 +514,6 @@ export function Space() {
|
|||
);
|
||||
}
|
||||
|
||||
// TODO: closedCategories.has(makeNavCategoryId(spaceId, parentId)
|
||||
const n = roomToNotifications.get(roomId);
|
||||
const notifications = n && getRoomTotalNotifications(n);
|
||||
return (
|
||||
|
|
|
|||
Loading…
Reference in a new issue