2018-10-23 14:29:44 +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 * as ContextualMenu from './ContextualMenu';
|
2018-10-24 12:04:14 +03:00
|
|
|
import {TopLeftMenu} from '../views/context_menus/TopLeftMenu';
|
2018-10-23 16:24:44 +03:00
|
|
|
import AccessibleButton from '../views/elements/AccessibleButton';
|
|
|
|
import BaseAvatar from '../views/avatars/BaseAvatar';
|
|
|
|
import MatrixClientPeg from '../../MatrixClientPeg';
|
|
|
|
import Avatar from '../../Avatar';
|
2019-01-29 18:29:30 +03:00
|
|
|
import { _t } from '../../languageHandler';
|
2018-10-23 14:29:44 +03:00
|
|
|
|
2018-10-23 16:24:44 +03:00
|
|
|
const AVATAR_SIZE = 28;
|
|
|
|
|
|
|
|
export default class TopLeftMenuButton extends React.Component {
|
2018-10-23 14:29:44 +03:00
|
|
|
static propTypes = {
|
|
|
|
collapsed: PropTypes.bool.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
static displayName = 'TopLeftMenuButton';
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
menuDisplayed: false,
|
2018-10-23 16:24:44 +03:00
|
|
|
profileInfo: null,
|
2018-10-23 14:29:44 +03:00
|
|
|
};
|
2018-10-23 16:24:44 +03:00
|
|
|
|
|
|
|
this.onToggleMenu = this.onToggleMenu.bind(this);
|
2018-10-23 14:29:44 +03:00
|
|
|
}
|
|
|
|
|
2018-10-23 16:24:44 +03:00
|
|
|
async _getProfileInfo() {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const userId = cli.getUserId();
|
|
|
|
const profileInfo = await cli.getProfileInfo(userId);
|
|
|
|
const avatarUrl = Avatar.avatarUrlForUser(
|
|
|
|
{avatarUrl: profileInfo.avatar_url},
|
|
|
|
AVATAR_SIZE, AVATAR_SIZE, "crop");
|
2018-10-23 14:29:44 +03:00
|
|
|
|
2018-10-23 16:24:44 +03:00
|
|
|
return {
|
|
|
|
userId,
|
|
|
|
name: profileInfo.displayname,
|
|
|
|
avatarUrl,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
try {
|
|
|
|
const profileInfo = await this._getProfileInfo();
|
|
|
|
this.setState({profileInfo});
|
2018-10-24 15:36:08 +03:00
|
|
|
} catch (ex) {
|
2018-10-23 16:24:44 +03:00
|
|
|
console.log("could not fetch profile");
|
|
|
|
console.error(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:12:02 +03:00
|
|
|
_getDisplayName() {
|
2019-01-29 18:29:30 +03:00
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
2019-03-05 19:12:02 +03:00
|
|
|
return _t("Guest");
|
|
|
|
} else if (this.state.profileInfo) {
|
|
|
|
return this.state.profileInfo.name;
|
2019-01-29 18:29:30 +03:00
|
|
|
} else {
|
2019-03-05 19:12:02 +03:00
|
|
|
return MatrixClientPeg.get().getUserId();
|
2019-01-29 18:29:30 +03:00
|
|
|
}
|
2019-03-05 19:12:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const name = this._getDisplayName();
|
2018-10-23 16:33:45 +03:00
|
|
|
let nameElement;
|
|
|
|
if (!this.props.collapsed) {
|
|
|
|
nameElement = <div className="mx_TopLeftMenuButton_name">
|
|
|
|
{ name }
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2018-10-23 14:29:44 +03:00
|
|
|
return (
|
2018-10-23 16:24:44 +03:00
|
|
|
<AccessibleButton className="mx_TopLeftMenuButton" onClick={this.onToggleMenu}>
|
2018-10-23 14:29:44 +03:00
|
|
|
<BaseAvatar
|
2019-03-05 19:12:02 +03:00
|
|
|
idName={MatrixClientPeg.get().getUserId()}
|
2018-10-23 14:29:44 +03:00
|
|
|
name={name}
|
2019-03-05 19:12:02 +03:00
|
|
|
url={this.state.profileInfo && this.state.profileInfo.avatarUrl}
|
2018-10-23 16:24:44 +03:00
|
|
|
width={AVATAR_SIZE}
|
|
|
|
height={AVATAR_SIZE}
|
|
|
|
resizeMethod="crop"
|
2018-10-23 14:29:44 +03:00
|
|
|
/>
|
2018-10-23 16:33:45 +03:00
|
|
|
{ nameElement }
|
2019-01-31 00:44:10 +03:00
|
|
|
<span className="mx_TopLeftMenuButton_chevron"></span>
|
2018-10-23 16:24:44 +03:00
|
|
|
</AccessibleButton>
|
2018-10-23 14:29:44 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggleMenu(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2018-10-23 16:24:44 +03:00
|
|
|
const elementRect = e.currentTarget.getBoundingClientRect();
|
2018-10-23 14:29:44 +03:00
|
|
|
const x = elementRect.left;
|
|
|
|
const y = elementRect.top + elementRect.height;
|
|
|
|
|
|
|
|
ContextualMenu.createMenu(TopLeftMenu, {
|
|
|
|
chevronFace: "none",
|
|
|
|
left: x,
|
|
|
|
top: y,
|
2019-03-05 19:12:02 +03:00
|
|
|
userId: MatrixClientPeg.get().getUserId(),
|
|
|
|
displayName: this._getDisplayName(),
|
2018-10-23 14:29:44 +03:00
|
|
|
onFinished: () => {
|
|
|
|
this.setState({ menuDisplayed: false });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.setState({ menuDisplayed: true });
|
|
|
|
}
|
|
|
|
}
|