2022-10-24 08:50:21 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { NotificationCount, NotificationCountType, Room, RoomEvent } from "matrix-js-sdk/src/models/room";
|
2023-01-11 12:49:03 +01:00
|
|
|
import { Thread } from "matrix-js-sdk/src/models/thread";
|
2022-10-24 08:50:21 +02:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import { getUnsentMessages } from "../components/structures/RoomStatusBar";
|
|
|
|
import { getRoomNotifsState, getUnreadNotificationCount, RoomNotifState } from "../RoomNotifs";
|
|
|
|
import { NotificationColor } from "../stores/notifications/NotificationColor";
|
2023-01-11 12:49:03 +01:00
|
|
|
import { doesRoomOrThreadHaveUnreadMessages } from "../Unread";
|
2022-10-24 08:50:21 +02:00
|
|
|
import { EffectiveMembership, getEffectiveMembership } from "../utils/membership";
|
|
|
|
import { useEventEmitter } from "./useEventEmitter";
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
export const useUnreadNotifications = (
|
|
|
|
room: Room,
|
|
|
|
threadId?: string,
|
|
|
|
): {
|
2022-10-24 08:50:21 +02:00
|
|
|
symbol: string | null;
|
|
|
|
count: number;
|
|
|
|
color: NotificationColor;
|
|
|
|
} => {
|
|
|
|
const [symbol, setSymbol] = useState<string | null>(null);
|
|
|
|
const [count, setCount] = useState<number>(0);
|
|
|
|
const [color, setColor] = useState<NotificationColor>(0);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
useEventEmitter(
|
|
|
|
room,
|
|
|
|
RoomEvent.UnreadNotifications,
|
2022-10-24 08:50:21 +02:00
|
|
|
(unreadNotifications: NotificationCount, evtThreadId?: string) => {
|
|
|
|
// Discarding all events not related to the thread if one has been setup
|
|
|
|
if (threadId && threadId !== evtThreadId) return;
|
|
|
|
updateNotificationState();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
useEventEmitter(room, RoomEvent.Receipt, () => updateNotificationState());
|
|
|
|
useEventEmitter(room, RoomEvent.Timeline, () => updateNotificationState());
|
|
|
|
useEventEmitter(room, RoomEvent.Redaction, () => updateNotificationState());
|
|
|
|
useEventEmitter(room, RoomEvent.LocalEchoUpdated, () => updateNotificationState());
|
|
|
|
useEventEmitter(room, RoomEvent.MyMembership, () => updateNotificationState());
|
|
|
|
|
|
|
|
const updateNotificationState = useCallback(() => {
|
|
|
|
if (getUnsentMessages(room, threadId).length > 0) {
|
|
|
|
setSymbol("!");
|
|
|
|
setCount(1);
|
|
|
|
setColor(NotificationColor.Unsent);
|
|
|
|
} else if (getEffectiveMembership(room.getMyMembership()) === EffectiveMembership.Invite) {
|
|
|
|
setSymbol("!");
|
|
|
|
setCount(1);
|
|
|
|
setColor(NotificationColor.Red);
|
2022-11-29 11:55:15 +01:00
|
|
|
} else if (getRoomNotifsState(room.client, room.roomId) === RoomNotifState.Mute) {
|
2022-10-24 08:50:21 +02:00
|
|
|
setSymbol(null);
|
|
|
|
setCount(0);
|
|
|
|
setColor(NotificationColor.None);
|
|
|
|
} else {
|
|
|
|
const redNotifs = getUnreadNotificationCount(room, NotificationCountType.Highlight, threadId);
|
|
|
|
const greyNotifs = getUnreadNotificationCount(room, NotificationCountType.Total, threadId);
|
|
|
|
|
|
|
|
const trueCount = greyNotifs || redNotifs;
|
|
|
|
setCount(trueCount);
|
|
|
|
setSymbol(null);
|
|
|
|
if (redNotifs > 0) {
|
|
|
|
setColor(NotificationColor.Red);
|
|
|
|
} else if (greyNotifs > 0) {
|
|
|
|
setColor(NotificationColor.Grey);
|
2023-01-11 12:49:03 +01:00
|
|
|
} else {
|
2022-10-24 08:50:21 +02:00
|
|
|
// We don't have any notified messages, but we might have unread messages. Let's
|
|
|
|
// find out.
|
2023-01-11 12:49:03 +01:00
|
|
|
let roomOrThread: Room | Thread = room;
|
|
|
|
if (threadId) {
|
|
|
|
roomOrThread = room.getThread(threadId)!;
|
|
|
|
}
|
|
|
|
const hasUnread = doesRoomOrThreadHaveUnreadMessages(roomOrThread);
|
2022-10-24 08:50:21 +02:00
|
|
|
setColor(hasUnread ? NotificationColor.Bold : NotificationColor.None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [room, threadId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
updateNotificationState();
|
|
|
|
}, [updateNotificationState]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
symbol,
|
|
|
|
count,
|
|
|
|
color,
|
|
|
|
};
|
|
|
|
};
|