2018-12-13 04:03:30 +03:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
|
|
|
import MemberAvatar from '../avatars/MemberAvatar';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import * as ContextualMenu from "../../structures/ContextualMenu";
|
2018-12-13 08:26:39 +03:00
|
|
|
import StatusMessageContextMenu from "../context_menus/StatusMessageContextMenu";
|
2018-12-19 01:11:08 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2018-12-13 04:03:30 +03:00
|
|
|
|
2018-12-19 01:11:08 +03:00
|
|
|
export default class MemberStatusMessageAvatar extends React.Component {
|
2018-12-18 20:53:37 +03:00
|
|
|
static propTypes = {
|
|
|
|
member: PropTypes.object.isRequired,
|
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
|
|
|
resizeMethod: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
|
|
|
resizeMethod: 'crop',
|
|
|
|
};
|
|
|
|
|
2018-12-13 04:03:30 +03:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
2019-01-15 01:59:15 +03:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
hasStatus: this.hasStatus,
|
|
|
|
};
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
if (this.props.member.userId !== MatrixClientPeg.get().getUserId()) {
|
|
|
|
throw new Error("Cannot use MemberStatusMessageAvatar on anyone but the logged in user");
|
|
|
|
}
|
2019-01-15 01:59:15 +03:00
|
|
|
if (!SettingsStore.isFeatureEnabled("feature_custom_status")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { user } = this.props.member;
|
|
|
|
if (!user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
user.on("User._unstable_statusMessage", this._onStatusMessageCommitted);
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 01:59:15 +03:00
|
|
|
componentWillUmount() {
|
|
|
|
const { user } = this.props.member;
|
|
|
|
if (!user) {
|
|
|
|
return;
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
2019-01-15 01:59:15 +03:00
|
|
|
user.removeListener(
|
|
|
|
"User._unstable_statusMessage",
|
|
|
|
this._onStatusMessageCommitted,
|
|
|
|
);
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 01:59:15 +03:00
|
|
|
get hasStatus() {
|
|
|
|
const { user } = this.props.member;
|
|
|
|
if (!user) {
|
|
|
|
return false;
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
2019-01-15 01:59:15 +03:00
|
|
|
return !!user._unstable_statusMessage;
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 01:59:15 +03:00
|
|
|
_onStatusMessageCommitted = () => {
|
|
|
|
// The `User` object has observed a status message change.
|
|
|
|
this.setState({
|
|
|
|
hasStatus: this.hasStatus,
|
|
|
|
});
|
2018-12-18 20:53:37 +03:00
|
|
|
};
|
2018-12-13 04:03:30 +03:00
|
|
|
|
2018-12-18 20:53:37 +03:00
|
|
|
_onClick = (e) => {
|
2018-12-13 04:03:30 +03:00
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const elementRect = e.target.getBoundingClientRect();
|
|
|
|
|
2019-01-14 21:47:50 +03:00
|
|
|
const x = (elementRect.left + window.pageXOffset);
|
|
|
|
const chevronWidth = 16; // See .mx_ContextualMenu_chevron_bottom
|
|
|
|
const chevronOffset = (elementRect.width - chevronWidth) / 2;
|
|
|
|
const chevronMargin = 1; // Add some spacing away from target
|
|
|
|
const y = elementRect.top + window.pageYOffset - chevronMargin;
|
2018-12-13 04:03:30 +03:00
|
|
|
|
2018-12-13 08:26:39 +03:00
|
|
|
ContextualMenu.createMenu(StatusMessageContextMenu, {
|
2018-12-13 04:03:30 +03:00
|
|
|
chevronOffset: chevronOffset,
|
|
|
|
chevronFace: 'bottom',
|
|
|
|
left: x,
|
|
|
|
top: y,
|
2019-01-14 21:47:50 +03:00
|
|
|
menuWidth: 226,
|
2018-12-13 08:26:39 +03:00
|
|
|
user: this.props.member.user,
|
2018-12-13 04:03:30 +03:00
|
|
|
});
|
2018-12-18 20:53:37 +03:00
|
|
|
};
|
2018-12-13 04:03:30 +03:00
|
|
|
|
|
|
|
render() {
|
2019-01-14 19:26:11 +03:00
|
|
|
const customStatusFeatureEnabled =
|
|
|
|
SettingsStore.isFeatureEnabled("feature_custom_status");
|
|
|
|
|
|
|
|
let onClick = null;
|
|
|
|
let hasStatus = false;
|
2018-12-19 01:11:08 +03:00
|
|
|
|
2019-01-14 19:26:11 +03:00
|
|
|
if (customStatusFeatureEnabled) {
|
|
|
|
onClick = this._onClick;
|
2019-01-15 01:59:15 +03:00
|
|
|
hasStatus = this.state.hasStatus;
|
2019-01-14 19:26:11 +03:00
|
|
|
}
|
2018-12-13 04:03:30 +03:00
|
|
|
|
|
|
|
const classes = classNames({
|
|
|
|
"mx_MemberStatusMessageAvatar": true,
|
|
|
|
"mx_MemberStatusMessageAvatar_hasStatus": hasStatus,
|
|
|
|
});
|
|
|
|
|
2019-01-14 19:26:11 +03:00
|
|
|
return <AccessibleButton onClick={onClick} className={classes} element="div">
|
2018-12-13 04:03:30 +03:00
|
|
|
<MemberAvatar member={this.props.member}
|
|
|
|
width={this.props.width}
|
|
|
|
height={this.props.height}
|
2018-12-13 08:26:39 +03:00
|
|
|
resizeMethod={this.props.resizeMethod} />
|
2018-12-13 04:03:30 +03:00
|
|
|
</AccessibleButton>;
|
|
|
|
}
|
|
|
|
}
|