2020-06-08 13:42:18 -06:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-06-08 13:42:18 -06:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
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.
|
2020-06-08 13:42:18 -06:00
|
|
|
*/
|
|
|
|
|
|
2025-03-27 10:43:58 +00:00
|
|
|
import React, { type JSX, type ReactNode } from "react";
|
2024-01-09 11:46:27 +00:00
|
|
|
import { Tooltip } from "@vector-im/compound-web";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2020-06-19 15:44:37 -06:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type XOR } from "../../../@types/common";
|
|
|
|
|
import { type NotificationState, NotificationStateEvents } from "../../../stores/notifications/NotificationState";
|
2021-09-10 09:15:54 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2024-01-15 15:25:48 +00:00
|
|
|
import { NotificationLevel } from "../../../stores/notifications/NotificationLevel";
|
2022-10-24 07:50:21 +01:00
|
|
|
import { StatelessNotificationBadge } from "./NotificationBadge/StatelessNotificationBadge";
|
2020-06-08 13:42:18 -06:00
|
|
|
|
|
|
|
|
interface IProps {
|
2020-07-08 19:26:25 -06:00
|
|
|
notification: NotificationState;
|
2020-06-08 13:42:18 -06:00
|
|
|
|
|
|
|
|
/**
|
2024-03-15 11:06:12 +00:00
|
|
|
* If true, show nothing if the notification would only cause a dot to be shown rather than
|
|
|
|
|
* a badge. That is: only display badges and not dots. Default: false.
|
2020-06-08 13:42:18 -06:00
|
|
|
*/
|
2024-03-15 11:06:12 +00:00
|
|
|
hideIfDot?: boolean;
|
2020-06-19 15:44:37 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The room ID, if any, the badge represents.
|
|
|
|
|
*/
|
|
|
|
|
roomId?: string;
|
2020-06-08 13:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
2020-07-02 19:48:06 +01:00
|
|
|
interface IClickableProps extends IProps, React.InputHTMLAttributes<Element> {
|
2021-09-10 09:15:54 +01:00
|
|
|
showUnsentTooltip?: boolean;
|
2020-07-02 19:48:06 +01:00
|
|
|
/**
|
|
|
|
|
* If specified will return an AccessibleButton instead of a div.
|
|
|
|
|
*/
|
2023-05-05 11:09:41 +01:00
|
|
|
onClick(ev: React.MouseEvent): void;
|
2020-07-02 19:48:06 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-08 13:42:18 -06:00
|
|
|
interface IState {
|
2024-03-15 11:06:12 +00:00
|
|
|
showCounts: boolean; // whether to show counts.
|
2020-06-08 13:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
2020-07-02 19:48:06 +01:00
|
|
|
export default class NotificationBadge extends React.PureComponent<XOR<IProps, IClickableProps>, IState> {
|
2024-11-01 17:39:08 +00:00
|
|
|
private countWatcherRef?: string;
|
2020-06-19 15:44:37 -06:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(props: IProps) {
|
2020-06-08 13:42:18 -06:00
|
|
|
super(props);
|
2020-06-19 15:44:37 -06:00
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
showCounts: SettingsStore.getValue("Notifications.alwaysShowBadgeCounts", this.roomId),
|
|
|
|
|
};
|
2024-11-01 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get roomId(): string | null {
|
|
|
|
|
// We should convert this to null for safety with the SettingsStore
|
|
|
|
|
return this.props.roomId || null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public componentDidMount(): void {
|
|
|
|
|
this.props.notification.on(NotificationStateEvents.Update, this.onNotificationUpdate);
|
2020-06-19 15:44:37 -06:00
|
|
|
|
|
|
|
|
this.countWatcherRef = SettingsStore.watchSetting(
|
|
|
|
|
"Notifications.alwaysShowBadgeCounts",
|
|
|
|
|
this.roomId,
|
|
|
|
|
this.countPreferenceChanged,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2020-06-19 15:44:37 -06:00
|
|
|
SettingsStore.unwatchSetting(this.countWatcherRef);
|
2021-12-07 12:51:34 +00:00
|
|
|
this.props.notification.off(NotificationStateEvents.Update, this.onNotificationUpdate);
|
2020-06-08 13:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentDidUpdate(prevProps: Readonly<IProps>): void {
|
2020-06-08 13:42:18 -06:00
|
|
|
if (prevProps.notification) {
|
2021-12-07 12:51:34 +00:00
|
|
|
prevProps.notification.off(NotificationStateEvents.Update, this.onNotificationUpdate);
|
2020-06-08 13:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-07 12:51:34 +00:00
|
|
|
this.props.notification.on(NotificationStateEvents.Update, this.onNotificationUpdate);
|
2020-06-08 13:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private countPreferenceChanged = (): void => {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ showCounts: SettingsStore.getValue("Notifications.alwaysShowBadgeCounts", this.roomId) });
|
2020-06-19 15:44:37 -06:00
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onNotificationUpdate = (): void => {
|
2020-06-08 13:42:18 -06:00
|
|
|
this.forceUpdate(); // notification state changed - update
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-13 15:07:20 +00:00
|
|
|
public render(): ReactNode {
|
2020-08-29 01:11:08 +01:00
|
|
|
/* eslint @typescript-eslint/no-unused-vars: ["error", { "ignoreRestSiblings": true }] */
|
2024-03-15 11:06:12 +00:00
|
|
|
const { notification, showUnsentTooltip, hideIfDot, onClick, tabIndex } = this.props;
|
2022-10-25 14:31:38 +01:00
|
|
|
|
2023-09-19 14:24:35 +03:00
|
|
|
if (notification.isIdle && !notification.knocked) return null;
|
2024-03-15 11:06:12 +00:00
|
|
|
if (hideIfDot && notification.level < NotificationLevel.Notification) {
|
|
|
|
|
// This would just be a dot and we've been told not to show dots, so don't show it
|
2024-03-19 13:28:20 +00:00
|
|
|
return null;
|
2022-10-25 14:31:38 +01:00
|
|
|
}
|
2020-06-08 13:42:18 -06:00
|
|
|
|
2023-05-25 09:39:23 +01:00
|
|
|
const commonProps: React.ComponentProps<typeof StatelessNotificationBadge> = {
|
|
|
|
|
symbol: notification.symbol,
|
|
|
|
|
count: notification.count,
|
2024-01-15 15:25:48 +00:00
|
|
|
level: notification.level,
|
2023-09-19 14:24:35 +03:00
|
|
|
knocked: notification.knocked,
|
2023-05-25 09:39:23 +01:00
|
|
|
};
|
|
|
|
|
|
2024-01-09 11:46:27 +00:00
|
|
|
let badge: JSX.Element;
|
2023-05-25 09:39:23 +01:00
|
|
|
if (onClick) {
|
2024-01-09 11:46:27 +00:00
|
|
|
badge = <StatelessNotificationBadge {...commonProps} onClick={onClick} tabIndex={tabIndex} />;
|
|
|
|
|
} else {
|
|
|
|
|
badge = <StatelessNotificationBadge {...commonProps} />;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 15:25:48 +00:00
|
|
|
if (showUnsentTooltip && notification.level === NotificationLevel.Unsent) {
|
2023-05-25 09:39:23 +01:00
|
|
|
return (
|
2024-04-12 14:56:23 +02:00
|
|
|
<Tooltip label={_t("notifications|message_didnt_send")} placement="right">
|
2024-01-09 11:46:27 +00:00
|
|
|
{badge}
|
|
|
|
|
</Tooltip>
|
2023-05-25 09:39:23 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 11:46:27 +00:00
|
|
|
return badge;
|
2020-06-08 13:42:18 -06:00
|
|
|
}
|
|
|
|
|
}
|