2016-01-13 18:55:28 +03:00
|
|
|
/*
|
|
|
|
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 17:09:57 +03:00
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-09-06 17:04:46 +03:00
|
|
|
import createReactClass from 'create-react-class';
|
2017-05-30 17:09:57 +03:00
|
|
|
|
|
|
|
import { _t } from '../../../languageHandler';
|
2016-01-13 18:55:28 +03:00
|
|
|
|
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
export default createReactClass({
|
2016-01-13 18:55:28 +03:00
|
|
|
displayName: 'PresenceLabel',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-03-17 03:06:53 +03:00
|
|
|
// number of milliseconds ago this user was last active.
|
|
|
|
// zero = unknown
|
2017-12-26 04:03:18 +03:00
|
|
|
activeAgo: PropTypes.number,
|
2016-03-17 03:06:53 +03:00
|
|
|
|
|
|
|
// if true, activeAgo is an approximation and "Now" should
|
|
|
|
// be shown instead
|
2017-12-26 04:03:18 +03:00
|
|
|
currentlyActive: PropTypes.bool,
|
2016-03-17 03:06:53 +03:00
|
|
|
|
|
|
|
// offline, online, etc
|
2017-12-26 04:03:18 +03:00
|
|
|
presenceState: PropTypes.string,
|
2016-01-13 18:55:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
ago: -1,
|
2017-10-11 19:56:17 +03:00
|
|
|
presenceState: null,
|
2016-01-13 18:55:28 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-11-22 20:13:18 +03:00
|
|
|
// Return duration as a string using appropriate time units
|
2017-11-16 20:36:54 +03:00
|
|
|
// XXX: This would be better handled using a culture-aware library, but we don't use one yet.
|
2016-01-13 18:55:28 +03:00
|
|
|
getDuration: function(time) {
|
|
|
|
if (!time) return;
|
2017-10-11 19:56:17 +03:00
|
|
|
const t = parseInt(time / 1000);
|
|
|
|
const s = t % 60;
|
|
|
|
const m = parseInt(t / 60) % 60;
|
|
|
|
const h = parseInt(t / (60 * 60)) % 24;
|
|
|
|
const d = parseInt(t / (60 * 60 * 24));
|
2016-01-13 18:55:28 +03:00
|
|
|
if (t < 60) {
|
|
|
|
if (t < 0) {
|
2017-11-16 20:36:54 +03:00
|
|
|
return _t("%(duration)ss", {duration: 0});
|
2016-01-13 18:55:28 +03:00
|
|
|
}
|
2017-11-16 20:36:54 +03:00
|
|
|
return _t("%(duration)ss", {duration: s});
|
2016-01-13 18:55:28 +03:00
|
|
|
}
|
|
|
|
if (t < 60 * 60) {
|
2017-11-16 20:36:54 +03:00
|
|
|
return _t("%(duration)sm", {duration: m});
|
2016-01-13 18:55:28 +03:00
|
|
|
}
|
|
|
|
if (t < 24 * 60 * 60) {
|
2017-11-16 20:36:54 +03:00
|
|
|
return _t("%(duration)sh", {duration: h});
|
2016-01-13 18:55:28 +03:00
|
|
|
}
|
2017-11-16 20:36:54 +03:00
|
|
|
return _t("%(duration)sd", {duration: d});
|
2016-01-13 18:55:28 +03:00
|
|
|
},
|
|
|
|
|
2017-11-16 20:36:54 +03:00
|
|
|
getPrettyPresence: function(presence, activeAgo, currentlyActive) {
|
|
|
|
if (!currentlyActive && activeAgo !== undefined && activeAgo > 0) {
|
|
|
|
const duration = this.getDuration(activeAgo);
|
|
|
|
if (presence === "online") return _t("Online for %(duration)s", { duration: duration });
|
|
|
|
if (presence === "unavailable") return _t("Idle for %(duration)s", { duration: duration }); // XXX: is this actually right?
|
|
|
|
if (presence === "offline") return _t("Offline for %(duration)s", { duration: duration });
|
|
|
|
return _t("Unknown for %(duration)s", { duration: duration });
|
|
|
|
} else {
|
|
|
|
if (presence === "online") return _t("Online");
|
|
|
|
if (presence === "unavailable") return _t("Idle"); // XXX: is this actually right?
|
|
|
|
if (presence === "offline") return _t("Offline");
|
|
|
|
return _t("Unknown");
|
|
|
|
}
|
2016-01-13 18:55:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2017-11-16 20:36:54 +03:00
|
|
|
return (
|
|
|
|
<div className="mx_PresenceLabel">
|
|
|
|
{ this.getPrettyPresence(this.props.presenceState, this.props.activeAgo, this.props.currentlyActive) }
|
|
|
|
</div>
|
|
|
|
);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-01-13 18:55:28 +03:00
|
|
|
});
|