2016-01-13 15:55:28 +00: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 14:09:57 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { _t } from '../../../languageHandler';
|
2016-01-13 15:55:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'PresenceLabel',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-03-17 00:06:53 +00:00
|
|
|
// number of milliseconds ago this user was last active.
|
|
|
|
// zero = unknown
|
2016-01-13 15:55:28 +00:00
|
|
|
activeAgo: React.PropTypes.number,
|
2016-03-17 00:06:53 +00:00
|
|
|
|
|
|
|
// if true, activeAgo is an approximation and "Now" should
|
|
|
|
// be shown instead
|
|
|
|
currentlyActive: React.PropTypes.bool,
|
|
|
|
|
|
|
|
// offline, online, etc
|
2016-01-13 15:55:28 +00:00
|
|
|
presenceState: React.PropTypes.string
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
ago: -1,
|
|
|
|
presenceState: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getDuration: function(time) {
|
|
|
|
if (!time) return;
|
|
|
|
var t = parseInt(time / 1000);
|
|
|
|
var s = t % 60;
|
|
|
|
var m = parseInt(t / 60) % 60;
|
|
|
|
var h = parseInt(t / (60 * 60)) % 24;
|
|
|
|
var d = parseInt(t / (60 * 60 * 24));
|
|
|
|
if (t < 60) {
|
|
|
|
if (t < 0) {
|
2017-05-31 17:55:25 +00:00
|
|
|
return _t("for %(amount)ss", {amount: 0});
|
2016-01-13 15:55:28 +00:00
|
|
|
}
|
2017-05-31 17:55:25 +00:00
|
|
|
return _t("for %(amount)ss", {amount: s});
|
2016-01-13 15:55:28 +00:00
|
|
|
}
|
|
|
|
if (t < 60 * 60) {
|
2017-05-31 17:55:25 +00:00
|
|
|
return _t("for %(amount)sm", {amount: m});
|
2016-01-13 15:55:28 +00:00
|
|
|
}
|
|
|
|
if (t < 24 * 60 * 60) {
|
2017-05-31 17:55:25 +00:00
|
|
|
return _t("for %(amount)sh", {amount: h});
|
2016-01-13 15:55:28 +00:00
|
|
|
}
|
2017-05-31 17:55:25 +00:00
|
|
|
return _t("for %(amount)sd", {amount: d});
|
2016-01-13 15:55:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getPrettyPresence: function(presence) {
|
2017-05-30 14:09:57 +00:00
|
|
|
if (presence === "online") return _t("Online");
|
|
|
|
if (presence === "unavailable") return _t("Idle"); // XXX: is this actually right?
|
|
|
|
if (presence === "offline") return _t("Offline");
|
2017-09-24 08:14:04 +00:00
|
|
|
return _t("Unknown");
|
2016-01-13 15:55:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
if (this.props.activeAgo >= 0) {
|
2017-05-31 17:55:25 +00:00
|
|
|
let duration = this.getDuration(this.props.activeAgo);
|
|
|
|
let ago = this.props.currentlyActive || !duration ? "" : duration;
|
2016-01-13 15:55:28 +00:00
|
|
|
return (
|
|
|
|
<div className="mx_PresenceLabel">
|
2016-03-16 22:44:54 +00:00
|
|
|
{ this.getPrettyPresence(this.props.presenceState) } { ago }
|
2016-01-13 15:55:28 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (
|
|
|
|
<div className="mx_PresenceLabel">
|
|
|
|
{ this.getPrettyPresence(this.props.presenceState) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|