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';
|
|
|
|
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';
|
2019-05-18 00:28:12 +03:00
|
|
|
import dis from "../../dispatcher";
|
2019-05-21 06:07:55 +03:00
|
|
|
import {focusCapturedRef} from "../../utils/Accessibility";
|
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,
|
2019-05-18 00:28:12 +03:00
|
|
|
menuFunctions: null, // should be { close: fn }
|
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() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-23 14:29:44 +03:00
|
|
|
return (
|
2019-05-18 00:28:12 +03:00
|
|
|
<AccessibleButton
|
|
|
|
className="mx_TopLeftMenuButton"
|
|
|
|
role="button"
|
|
|
|
onClick={this.onToggleMenu}
|
|
|
|
inputRef={(r) => this._buttonRef = r}
|
|
|
|
aria-label={_t("Your profile")}
|
|
|
|
>
|
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 }
|
2018-10-23 16:24:44 +03:00
|
|
|
</AccessibleButton>
|
2018-10-23 14:29:44 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggleMenu(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-05-18 00:28:12 +03:00
|
|
|
if (this.state.menuDisplayed && this.state.menuFunctions) {
|
|
|
|
this.state.menuFunctions.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2019-05-18 00:28:12 +03:00
|
|
|
const menuFunctions = ContextualMenu.createMenu(TopLeftMenu, {
|
2018-10-23 14:29:44 +03:00
|
|
|
chevronFace: "none",
|
|
|
|
left: x,
|
|
|
|
top: y,
|
2019-03-05 19:12:02 +03:00
|
|
|
userId: MatrixClientPeg.get().getUserId(),
|
|
|
|
displayName: this._getDisplayName(),
|
2019-05-21 06:07:55 +03:00
|
|
|
containerRef: focusCapturedRef, // Focus the TopLeftMenu on first render
|
2018-10-23 14:29:44 +03:00
|
|
|
onFinished: () => {
|
2019-05-18 00:28:12 +03:00
|
|
|
this.setState({ menuDisplayed: false, menuFunctions: null });
|
2018-10-23 14:29:44 +03:00
|
|
|
},
|
|
|
|
});
|
2019-05-18 00:28:12 +03:00
|
|
|
this.setState({ menuDisplayed: true, menuFunctions });
|
2018-10-23 14:29:44 +03:00
|
|
|
}
|
|
|
|
}
|