2020-06-08 22:42:18 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 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 React from "react";
|
|
|
|
import classNames from "classnames";
|
|
|
|
import { formatMinimalBadgeCount } from "../../../utils/FormattingUtils";
|
2020-06-20 00:44:37 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2020-07-02 21:48:06 +03:00
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
import { XOR } from "../../../@types/common";
|
2020-07-09 04:26:25 +03:00
|
|
|
import { NOTIFICATION_STATE_UPDATE, NotificationState } from "../../../stores/notifications/NotificationState";
|
2020-06-08 22:42:18 +03:00
|
|
|
|
|
|
|
interface IProps {
|
2020-07-09 04:26:25 +03:00
|
|
|
notification: NotificationState;
|
2020-06-08 22:42:18 +03:00
|
|
|
|
|
|
|
/**
|
2020-06-20 00:44:37 +03:00
|
|
|
* If true, the badge will show a count if at all possible. This is typically
|
|
|
|
* used to override the user's preference for things like room sublists.
|
2020-06-08 22:42:18 +03:00
|
|
|
*/
|
2020-06-20 00:44:37 +03:00
|
|
|
forceCount: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The room ID, if any, the badge represents.
|
|
|
|
*/
|
|
|
|
roomId?: string;
|
2020-06-08 22:42:18 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 21:48:06 +03:00
|
|
|
interface IClickableProps extends IProps, React.InputHTMLAttributes<Element> {
|
|
|
|
/**
|
|
|
|
* If specified will return an AccessibleButton instead of a div.
|
|
|
|
*/
|
|
|
|
onClick?(ev: React.MouseEvent);
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:42:18 +03:00
|
|
|
interface IState {
|
2020-06-20 00:44:37 +03:00
|
|
|
showCounts: boolean; // whether or not to show counts. Independent of props.forceCount
|
2020-06-08 22:42:18 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 21:48:06 +03:00
|
|
|
export default class NotificationBadge extends React.PureComponent<XOR<IProps, IClickableProps>, IState> {
|
2020-06-20 00:44:37 +03:00
|
|
|
private countWatcherRef: string;
|
|
|
|
|
2020-06-08 22:42:18 +03:00
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
this.props.notification.on(NOTIFICATION_STATE_UPDATE, this.onNotificationUpdate);
|
2020-06-20 00:44:37 +03:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
showCounts: SettingsStore.getValue("Notifications.alwaysShowBadgeCounts", this.roomId),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.countWatcherRef = SettingsStore.watchSetting(
|
|
|
|
"Notifications.alwaysShowBadgeCounts", this.roomId,
|
|
|
|
this.countPreferenceChanged,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private get roomId(): string {
|
|
|
|
// We should convert this to null for safety with the SettingsStore
|
|
|
|
return this.props.roomId || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
SettingsStore.unwatchSetting(this.countWatcherRef);
|
2020-07-24 23:07:37 +03:00
|
|
|
this.props.notification.off(NOTIFICATION_STATE_UPDATE, this.onNotificationUpdate);
|
2020-06-08 22:42:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidUpdate(prevProps: Readonly<IProps>) {
|
|
|
|
if (prevProps.notification) {
|
|
|
|
prevProps.notification.off(NOTIFICATION_STATE_UPDATE, this.onNotificationUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.notification.on(NOTIFICATION_STATE_UPDATE, this.onNotificationUpdate);
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:44:37 +03:00
|
|
|
private countPreferenceChanged = () => {
|
|
|
|
this.setState({showCounts: SettingsStore.getValue("Notifications.alwaysShowBadgeCounts", this.roomId)});
|
|
|
|
};
|
|
|
|
|
2020-06-08 22:42:18 +03:00
|
|
|
private onNotificationUpdate = () => {
|
|
|
|
this.forceUpdate(); // notification state changed - update
|
|
|
|
};
|
|
|
|
|
|
|
|
public render(): React.ReactElement {
|
2020-07-02 21:48:06 +03:00
|
|
|
const {notification, forceCount, roomId, onClick, ...props} = this.props;
|
|
|
|
|
2020-06-08 22:42:18 +03:00
|
|
|
// Don't show a badge if we don't need to
|
2020-07-09 04:26:25 +03:00
|
|
|
if (notification.isIdle) return null;
|
2020-06-08 22:42:18 +03:00
|
|
|
|
2020-06-30 22:45:10 +03:00
|
|
|
// TODO: Update these booleans for FTUE Notifications: https://github.com/vector-im/riot-web/issues/14261
|
|
|
|
// As of writing, that is "if red, show count always" and "optionally show counts instead of dots".
|
|
|
|
// See git diff for what that boolean state looks like.
|
|
|
|
// XXX: We ignore this.state.showCounts (the setting which controls counts vs dots).
|
2020-07-02 22:26:16 +03:00
|
|
|
const hasAnySymbol = notification.symbol || notification.count > 0;
|
2020-07-09 04:26:25 +03:00
|
|
|
let isEmptyBadge = !hasAnySymbol || !notification.hasUnreadCount;
|
2020-07-02 21:48:06 +03:00
|
|
|
if (forceCount) {
|
2020-06-20 00:44:37 +03:00
|
|
|
isEmptyBadge = false;
|
2020-07-09 04:26:25 +03:00
|
|
|
if (!notification.hasUnreadCount) return null; // Can't render a badge
|
2020-06-20 00:44:37 +03:00
|
|
|
}
|
2020-06-08 22:42:18 +03:00
|
|
|
|
2020-07-02 21:48:06 +03:00
|
|
|
let symbol = notification.symbol || formatMinimalBadgeCount(notification.count);
|
2020-06-08 22:42:18 +03:00
|
|
|
if (isEmptyBadge) symbol = "";
|
|
|
|
|
|
|
|
const classes = classNames({
|
|
|
|
'mx_NotificationBadge': true,
|
2020-07-09 04:26:25 +03:00
|
|
|
'mx_NotificationBadge_visible': isEmptyBadge ? true : notification.hasUnreadCount,
|
|
|
|
'mx_NotificationBadge_highlighted': notification.hasMentions,
|
2020-06-08 22:42:18 +03:00
|
|
|
'mx_NotificationBadge_dot': isEmptyBadge,
|
|
|
|
'mx_NotificationBadge_2char': symbol.length > 0 && symbol.length < 3,
|
|
|
|
'mx_NotificationBadge_3char': symbol.length > 2,
|
|
|
|
});
|
|
|
|
|
2020-07-02 21:48:06 +03:00
|
|
|
if (onClick) {
|
|
|
|
return (
|
|
|
|
<AccessibleButton {...props} className={classes} onClick={onClick}>
|
|
|
|
<span className="mx_NotificationBadge_count">{symbol}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:42:18 +03:00
|
|
|
return (
|
|
|
|
<div className={classes}>
|
|
|
|
<span className="mx_NotificationBadge_count">{symbol}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|