2022-10-24 08:50:21 +02:00
|
|
|
/*
|
2023-01-31 10:58:17 +01:00
|
|
|
Copyright 2022 - 2023 The Matrix.org Foundation C.I.C.
|
2022-10-24 08:50:21 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2023-08-04 09:36:16 +02:00
|
|
|
import { RoomEvent } from "matrix-js-sdk/src/matrix";
|
2022-10-24 08:50:21 +02:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
|
2023-08-04 09:36:16 +02:00
|
|
|
import type { NotificationCount, Room } from "matrix-js-sdk/src/matrix";
|
2023-01-31 10:58:17 +01:00
|
|
|
import { determineUnreadState } from "../RoomNotifs";
|
2023-01-31 13:38:25 +01:00
|
|
|
import { NotificationColor } from "../stores/notifications/NotificationColor";
|
2022-10-24 08:50:21 +02:00
|
|
|
import { useEventEmitter } from "./useEventEmitter";
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
export const useUnreadNotifications = (
|
2023-01-31 13:38:25 +01:00
|
|
|
room?: Room,
|
2022-12-12 12:24:14 +01:00
|
|
|
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);
|
2023-01-31 13:38:25 +01:00
|
|
|
const [color, setColor] = useState<NotificationColor>(NotificationColor.None);
|
2022-10-24 08:50:21 +02:00
|
|
|
|
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(() => {
|
2023-01-31 10:58:17 +01:00
|
|
|
const { symbol, count, color } = determineUnreadState(room, threadId);
|
|
|
|
setSymbol(symbol);
|
|
|
|
setCount(count);
|
|
|
|
setColor(color);
|
2022-10-24 08:50:21 +02:00
|
|
|
}, [room, threadId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
updateNotificationState();
|
|
|
|
}, [updateNotificationState]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
symbol,
|
|
|
|
count,
|
|
|
|
color,
|
|
|
|
};
|
|
|
|
};
|