Files
ThreadNet-Web/src/components/views/rooms/NotificationBadge.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
4.4 KiB
TypeScript
Raw Normal View History

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.
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-02-05 13:25:06 +00:00
import React, { type ReactNode } from "react";
import { Tooltip } from "@vector-im/compound-web";
2021-10-22 17:23:32 -05: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";
import { _t } from "../../../languageHandler";
import { NotificationLevel } from "../../../stores/notifications/NotificationLevel";
import { StatelessNotificationBadge } from "./NotificationBadge/StatelessNotificationBadge";
2020-06-08 13:42:18 -06:00
interface IProps {
notification: NotificationState;
2020-06-08 13:42:18 -06: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
*/
hideIfDot?: boolean;
/**
* The room ID, if any, the badge represents.
*/
roomId?: string;
2020-06-08 13:42:18 -06:00
}
interface IClickableProps extends IProps, React.InputHTMLAttributes<Element> {
showUnsentTooltip?: boolean;
/**
* If specified will return an AccessibleButton instead of a div.
*/
onClick(ev: React.MouseEvent): void;
}
2020-06-08 13:42:18 -06:00
interface IState {
showCounts: boolean; // whether to show counts.
2020-06-08 13:42:18 -06:00
}
export default class NotificationBadge extends React.PureComponent<XOR<IProps, IClickableProps>, IState> {
private countWatcherRef?: string;
public constructor(props: IProps) {
2020-06-08 13:42:18 -06:00
super(props);
this.state = {
showCounts: SettingsStore.getValue("Notifications.alwaysShowBadgeCounts", this.roomId),
};
}
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);
this.countWatcherRef = SettingsStore.watchSetting(
"Notifications.alwaysShowBadgeCounts",
this.roomId,
this.countPreferenceChanged,
);
}
public componentWillUnmount(): void {
SettingsStore.unwatchSetting(this.countWatcherRef);
this.props.notification.off(NotificationStateEvents.Update, this.onNotificationUpdate);
2020-06-08 13:42:18 -06:00
}
public componentDidUpdate(prevProps: Readonly<IProps>): void {
2020-06-08 13:42:18 -06:00
if (prevProps.notification) {
prevProps.notification.off(NotificationStateEvents.Update, this.onNotificationUpdate);
2020-06-08 13:42:18 -06:00
}
this.props.notification.on(NotificationStateEvents.Update, this.onNotificationUpdate);
2020-06-08 13:42:18 -06:00
}
private countPreferenceChanged = (): void => {
2021-06-29 13:11:58 +01:00
this.setState({ showCounts: SettingsStore.getValue("Notifications.alwaysShowBadgeCounts", this.roomId) });
};
private onNotificationUpdate = (): void => {
2020-06-08 13:42:18 -06:00
this.forceUpdate(); // notification state changed - update
};
public render(): ReactNode {
/* eslint @typescript-eslint/no-unused-vars: ["error", { "ignoreRestSiblings": true }] */
const { notification, showUnsentTooltip, hideIfDot, onClick, tabIndex } = this.props;
2023-09-19 14:24:35 +03:00
if (notification.isIdle && !notification.knocked) return null;
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;
}
2020-06-08 13:42:18 -06:00
const commonProps: React.ComponentProps<typeof StatelessNotificationBadge> = {
symbol: notification.symbol,
count: notification.count,
level: notification.level,
2023-09-19 14:24:35 +03:00
knocked: notification.knocked,
};
let badge: JSX.Element;
if (onClick) {
badge = <StatelessNotificationBadge {...commonProps} onClick={onClick} tabIndex={tabIndex} />;
} else {
badge = <StatelessNotificationBadge {...commonProps} />;
}
if (showUnsentTooltip && notification.level === NotificationLevel.Unsent) {
return (
2024-04-12 14:56:23 +02:00
<Tooltip label={_t("notifications|message_didnt_send")} placement="right">
{badge}
</Tooltip>
);
}
return badge;
2020-06-08 13:42:18 -06:00
}
}