2018-10-23 14:29:44 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
2019-05-21 06:07:55 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2018-10-23 14:29:44 +03:00
|
|
|
|
|
|
|
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';
|
2018-10-24 12:04:14 +03:00
|
|
|
import {TopLeftMenu} from '../views/context_menus/TopLeftMenu';
|
2018-10-23 16:24:44 +03:00
|
|
|
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';
|
2019-05-18 00:28:12 +03:00
|
|
|
import dis from "../../dispatcher";
|
2019-12-03 02:21:59 +03:00
|
|
|
import {ContextMenu, ContextMenuButton} from "./ContextMenu";
|
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
|
|
|
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() {
|
2019-05-18 00:28:12 +03:00
|
|
|
this._dispatcherRef = dis.register(this.onAction);
|
|
|
|
|
2018-10-23 16:24:44 +03:00
|
|
|
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-05-18 00:28:12 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
dis.unregister(this._dispatcherRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
onAction = (payload) => {
|
|
|
|
// For accessibility
|
|
|
|
if (payload.action === "toggle_top_left_menu") {
|
|
|
|
if (this._buttonRef) this._buttonRef.click();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-11 20:53:17 +03:00
|
|
|
openMenu = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.setState({ menuDisplayed: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
closeMenu = () => {
|
|
|
|
this.setState({
|
|
|
|
menuDisplayed: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-03-05 19:12:02 +03:00
|
|
|
render() {
|
2019-11-11 20:53:17 +03:00
|
|
|
const cli = MatrixClientPeg.get().getUserId();
|
|
|
|
|
2019-03-05 19:12:02 +03:00
|
|
|
const name = this._getDisplayName();
|
2018-10-23 16:33:45 +03:00
|
|
|
let nameElement;
|
2019-08-05 19:03:20 +03:00
|
|
|
let chevronElement;
|
2018-10-23 16:33:45 +03:00
|
|
|
if (!this.props.collapsed) {
|
|
|
|
nameElement = <div className="mx_TopLeftMenuButton_name">
|
|
|
|
{ name }
|
|
|
|
</div>;
|
2019-08-05 19:03:20 +03:00
|
|
|
chevronElement = <span className="mx_TopLeftMenuButton_chevron" />;
|
2018-10-23 16:33:45 +03:00
|
|
|
}
|
|
|
|
|
2019-11-11 20:53:17 +03:00
|
|
|
let contextMenu;
|
|
|
|
if (this.state.menuDisplayed) {
|
|
|
|
const elementRect = this._buttonRef.getBoundingClientRect();
|
2019-11-28 21:16:59 +03:00
|
|
|
|
|
|
|
contextMenu = (
|
|
|
|
<ContextMenu
|
|
|
|
chevronFace="none"
|
|
|
|
left={elementRect.left}
|
|
|
|
top={elementRect.top + elementRect.height}
|
|
|
|
onFinished={this.closeMenu}
|
|
|
|
>
|
|
|
|
<TopLeftMenu displayName={name} userId={cli} onFinished={this.closeMenu} />
|
|
|
|
</ContextMenu>
|
|
|
|
);
|
2019-11-11 20:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return <React.Fragment>
|
2019-11-28 21:16:59 +03:00
|
|
|
<ContextMenuButton
|
2019-05-18 00:28:12 +03:00
|
|
|
className="mx_TopLeftMenuButton"
|
2019-11-11 20:53:17 +03:00
|
|
|
onClick={this.openMenu}
|
2019-05-18 00:28:12 +03:00
|
|
|
inputRef={(r) => this._buttonRef = r}
|
2019-11-28 21:16:59 +03:00
|
|
|
label={_t("Your profile")}
|
|
|
|
isExpanded={this.state.menuDisplayed}
|
2019-05-18 00:28:12 +03:00
|
|
|
>
|
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-08-05 19:03:20 +03:00
|
|
|
{ chevronElement }
|
2019-11-28 21:16:59 +03:00
|
|
|
</ContextMenuButton>
|
2018-10-23 14:29:44 +03:00
|
|
|
|
2019-11-11 20:53:17 +03:00
|
|
|
{ contextMenu }
|
|
|
|
</React.Fragment>;
|
2018-10-23 14:29:44 +03:00
|
|
|
}
|
|
|
|
}
|