element-web/src/components/views/rooms/EntityTile.js

201 lines
6.9 KiB
JavaScript
Raw Normal View History

/*
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2018 New Vector 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';
const React = require('react');
import PropTypes from 'prop-types';
const sdk = require('../../../index');
2017-01-25 01:41:52 +03:00
import AccessibleButton from '../elements/AccessibleButton';
import { _t } from '../../../languageHandler';
2019-03-08 00:55:17 +03:00
import classNames from "classnames";
const PRESENCE_CLASS = {
2016-01-18 19:47:31 +03:00
"offline": "mx_EntityTile_offline",
"online": "mx_EntityTile_online",
"unavailable": "mx_EntityTile_unavailable",
};
function presenceClassForMember(presenceState, lastActiveAgo, showPresence) {
if (showPresence === false) {
return 'mx_EntityTile_online_beenactive';
}
// offline is split into two categories depending on whether we have
// a last_active_ago for them.
2016-07-14 20:18:44 +03:00
if (presenceState == 'offline') {
if (lastActiveAgo) {
return PRESENCE_CLASS['offline'] + '_beenactive';
} else {
return PRESENCE_CLASS['offline'] + '_neveractive';
}
2016-07-14 20:18:44 +03:00
} else if (presenceState) {
return PRESENCE_CLASS[presenceState];
} else {
2016-07-14 20:13:54 +03:00
return PRESENCE_CLASS['offline'] + '_neveractive';
}
}
const EntityTile = React.createClass({
displayName: 'EntityTile',
propTypes: {
name: PropTypes.string,
title: PropTypes.string,
avatarJsx: PropTypes.any, // <BaseAvatar />
className: PropTypes.string,
presenceState: PropTypes.string,
presenceLastActiveAgo: PropTypes.number,
presenceLastTs: PropTypes.number,
presenceCurrentlyActive: PropTypes.bool,
showInviteButton: PropTypes.bool,
shouldComponentUpdate: PropTypes.func,
onClick: PropTypes.func,
suppressOnHover: PropTypes.bool,
showPresence: PropTypes.bool,
subtextLabel: PropTypes.string,
},
getDefaultProps: function() {
return {
shouldComponentUpdate: function(nextProps, nextState) { return true; },
onClick: function() {},
presenceState: "offline",
presenceLastActiveAgo: 0,
presenceLastTs: 0,
showInviteButton: false,
suppressOnHover: false,
showPresence: true,
};
},
getInitialState: function() {
return {
hover: false,
};
},
shouldComponentUpdate: function(nextProps, nextState) {
if (this.state.hover !== nextState.hover) return true;
return this.props.shouldComponentUpdate(nextProps, nextState);
},
render: function() {
2019-03-08 00:55:17 +03:00
const mainClassNames = {
"mx_EntityTile": true,
"mx_EntityTile_noHover": this.props.suppressOnHover,
};
if (this.props.className) mainClassNames[this.props.className] = true;
2016-07-14 20:13:15 +03:00
const presenceClass = presenceClassForMember(
this.props.presenceState, this.props.presenceLastActiveAgo, this.props.showPresence,
);
2019-03-08 00:55:17 +03:00
mainClassNames[presenceClass] = true;
let nameEl;
const {name} = this.props;
const EmojiText = sdk.getComponent('elements.EmojiText');
2019-03-08 00:55:17 +03:00
if (!this.props.suppressOnHover) {
const activeAgo = this.props.presenceLastActiveAgo ?
(Date.now() - (this.props.presenceLastTs - this.props.presenceLastActiveAgo)) : -1;
const PresenceLabel = sdk.getComponent("rooms.PresenceLabel");
let presenceLabel = null;
if (this.props.showPresence) {
presenceLabel = <PresenceLabel activeAgo={activeAgo}
2018-12-13 04:18:43 +03:00
currentlyActive={this.props.presenceCurrentlyActive}
presenceState={this.props.presenceState} />;
}
if (this.props.subtextLabel) {
presenceLabel = <span className="mx_EntityTile_subtext">{this.props.subtextLabel}</span>;
}
nameEl = (
2016-01-18 19:47:31 +03:00
<div className="mx_EntityTile_details">
2019-03-08 00:55:17 +03:00
<EmojiText element="div" className="mx_EntityTile_name" dir="auto">
2018-12-13 04:18:43 +03:00
{ name }
</EmojiText>
{presenceLabel}
</div>
);
} else if (this.props.subtextLabel) {
nameEl = (
<div className="mx_EntityTile_details">
<EmojiText element="div" className="mx_EntityTile_name" dir="auto">
{name}
</EmojiText>
<span className="mx_EntityTile_subtext">{this.props.subtextLabel}</span>
</div>
);
} else {
nameEl = (
<EmojiText element="div" className="mx_EntityTile_name" dir="auto">{ name }</EmojiText>
);
}
let inviteButton;
2016-01-18 19:47:31 +03:00
if (this.props.showInviteButton) {
inviteButton = (
<div className="mx_EntityTile_invite">
<img src={require("../../../../res/img/plus.svg")} width="16" height="16" />
2016-01-18 19:47:31 +03:00
</div>
);
}
let power;
const powerStatus = this.props.powerStatus;
if (powerStatus) {
const src = {
[EntityTile.POWER_STATUS_MODERATOR]: require("../../../../res/img/mod.svg"),
[EntityTile.POWER_STATUS_ADMIN]: require("../../../../res/img/admin.svg"),
}[powerStatus];
const alt = {
[EntityTile.POWER_STATUS_MODERATOR]: _t("Moderator"),
[EntityTile.POWER_STATUS_ADMIN]: _t("Admin"),
}[powerStatus];
power = <img src={src} className="mx_EntityTile_power" width="16" height="17" alt={alt} />;
}
2016-01-18 19:47:31 +03:00
const BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
const av = this.props.avatarJsx || <BaseAvatar name={this.props.name} width={36} height={36} />;
// The wrapping div is required to make the magic mouse listener work, for some reason.
return (
<div ref={(c) => this.container = c} >
2019-03-08 00:55:17 +03:00
<AccessibleButton className={classNames(mainClassNames)} title={this.props.title}
onClick={this.props.onClick}>
<div className="mx_EntityTile_avatar">
{ av }
{ power }
</div>
{ nameEl }
{ inviteButton }
</AccessibleButton>
</div>
);
},
});
EntityTile.POWER_STATUS_MODERATOR = "moderator";
EntityTile.POWER_STATUS_ADMIN = "admin";
export default EntityTile;