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

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

69 lines
2.7 KiB
TypeScript
Raw Normal View History

/*
Copyright 2015, 2016 OpenMarket Ltd
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.
*/
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";
2017-05-30 16:09:57 +02:00
import { _t } from "../../../languageHandler";
import { formatDuration } from "../../../DateUtils";
2022-03-14 10:22:12 +00:00
const BUSY_PRESENCE_NAME = new UnstableValue("busy", "org.matrix.msc3026.busy");
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;
2021-06-23 19:18:36 +02:00
}
export default class PresenceLabel extends React.Component<IProps> {
public static defaultProps = {
2020-08-29 12:14:16 +01:00
activeAgo: -1,
};
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.
if (presence && BUSY_PRESENCE_NAME.matches(presence)) return _t("presence|busy");
2022-03-14 10:22:12 +00:00
2017-11-16 18:36:54 +01:00
if (!currentlyActive && activeAgo !== undefined && activeAgo > 0) {
const duration = formatDuration(activeAgo);
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 {
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
}
public render(): React.ReactNode {
2017-11-16 18:36:54 +01:00
return (
<div className="mx_PresenceLabel">
{this.getPrettyPresence(this.props.presenceState, this.props.activeAgo, this.props.currentlyActive)}
</div>
);
2020-08-29 12:14:16 +01:00
}
}