show unread badge on sidebar
This commit is contained in:
parent
188dce0e30
commit
a94acadbd8
4 changed files with 29 additions and 7 deletions
|
|
@ -28,6 +28,8 @@ import {
|
|||
RoomToNotifications,
|
||||
roomToNotificationsAtom,
|
||||
} from '../../../state/room/roomToNotifications';
|
||||
import { useRoomsUnread } from '../../../state/hooks/unread';
|
||||
import { unreadRoomsAtom } from '../../../state/room/roomToUnread';
|
||||
|
||||
type DirectMenuProps = {
|
||||
directRooms: string[];
|
||||
|
|
@ -77,6 +79,9 @@ export function DirectTab() {
|
|||
const directRooms = useDirectRooms();
|
||||
const roomsNotifications = useRoomsNotifications(directRooms, roomToNotificationsAtom);
|
||||
const notifications = useRoomsTotalNotifications(roomsNotifications);
|
||||
const unreadRooms = useRoomsUnread(directRooms, unreadRoomsAtom);
|
||||
const unreadOrNotification = (notifications && notifications.count > 0) || unreadRooms.size > 0;
|
||||
|
||||
const [menuAnchor, setMenuAnchor] = useState<RectCords>();
|
||||
|
||||
const directSelected = useDirectSelected();
|
||||
|
|
@ -114,7 +119,7 @@ export function DirectTab() {
|
|||
</SidebarAvatar>
|
||||
)}
|
||||
</SidebarItemTooltip>
|
||||
{notifications.count > 0 && (
|
||||
{unreadOrNotification && (
|
||||
<SidebarItemBadge hasCount={notifications.count > 0}>
|
||||
<UnreadBadge highlight={notifications.highlight} count={notifications.count} />
|
||||
</SidebarItemBadge>
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ import {
|
|||
RoomToNotifications,
|
||||
roomToNotificationsAtom,
|
||||
} from '../../../state/room/roomToNotifications';
|
||||
import { useRoomsUnread } from '../../../state/hooks/unread';
|
||||
import { unreadRoomsAtom } from '../../../state/room/roomToUnread';
|
||||
|
||||
type HomeMenuProps = {
|
||||
homeRooms: string[];
|
||||
|
|
@ -77,6 +79,9 @@ export function HomeTab() {
|
|||
const homeRooms = useHomeRooms();
|
||||
const roomsNotifications = useRoomsNotifications(homeRooms, roomToNotificationsAtom);
|
||||
const notifications = useRoomsTotalNotifications(roomsNotifications);
|
||||
const unreadRooms = useRoomsUnread(homeRooms, unreadRoomsAtom);
|
||||
const unreadOrNotification = (notifications && notifications.count > 0) || unreadRooms.size > 0;
|
||||
|
||||
const homeSelected = useHomeSelected();
|
||||
const [menuAnchor, setMenuAnchor] = useState<RectCords>();
|
||||
|
||||
|
|
@ -114,7 +119,7 @@ export function HomeTab() {
|
|||
</SidebarAvatar>
|
||||
)}
|
||||
</SidebarItemTooltip>
|
||||
{notifications.count > 0 && (
|
||||
{unreadOrNotification && (
|
||||
<SidebarItemBadge hasCount={notifications.count > 0}>
|
||||
<UnreadBadge highlight={notifications.highlight} count={notifications.count} />
|
||||
</SidebarItemBadge>
|
||||
|
|
|
|||
|
|
@ -103,6 +103,8 @@ import { roomToNotificationsAtom } from '../../../state/room/roomToNotifications
|
|||
import { mxcToHttp } from '../../../../client/mxcToHttp';
|
||||
import { useRoomHeroes } from '../../../hooks/storage/useRoomHeros';
|
||||
import { GetRoomAvatarCallback, useGetRoomAvatar } from '../../../hooks/storage/useGetRoomAvatar';
|
||||
import { useRoomsUnread } from '../../../state/hooks/unread';
|
||||
import { unreadRoomsAtom } from '../../../state/room/roomToUnread';
|
||||
|
||||
type SpaceMenuProps = {
|
||||
spaceId: string;
|
||||
|
|
@ -419,6 +421,8 @@ function SpaceTab({
|
|||
);
|
||||
const roomsNotifications = useRoomsNotifications(childRooms, roomToNotificationsAtom);
|
||||
const notifications = useRoomsTotalNotifications(roomsNotifications);
|
||||
const unreadRooms = useRoomsUnread(childRooms, unreadRoomsAtom);
|
||||
const unreadOrNotification = (notifications && notifications.count > 0) || unreadRooms.size > 0;
|
||||
|
||||
const spaceName = getName(spaceId) ?? spaceId;
|
||||
const avatarMxc = getAvatar(spaceId);
|
||||
|
|
@ -487,7 +491,7 @@ function SpaceTab({
|
|||
</SidebarAvatar>
|
||||
)}
|
||||
</SidebarItemTooltip>
|
||||
{notifications.count > 0 && (
|
||||
{unreadOrNotification && (
|
||||
<SidebarItemBadge hasCount={notifications.count > 0}>
|
||||
<UnreadBadge highlight={notifications.highlight} count={notifications.count} />
|
||||
</SidebarItemBadge>
|
||||
|
|
@ -584,6 +588,8 @@ function ClosedSpaceFolder({
|
|||
);
|
||||
const roomsNotifications = useRoomsNotifications(childRooms, roomToNotificationsAtom);
|
||||
const notifications = useRoomsTotalNotifications(roomsNotifications);
|
||||
const unreadRooms = useRoomsUnread(childRooms, unreadRoomsAtom);
|
||||
const unreadOrNotification = (notifications && notifications.count > 0) || unreadRooms.size > 0;
|
||||
|
||||
const handlerRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
|
|
@ -634,7 +640,7 @@ function ClosedSpaceFolder({
|
|||
</SidebarFolder>
|
||||
)}
|
||||
</SidebarItemTooltip>
|
||||
{notifications.count > 0 && (
|
||||
{unreadOrNotification && (
|
||||
<SidebarItemBadge hasCount={notifications.count > 0}>
|
||||
<UnreadBadge highlight={notifications.highlight} count={notifications.count} />
|
||||
</SidebarItemBadge>
|
||||
|
|
|
|||
|
|
@ -2,21 +2,27 @@ import { useCallback } from 'react';
|
|||
import { useAtomValue } from 'jotai';
|
||||
import { selectAtom } from 'jotai/utils';
|
||||
import { unreadRoomsAtom } from '../room/roomToUnread';
|
||||
import { useClient } from '../../hooks/useClient';
|
||||
|
||||
export const useRoomsUnread = (
|
||||
rooms: string[],
|
||||
unreadRooms: typeof unreadRoomsAtom
|
||||
): Set<string> => {
|
||||
const ct = useClient();
|
||||
|
||||
const selector = useCallback(
|
||||
(unreads: Set<string>) => new Set<string>(rooms.filter((roomId) => unreads.has(roomId))),
|
||||
[rooms]
|
||||
(unread: Set<string>) =>
|
||||
new Set<string>(
|
||||
rooms.filter((roomId) => unread.has(roomId) && !ct.spaceRooms.read.has(roomId))
|
||||
),
|
||||
[rooms, ct]
|
||||
);
|
||||
|
||||
return useAtomValue(selectAtom(unreadRooms, selector));
|
||||
};
|
||||
|
||||
export const useRoomUnread = (roomId: string, unreadRooms: typeof unreadRoomsAtom): boolean => {
|
||||
const selector = useCallback((unreads: Set<string>) => unreads.has(roomId), [roomId]);
|
||||
const selector = useCallback((unread: Set<string>) => unread.has(roomId), [roomId]);
|
||||
|
||||
return useAtomValue(selectAtom(unreadRooms, selector));
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue