/* 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 {createRef} from "react"; import {MatrixClientPeg} from "../../MatrixClientPeg"; import defaultDispatcher from "../../dispatcher/dispatcher"; import {ActionPayload} from "../../dispatcher/payloads"; import {Action} from "../../dispatcher/actions"; import {_t} from "../../languageHandler"; import {ChevronFace, ContextMenu, ContextMenuButton} from "./ContextMenu"; import {USER_NOTIFICATIONS_TAB, USER_SECURITY_TAB} from "../views/dialogs/UserSettingsDialog"; import {OpenToTabPayload} from "../../dispatcher/payloads/OpenToTabPayload"; import RedesignFeedbackDialog from "../views/dialogs/RedesignFeedbackDialog"; import Modal from "../../Modal"; import LogoutDialog from "../views/dialogs/LogoutDialog"; import SettingsStore, {SettingLevel} from "../../settings/SettingsStore"; import {getCustomTheme} from "../../theme"; import {getHostingLink} from "../../utils/HostingLink"; import AccessibleButton, {ButtonEvent} from "../views/elements/AccessibleButton"; import SdkConfig from "../../SdkConfig"; import {getHomePageUrl} from "../../utils/pages"; import {OwnProfileStore} from "../../stores/OwnProfileStore"; import {UPDATE_EVENT} from "../../stores/AsyncStore"; import BaseAvatar from '../views/avatars/BaseAvatar'; import classNames from "classnames"; import AccessibleTooltipButton from "../views/elements/AccessibleTooltipButton"; interface IProps { isMinimized: boolean; } type PartialDOMRect = Pick; interface IState { contextMenuPosition: PartialDOMRect; isDarkTheme: boolean; } export default class UserMenu extends React.Component { private dispatcherRef: string; private themeWatcherRef: string; private buttonRef: React.RefObject = createRef(); constructor(props: IProps) { super(props); this.state = { contextMenuPosition: null, isDarkTheme: this.isUserOnDarkTheme(), }; OwnProfileStore.instance.on(UPDATE_EVENT, this.onProfileUpdate); } private get hasHomePage(): boolean { return !!getHomePageUrl(SdkConfig.get()); } public componentDidMount() { this.dispatcherRef = defaultDispatcher.register(this.onAction); this.themeWatcherRef = SettingsStore.watchSetting("theme", null, this.onThemeChanged); } public componentWillUnmount() { if (this.themeWatcherRef) SettingsStore.unwatchSetting(this.themeWatcherRef); if (this.dispatcherRef) defaultDispatcher.unregister(this.dispatcherRef); OwnProfileStore.instance.off(UPDATE_EVENT, this.onProfileUpdate); } private isUserOnDarkTheme(): boolean { const theme = SettingsStore.getValue("theme"); if (theme.startsWith("custom-")) { return getCustomTheme(theme.substring("custom-".length)).is_dark; } return theme === "dark"; } private onProfileUpdate = async () => { // the store triggered an update, so force a layout update. We don't // have any state to store here for that to magically happen. this.forceUpdate(); }; private onThemeChanged = () => { this.setState({isDarkTheme: this.isUserOnDarkTheme()}); }; 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: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); const target = ev.target as HTMLButtonElement; this.setState({contextMenuPosition: target.getBoundingClientRect()}); }; private onContextMenu = (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); this.setState({ contextMenuPosition: { left: ev.clientX, top: ev.clientY, width: 20, height: 0, }, }); }; private onCloseMenu = () => { this.setState({contextMenuPosition: null}); }; private onSwitchThemeClick = () => { // Disable system theme matching if the user hits this button SettingsStore.setValue("use_system_theme", null, SettingLevel.DEVICE, false); const newTheme = this.state.isDarkTheme ? "light" : "dark"; SettingsStore.setValue("theme", null, SettingLevel.DEVICE, newTheme); // set at same level as Appearance tab }; private onSettingsOpen = (ev: ButtonEvent, tabId: string) => { ev.preventDefault(); ev.stopPropagation(); const payload: OpenToTabPayload = {action: Action.ViewUserSettings, initialTabId: tabId}; defaultDispatcher.dispatch(payload); this.setState({contextMenuPosition: null}); // also close the menu }; private onShowArchived = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); // TODO: Archived room view: https://github.com/vector-im/riot-web/issues/14038 console.log("TODO: Show archived rooms"); }; private onProvideFeedback = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); Modal.createTrackedDialog('Report bugs & give feedback', '', RedesignFeedbackDialog); this.setState({contextMenuPosition: null}); // also close the menu }; private onSignOutClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); Modal.createTrackedDialog('Logout from LeftPanel', '', LogoutDialog); this.setState({contextMenuPosition: null}); // also close the menu }; private onHomeClick = (ev: ButtonEvent) => { ev.preventDefault(); ev.stopPropagation(); defaultDispatcher.dispatch({action: 'view_home_page'}); }; private renderContextMenu = (): React.ReactNode => { if (!this.state.contextMenuPosition) return null; let hostingLink; const signupLink = getHostingLink("user-context-menu"); if (signupLink) { hostingLink = (
{_t( "Upgrade to your own domain", {}, { a: sub => ( {sub} ), }, )}
); } let homeButton = null; if (this.hasHomePage) { homeButton = ( {_t("Home")} ); } return (
{OwnProfileStore.instance.displayName} {MatrixClientPeg.get().getUserId()}
{_t("Switch
{hostingLink}
{homeButton} this.onSettingsOpen(e, USER_NOTIFICATIONS_TAB)}> {_t("Notification settings")} this.onSettingsOpen(e, USER_SECURITY_TAB)}> {_t("Security & privacy")} this.onSettingsOpen(e, null)}> {_t("All settings")} {_t("Archived rooms")} {_t("Feedback")}
{_t("Sign out")}
); }; public render() { const avatarSize = 32; // should match border-radius of the avatar let name = {OwnProfileStore.instance.displayName}; let buttons = ( {/* masked image in CSS */} ); if (this.props.isMinimized) { name = null; buttons = null; } const classes = classNames({ 'mx_UserMenu': true, 'mx_UserMenu_minimized': this.props.isMinimized, }); return (
{name} {buttons}
{this.renderContextMenu()}
); } }