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.
|
|
|
|
*/
|
|
|
|
|
2019-11-28 19:40:45 +03:00
|
|
|
import React, {createRef} from 'react';
|
2018-12-13 04:03:30 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../../MatrixClientPeg';
|
2019-11-28 21:16:59 +03:00
|
|
|
import {_t} from "../../../languageHandler";
|
2018-12-13 04:03:30 +03:00
|
|
|
import MemberAvatar from '../avatars/MemberAvatar';
|
|
|
|
import classNames from 'classnames';
|
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";
|
2019-12-03 02:21:59 +03:00
|
|
|
import {ContextMenu, ContextMenuButton} from "../../structures/ContextMenu";
|
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',
|
|
|
|
};
|
|
|
|
|
2019-12-17 20:26:12 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-01-15 01:59:15 +03:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
hasStatus: this.hasStatus,
|
2019-11-28 19:40:45 +03:00
|
|
|
menuDisplayed: false,
|
2019-01-15 01:59:15 +03:00
|
|
|
};
|
2019-11-28 19:40:45 +03:00
|
|
|
|
|
|
|
this._button = createRef();
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 23:14:17 +03:00
|
|
|
componentDidMount() {
|
2018-12-13 04:03:30 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-17 00:52:33 +03:00
|
|
|
componentWillUnmount() {
|
2019-01-15 01:59:15 +03:00
|
|
|
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
|
|
|
|
2019-11-28 19:40:45 +03:00
|
|
|
openMenu = () => {
|
|
|
|
this.setState({ menuDisplayed: true });
|
|
|
|
};
|
2018-12-13 04:03:30 +03:00
|
|
|
|
2019-11-28 19:40:45 +03:00
|
|
|
closeMenu = () => {
|
|
|
|
this.setState({ menuDisplayed: false });
|
2018-12-18 20:53:37 +03:00
|
|
|
};
|
2018-12-13 04:03:30 +03:00
|
|
|
|
|
|
|
render() {
|
2019-01-15 19:20:27 +03:00
|
|
|
const avatar = <MemberAvatar
|
|
|
|
member={this.props.member}
|
|
|
|
width={this.props.width}
|
|
|
|
height={this.props.height}
|
|
|
|
resizeMethod={this.props.resizeMethod}
|
|
|
|
/>;
|
2019-01-14 19:26:11 +03:00
|
|
|
|
2019-01-15 19:20:27 +03:00
|
|
|
if (!SettingsStore.isFeatureEnabled("feature_custom_status")) {
|
|
|
|
return avatar;
|
2019-01-14 19:26:11 +03:00
|
|
|
}
|
2018-12-13 04:03:30 +03:00
|
|
|
|
|
|
|
const classes = classNames({
|
|
|
|
"mx_MemberStatusMessageAvatar": true,
|
2019-01-15 19:20:27 +03:00
|
|
|
"mx_MemberStatusMessageAvatar_hasStatus": this.state.hasStatus,
|
2018-12-13 04:03:30 +03:00
|
|
|
});
|
|
|
|
|
2019-11-28 19:40:45 +03:00
|
|
|
let contextMenu;
|
|
|
|
if (this.state.menuDisplayed) {
|
|
|
|
const elementRect = this._button.current.getBoundingClientRect();
|
|
|
|
|
|
|
|
const chevronWidth = 16; // See .mx_ContextualMenu_chevron_bottom
|
|
|
|
const chevronMargin = 1; // Add some spacing away from target
|
2019-11-28 21:16:59 +03:00
|
|
|
|
|
|
|
contextMenu = (
|
|
|
|
<ContextMenu
|
|
|
|
chevronOffset={(elementRect.width - chevronWidth) / 2}
|
|
|
|
chevronFace="bottom"
|
|
|
|
left={elementRect.left + window.pageXOffset}
|
|
|
|
top={elementRect.top + window.pageYOffset - chevronMargin}
|
|
|
|
menuWidth={226}
|
|
|
|
onFinished={this.closeMenu}
|
|
|
|
>
|
|
|
|
<StatusMessageContextMenu user={this.props.member.user} onFinished={this.closeMenu} />
|
|
|
|
</ContextMenu>
|
|
|
|
);
|
2019-11-28 19:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return <React.Fragment>
|
2019-11-28 21:16:59 +03:00
|
|
|
<ContextMenuButton
|
|
|
|
className={classes}
|
|
|
|
inputRef={this._button}
|
|
|
|
onClick={this.openMenu}
|
|
|
|
isExpanded={this.state.menuDisplayed}
|
|
|
|
label={_t("User Status")}
|
|
|
|
>
|
2019-11-28 19:40:45 +03:00
|
|
|
{avatar}
|
2019-11-28 21:16:59 +03:00
|
|
|
</ContextMenuButton>
|
2019-11-28 19:40:45 +03:00
|
|
|
|
|
|
|
{ contextMenu }
|
|
|
|
</React.Fragment>;
|
2018-12-13 04:03:30 +03:00
|
|
|
}
|
|
|
|
}
|