2020-06-08 07:06:41 +03:00
|
|
|
/*
|
|
|
|
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 { 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";
|
2020-06-08 17:20:15 +03:00
|
|
|
import {USER_NOTIFICATIONS_TAB, USER_SECURITY_TAB} from "../views/dialogs/UserSettingsDialog";
|
|
|
|
import { OpenToTabPayload } from "../../dispatcher/payloads/OpenToTabPayload";
|
2020-06-08 18:04:43 +03:00
|
|
|
import RedesignFeedbackDialog from "../views/dialogs/RedesignFeedbackDialog";
|
|
|
|
import Modal from "../../Modal";
|
|
|
|
import LogoutDialog from "../views/dialogs/LogoutDialog";
|
2020-06-08 18:24:08 +03:00
|
|
|
import SettingsStore, {SettingLevel} from "../../settings/SettingsStore";
|
|
|
|
import {getCustomTheme} from "../../theme";
|
2020-06-08 18:32:16 +03:00
|
|
|
import {getHostingLink} from "../../utils/HostingLink";
|
2020-06-11 00:05:29 +03:00
|
|
|
import AccessibleButton, {ButtonEvent} from "../views/elements/AccessibleButton";
|
2020-06-16 17:46:48 +03:00
|
|
|
import SdkConfig from "../../SdkConfig";
|
|
|
|
import {getHomePageUrl} from "../../utils/pages";
|
2020-06-24 05:59:26 +03:00
|
|
|
import { OwnProfileStore } from "../../stores/OwnProfileStore";
|
|
|
|
import { UPDATE_EVENT } from "../../stores/AsyncStore";
|
2020-06-26 04:38:11 +03:00
|
|
|
import BaseAvatar from '../views/avatars/BaseAvatar';
|
2020-06-26 04:54:17 +03:00
|
|
|
import classNames from "classnames";
|
2020-06-08 07:06:41 +03:00
|
|
|
|
|
|
|
interface IProps {
|
2020-06-26 04:38:11 +03:00
|
|
|
isMinimized: boolean;
|
2020-06-08 07:06:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
menuDisplayed: boolean;
|
2020-06-08 18:24:08 +03:00
|
|
|
isDarkTheme: boolean;
|
2020-06-08 07:06:41 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 04:29:12 +03:00
|
|
|
export default class UserMenu extends React.Component<IProps, IState> {
|
2020-06-08 07:06:41 +03:00
|
|
|
private dispatcherRef: string;
|
2020-06-08 18:24:08 +03:00
|
|
|
private themeWatcherRef: string;
|
2020-06-08 07:06:41 +03:00
|
|
|
private buttonRef: React.RefObject<HTMLButtonElement> = createRef();
|
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
menuDisplayed: false,
|
2020-06-08 18:24:08 +03:00
|
|
|
isDarkTheme: this.isUserOnDarkTheme(),
|
2020-06-08 07:06:41 +03:00
|
|
|
};
|
|
|
|
|
2020-06-24 05:59:26 +03:00
|
|
|
OwnProfileStore.instance.on(UPDATE_EVENT, this.onProfileUpdate);
|
2020-06-08 07:06:41 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 17:46:48 +03:00
|
|
|
private get hasHomePage(): boolean {
|
|
|
|
return !!getHomePageUrl(SdkConfig.get());
|
|
|
|
}
|
|
|
|
|
2020-06-08 07:06:41 +03:00
|
|
|
public componentDidMount() {
|
|
|
|
this.dispatcherRef = defaultDispatcher.register(this.onAction);
|
2020-06-08 18:24:08 +03:00
|
|
|
this.themeWatcherRef = SettingsStore.watchSetting("theme", null, this.onThemeChanged);
|
2020-06-08 07:06:41 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 18:24:08 +03:00
|
|
|
public componentWillUnmount() {
|
|
|
|
if (this.themeWatcherRef) SettingsStore.unwatchSetting(this.themeWatcherRef);
|
|
|
|
if (this.dispatcherRef) defaultDispatcher.unregister(this.dispatcherRef);
|
2020-06-24 05:59:26 +03:00
|
|
|
OwnProfileStore.instance.off(UPDATE_EVENT, this.onProfileUpdate);
|
2020-06-08 18:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private isUserOnDarkTheme(): boolean {
|
|
|
|
const theme = SettingsStore.getValue("theme");
|
|
|
|
if (theme.startsWith("custom-")) {
|
2020-06-10 00:23:34 +03:00
|
|
|
return getCustomTheme(theme.substring("custom-".length)).is_dark;
|
2020-06-08 18:24:08 +03:00
|
|
|
}
|
|
|
|
return theme === "dark";
|
|
|
|
}
|
|
|
|
|
2020-06-24 05:59:26 +03:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2020-06-08 18:24:08 +03:00
|
|
|
private onThemeChanged = () => {
|
|
|
|
this.setState({isDarkTheme: this.isUserOnDarkTheme()});
|
|
|
|
};
|
|
|
|
|
2020-06-08 07:06:41 +03:00
|
|
|
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});
|
|
|
|
};
|
|
|
|
|
2020-06-26 04:54:17 +03:00
|
|
|
private onCloseMenu = (ev: InputEvent) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2020-06-08 07:06:41 +03:00
|
|
|
this.setState({menuDisplayed: false});
|
|
|
|
};
|
|
|
|
|
|
|
|
private onSwitchThemeClick = () => {
|
2020-06-08 18:24:08 +03:00
|
|
|
// 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";
|
2020-06-22 22:34:49 +03:00
|
|
|
SettingsStore.setValue("theme", null, SettingLevel.DEVICE, newTheme); // set at same level as Appearance tab
|
2020-06-08 07:06:41 +03:00
|
|
|
};
|
|
|
|
|
2020-06-11 00:05:29 +03:00
|
|
|
private onSettingsOpen = (ev: ButtonEvent, tabId: string) => {
|
2020-06-08 07:06:41 +03:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 17:20:15 +03:00
|
|
|
const payload: OpenToTabPayload = {action: Action.ViewUserSettings, initialTabId: tabId};
|
|
|
|
defaultDispatcher.dispatch(payload);
|
|
|
|
this.setState({menuDisplayed: false}); // also close the menu
|
2020-06-08 07:06:41 +03:00
|
|
|
};
|
|
|
|
|
2020-06-11 00:05:29 +03:00
|
|
|
private onShowArchived = (ev: ButtonEvent) => {
|
2020-06-08 07:06:41 +03:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 18:04:43 +03:00
|
|
|
// TODO: Archived room view (deferred)
|
2020-06-08 07:06:41 +03:00
|
|
|
console.log("TODO: Show archived rooms");
|
|
|
|
};
|
|
|
|
|
2020-06-11 00:05:29 +03:00
|
|
|
private onProvideFeedback = (ev: ButtonEvent) => {
|
2020-06-08 07:06:41 +03:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 18:04:43 +03:00
|
|
|
Modal.createTrackedDialog('Report bugs & give feedback', '', RedesignFeedbackDialog);
|
|
|
|
this.setState({menuDisplayed: false}); // also close the menu
|
2020-06-08 07:06:41 +03:00
|
|
|
};
|
|
|
|
|
2020-06-11 00:05:29 +03:00
|
|
|
private onSignOutClick = (ev: ButtonEvent) => {
|
2020-06-08 07:06:41 +03:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 18:04:43 +03:00
|
|
|
Modal.createTrackedDialog('Logout from LeftPanel', '', LogoutDialog);
|
|
|
|
this.setState({menuDisplayed: false}); // also close the menu
|
2020-06-08 07:06:41 +03:00
|
|
|
};
|
|
|
|
|
2020-06-16 17:46:48 +03:00
|
|
|
private onHomeClick = (ev: ButtonEvent) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
|
|
defaultDispatcher.dispatch({action: 'view_home_page'});
|
|
|
|
};
|
|
|
|
|
2020-06-26 04:54:17 +03:00
|
|
|
private renderContextMenu = (): React.ReactNode => {
|
|
|
|
if (!this.state.menuDisplayed) return null;
|
|
|
|
|
|
|
|
let hostingLink;
|
|
|
|
const signupLink = getHostingLink("user-context-menu");
|
|
|
|
if (signupLink) {
|
|
|
|
hostingLink = (
|
|
|
|
<div className="mx_UserMenu_contextMenu_header">
|
|
|
|
{_t(
|
|
|
|
"<a>Upgrade</a> to your own domain", {},
|
|
|
|
{
|
|
|
|
a: sub => (
|
|
|
|
<a
|
|
|
|
href={signupLink}
|
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
tabIndex={-1}
|
|
|
|
>{sub}</a>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
</div>
|
2020-06-08 07:06:41 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-26 04:54:17 +03:00
|
|
|
let homeButton = null;
|
|
|
|
if (this.hasHomePage) {
|
|
|
|
homeButton = (
|
|
|
|
<li>
|
|
|
|
<AccessibleButton onClick={this.onHomeClick}>
|
|
|
|
<span className="mx_IconizedContextMenu_icon mx_UserMenu_iconHome" />
|
|
|
|
<span>{_t("Home")}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const elementRect = this.buttonRef.current.getBoundingClientRect();
|
2020-06-08 07:06:41 +03:00
|
|
|
return (
|
2020-06-26 04:54:17 +03:00
|
|
|
<ContextMenu
|
|
|
|
chevronFace="none"
|
2020-06-26 04:55:08 +03:00
|
|
|
left={elementRect.width + elementRect.left}
|
2020-06-26 04:54:17 +03:00
|
|
|
top={elementRect.top + elementRect.height}
|
|
|
|
onFinished={this.onCloseMenu}
|
|
|
|
>
|
|
|
|
<div className="mx_IconizedContextMenu mx_UserMenu_contextMenu">
|
|
|
|
<div className="mx_UserMenu_contextMenu_header">
|
|
|
|
<div className="mx_UserMenu_contextMenu_name">
|
|
|
|
<span className="mx_UserMenu_contextMenu_displayName">
|
|
|
|
{OwnProfileStore.instance.displayName}
|
|
|
|
</span>
|
|
|
|
<span className="mx_UserMenu_contextMenu_userId">
|
|
|
|
{MatrixClientPeg.get().getUserId()}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="mx_UserMenu_contextMenu_themeButton"
|
|
|
|
onClick={this.onSwitchThemeClick}
|
|
|
|
title={this.state.isDarkTheme ? _t("Switch to light mode") : _t("Switch to dark mode")}
|
|
|
|
>
|
|
|
|
<img
|
|
|
|
src={require("../../../res/img/feather-customised/sun.svg")}
|
|
|
|
alt={_t("Switch theme")}
|
|
|
|
width={16}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{hostingLink}
|
|
|
|
<div className="mx_IconizedContextMenu_optionList mx_IconizedContextMenu_optionList_notFirst">
|
|
|
|
<ul>
|
|
|
|
{homeButton}
|
|
|
|
<li>
|
|
|
|
<AccessibleButton onClick={(e) => this.onSettingsOpen(e, USER_NOTIFICATIONS_TAB)}>
|
|
|
|
<span className="mx_IconizedContextMenu_icon mx_UserMenu_iconBell" />
|
|
|
|
<span>{_t("Notification settings")}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<AccessibleButton onClick={(e) => this.onSettingsOpen(e, USER_SECURITY_TAB)}>
|
|
|
|
<span className="mx_IconizedContextMenu_icon mx_UserMenu_iconLock" />
|
|
|
|
<span>{_t("Security & privacy")}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<AccessibleButton onClick={(e) => this.onSettingsOpen(e, null)}>
|
|
|
|
<span className="mx_IconizedContextMenu_icon mx_UserMenu_iconSettings" />
|
|
|
|
<span>{_t("All settings")}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<AccessibleButton onClick={this.onShowArchived}>
|
|
|
|
<span className="mx_IconizedContextMenu_icon mx_UserMenu_iconArchive" />
|
|
|
|
<span>{_t("Archived rooms")}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<AccessibleButton onClick={this.onProvideFeedback}>
|
|
|
|
<span className="mx_IconizedContextMenu_icon mx_UserMenu_iconMessage" />
|
|
|
|
<span>{_t("Feedback")}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div className="mx_IconizedContextMenu_optionList">
|
|
|
|
<ul>
|
|
|
|
<li className="mx_UserMenu_contextMenu_redRow">
|
|
|
|
<AccessibleButton onClick={this.onSignOutClick}>
|
|
|
|
<span className="mx_IconizedContextMenu_icon mx_UserMenu_iconSignOut" />
|
|
|
|
<span>{_t("Sign out")}</span>
|
|
|
|
</AccessibleButton>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ContextMenu>
|
2020-06-18 16:32:43 +03:00
|
|
|
);
|
2020-06-26 04:54:17 +03:00
|
|
|
};
|
2020-06-26 04:38:11 +03:00
|
|
|
|
|
|
|
public render() {
|
|
|
|
const avatarSize = 32; // should match border-radius of the avatar
|
|
|
|
|
|
|
|
let name = <span className="mx_UserMenu_userName">{OwnProfileStore.instance.displayName}</span>;
|
|
|
|
let buttons = (
|
|
|
|
<span className="mx_UserMenu_headerButtons">
|
2020-06-26 04:54:17 +03:00
|
|
|
{/* masked image in CSS */}
|
2020-06-26 04:38:11 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
if (this.props.isMinimized) {
|
|
|
|
name = null;
|
|
|
|
buttons = null;
|
|
|
|
}
|
|
|
|
|
2020-06-26 04:54:17 +03:00
|
|
|
const classes = classNames({
|
|
|
|
'mx_UserMenu': true,
|
|
|
|
'mx_UserMenu_minimized': this.props.isMinimized,
|
|
|
|
});
|
|
|
|
|
2020-06-26 04:38:11 +03:00
|
|
|
return (
|
2020-06-26 04:54:17 +03:00
|
|
|
<React.Fragment>
|
|
|
|
<ContextMenuButton
|
|
|
|
className={classes}
|
|
|
|
onClick={this.onOpenMenuClick}
|
|
|
|
inputRef={this.buttonRef}
|
|
|
|
label={_t("Account settings")}
|
|
|
|
isExpanded={this.state.menuDisplayed}
|
|
|
|
>
|
|
|
|
<div className="mx_UserMenu_row">
|
|
|
|
<span className="mx_UserMenu_userAvatarContainer">
|
|
|
|
<BaseAvatar
|
|
|
|
idName={MatrixClientPeg.get().getUserId()}
|
|
|
|
name={OwnProfileStore.instance.displayName || MatrixClientPeg.get().getUserId()}
|
|
|
|
url={OwnProfileStore.instance.getHttpAvatarUrl(avatarSize)}
|
|
|
|
width={avatarSize}
|
|
|
|
height={avatarSize}
|
|
|
|
resizeMethod="crop"
|
|
|
|
className="mx_UserMenu_userAvatar"
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
{name}
|
|
|
|
{buttons}
|
|
|
|
</div>
|
|
|
|
{this.renderContextMenu()}
|
|
|
|
</ContextMenuButton>
|
|
|
|
</React.Fragment>
|
2020-06-26 04:38:11 +03:00
|
|
|
);
|
|
|
|
}
|
2020-06-08 07:06:41 +03:00
|
|
|
}
|