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