/* Copyright 2020 The Matrix.org Foundation C.I.C. 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 * as React from "react"; import {User} from "matrix-js-sdk/src/models/user"; import { MatrixClientPeg } from "../../MatrixClientPeg"; import defaultDispatcher from "../../dispatcher/dispatcher"; import { ActionPayload } from "../../dispatcher/payloads"; import { Action } from "../../dispatcher/actions"; import { createRef } from "react"; import { _t } from "../../languageHandler"; import {ContextMenu, ContextMenuButton} from "./ContextMenu"; interface IProps { } interface IState { user: User; menuDisplayed: boolean; } export default class UserMenuButton extends React.Component { private dispatcherRef: string; private buttonRef: React.RefObject = createRef(); constructor(props: IProps) { super(props); this.state = { menuDisplayed: false, user: MatrixClientPeg.get().getUser(MatrixClientPeg.get().getUserId()), }; } private get displayName(): string { if (MatrixClientPeg.get().isGuest()) { return _t("Guest"); } else if (this.state.user) { return this.state.user.displayName; } else { return MatrixClientPeg.get().getUserId(); } } public componentDidMount() { this.dispatcherRef = defaultDispatcher.register(this.onAction); } private onAction = (ev: ActionPayload) => { if (ev.action !== Action.ToggleUserMenu) return; // not interested // For accessibility if (this.buttonRef.current) this.buttonRef.current.click(); }; private onOpenMenuClick = (ev: InputEvent) => { ev.preventDefault(); ev.stopPropagation(); this.setState({menuDisplayed: true}); }; private onCloseMenu = () => { this.setState({menuDisplayed: false}); }; private onSwitchThemeClick = () => { console.log("TODO: Switch theme"); }; private onSettingsOpen = (ev: React.MouseEvent, tabRef: string) => { ev.preventDefault(); ev.stopPropagation(); console.log("TODO: Open settings", tabRef); }; private onShowArchived = (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); console.log("TODO: Show archived rooms"); }; private onProvideFeedback = (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); console.log("TODO: Show feedback"); }; private onSignOutClick = (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); console.log("TODO: Sign out"); }; public render() { let contextMenu; if (this.state.menuDisplayed) { const elementRect = this.buttonRef.current.getBoundingClientRect(); contextMenu = (
{this.displayName} {MatrixClientPeg.get().getUserId()}
{_t("Switch
TODO: Upgrade prompt
); } return ( ... {contextMenu} ) } }