2022-10-24 07:50:21 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2022-10-24 07:50:21 +01:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-10-24 07:50:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-08-04 08:36:16 +01:00
|
|
|
import { RoomEvent } from "matrix-js-sdk/src/matrix";
|
2024-04-29 16:30:19 +01:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2022-10-24 07:50:21 +01:00
|
|
|
|
2023-08-04 08:36:16 +01:00
|
|
|
import type { NotificationCount, Room } from "matrix-js-sdk/src/matrix";
|
2023-01-31 09:58:17 +00:00
|
|
|
import { determineUnreadState } from "../RoomNotifs";
|
2024-01-15 15:25:48 +00:00
|
|
|
import { NotificationLevel } from "../stores/notifications/NotificationLevel";
|
2022-10-24 07:50:21 +01:00
|
|
|
import { useEventEmitter } from "./useEventEmitter";
|
|
|
|
|
|
|
|
|
|
export const useUnreadNotifications = (
|
2023-01-31 12:38:25 +00:00
|
|
|
room?: Room,
|
2022-10-24 07:50:21 +01:00
|
|
|
threadId?: string,
|
|
|
|
|
): {
|
|
|
|
|
symbol: string | null;
|
|
|
|
|
count: number;
|
2024-01-15 15:25:48 +00:00
|
|
|
level: NotificationLevel;
|
2022-10-24 07:50:21 +01:00
|
|
|
} => {
|
|
|
|
|
const [symbol, setSymbol] = useState<string | null>(null);
|
|
|
|
|
const [count, setCount] = useState<number>(0);
|
2024-01-15 15:25:48 +00:00
|
|
|
const [level, setLevel] = useState<NotificationLevel>(NotificationLevel.None);
|
2022-10-24 07:50:21 +01:00
|
|
|
|
|
|
|
|
useEventEmitter(
|
|
|
|
|
room,
|
|
|
|
|
RoomEvent.UnreadNotifications,
|
|
|
|
|
(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(() => {
|
2024-04-29 16:30:19 +01:00
|
|
|
const { symbol, count, level } = determineUnreadState(room, threadId, false);
|
2023-01-31 09:58:17 +00:00
|
|
|
setSymbol(symbol);
|
|
|
|
|
setCount(count);
|
2024-01-15 15:25:48 +00:00
|
|
|
setLevel(level);
|
2024-04-29 16:30:19 +01:00
|
|
|
}, [room, threadId]);
|
2022-10-24 07:50:21 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
updateNotificationState();
|
|
|
|
|
}, [updateNotificationState]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
symbol,
|
|
|
|
|
count,
|
2024-01-15 15:25:48 +00:00
|
|
|
level,
|
2022-10-24 07:50:21 +01:00
|
|
|
};
|
|
|
|
|
};
|