2016-01-13 15:55:28 +00:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2016-01-13 15:55:28 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
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.
|
2016-01-13 15:55:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2017-05-30 16:09:57 +02:00
|
|
|
import React from "react";
|
2022-03-14 10:22:12 +00:00
|
|
|
import { UnstableValue } from "matrix-js-sdk/src/NamespacedValue";
|
2024-07-18 15:24:44 +01:00
|
|
|
import classNames from "classnames";
|
2017-05-30 16:09:57 +02:00
|
|
|
|
|
|
|
|
import { _t } from "../../../languageHandler";
|
2023-09-04 19:41:39 +01:00
|
|
|
import { formatDuration } from "../../../DateUtils";
|
2016-01-13 15:55:28 +00:00
|
|
|
|
2024-08-06 15:51:25 +01:00
|
|
|
export const BUSY_PRESENCE_NAME = new UnstableValue("busy", "org.matrix.msc3026.busy");
|
2022-03-14 10:22:12 +00:00
|
|
|
|
2021-06-23 19:18:36 +02:00
|
|
|
interface IProps {
|
|
|
|
|
// number of milliseconds ago this user was last active.
|
|
|
|
|
// zero = unknown
|
2021-06-24 14:36:22 +02:00
|
|
|
activeAgo?: number;
|
2021-06-23 19:18:36 +02:00
|
|
|
// if true, activeAgo is an approximation and "Now" should
|
|
|
|
|
// be shown instead
|
2021-06-24 14:36:22 +02:00
|
|
|
currentlyActive?: boolean;
|
2021-06-23 19:18:36 +02:00
|
|
|
// offline, online, etc
|
2021-06-24 14:36:22 +02:00
|
|
|
presenceState?: string;
|
2024-07-18 15:24:44 +01:00
|
|
|
// whether to apply colouring to the label
|
|
|
|
|
coloured?: boolean;
|
|
|
|
|
className?: string;
|
2021-06-23 19:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class PresenceLabel extends React.Component<IProps> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public static defaultProps = {
|
2020-08-29 12:14:16 +01:00
|
|
|
activeAgo: -1,
|
|
|
|
|
};
|
2016-01-13 15:55:28 +00:00
|
|
|
|
2023-03-10 14:55:06 +00:00
|
|
|
private getPrettyPresence(presence?: string, activeAgo?: number, currentlyActive?: boolean): string {
|
2022-03-14 10:22:12 +00:00
|
|
|
// for busy presence, we ignore the 'currentlyActive' flag: they're busy whether
|
|
|
|
|
// they're active or not. It can be set while the user is active in which case
|
|
|
|
|
// the 'active ago' ends up being 0.
|
2023-09-05 10:44:41 +01:00
|
|
|
if (presence && BUSY_PRESENCE_NAME.matches(presence)) return _t("presence|busy");
|
2022-03-14 10:22:12 +00:00
|
|
|
|
2023-11-07 15:44:30 +05:30
|
|
|
if (presence === "io.element.unreachable") return _t("presence|unreachable");
|
|
|
|
|
|
2017-11-16 18:36:54 +01:00
|
|
|
if (!currentlyActive && activeAgo !== undefined && activeAgo > 0) {
|
2023-09-04 19:41:39 +01:00
|
|
|
const duration = formatDuration(activeAgo);
|
2023-09-05 10:44:41 +01:00
|
|
|
if (presence === "online") return _t("presence|online_for", { duration: duration });
|
|
|
|
|
if (presence === "unavailable") return _t("presence|idle_for", { duration: duration }); // XXX: is this actually right?
|
|
|
|
|
if (presence === "offline") return _t("presence|offline_for", { duration: duration });
|
|
|
|
|
return _t("presence|unknown_for", { duration: duration });
|
2017-11-16 18:36:54 +01:00
|
|
|
} else {
|
2023-09-05 10:44:41 +01:00
|
|
|
if (presence === "online") return _t("presence|online");
|
|
|
|
|
if (presence === "unavailable") return _t("presence|idle"); // XXX: is this actually right?
|
|
|
|
|
if (presence === "offline") return _t("presence|offline");
|
|
|
|
|
return _t("presence|unknown");
|
2017-11-16 18:36:54 +01:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2016-01-13 15:55:28 +00:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2017-11-16 18:36:54 +01:00
|
|
|
return (
|
2024-07-18 15:24:44 +01:00
|
|
|
<div
|
|
|
|
|
className={classNames("mx_PresenceLabel", this.props.className, {
|
|
|
|
|
mx_PresenceLabel_online: this.props.coloured && this.props.presenceState === "online",
|
|
|
|
|
})}
|
|
|
|
|
>
|
2017-11-16 18:36:54 +01:00
|
|
|
{this.getPrettyPresence(this.props.presenceState, this.props.activeAgo, this.props.currentlyActive)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|