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 {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";
|
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 07:06:41 +03:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
user: User;
|
|
|
|
menuDisplayed: boolean;
|
2020-06-08 18:24:08 +03:00
|
|
|
isDarkTheme: boolean;
|
2020-06-08 07:06:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class UserMenuButton extends React.Component<IProps, IState> {
|
|
|
|
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,
|
|
|
|
user: MatrixClientPeg.get().getUser(MatrixClientPeg.get().getUserId()),
|
2020-06-08 18:24:08 +03:00
|
|
|
isDarkTheme: this.isUserOnDarkTheme(),
|
2020-06-08 07:06:41 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private isUserOnDarkTheme(): boolean {
|
|
|
|
const theme = SettingsStore.getValue("theme");
|
|
|
|
if (theme.startsWith("custom-")) {
|
|
|
|
return getCustomTheme(theme.substring(0, 7)).is_dark;
|
|
|
|
}
|
|
|
|
return theme === "dark";
|
|
|
|
}
|
|
|
|
|
|
|
|
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});
|
|
|
|
};
|
|
|
|
|
|
|
|
private onCloseMenu = () => {
|
|
|
|
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";
|
|
|
|
SettingsStore.setValue("theme", null, SettingLevel.ACCOUNT, newTheme);
|
2020-06-08 07:06:41 +03:00
|
|
|
};
|
|
|
|
|
2020-06-08 17:20:15 +03:00
|
|
|
private onSettingsOpen = (ev: React.MouseEvent, 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
|
|
|
};
|
|
|
|
|
|
|
|
private onShowArchived = (ev: React.MouseEvent) => {
|
|
|
|
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");
|
|
|
|
};
|
|
|
|
|
|
|
|
private onProvideFeedback = (ev: React.MouseEvent) => {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
private onSignOutClick = (ev: React.MouseEvent) => {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
let contextMenu;
|
|
|
|
if (this.state.menuDisplayed) {
|
|
|
|
const elementRect = this.buttonRef.current.getBoundingClientRect();
|
|
|
|
contextMenu = (
|
|
|
|
<ContextMenu
|
|
|
|
chevronFace="none"
|
|
|
|
left={elementRect.left}
|
|
|
|
top={elementRect.top + elementRect.height}
|
|
|
|
onFinished={this.onCloseMenu}
|
|
|
|
>
|
|
|
|
<div className="mx_UserMenuButton_contextMenu">
|
|
|
|
<div className="mx_UserMenuButton_contextMenu_header">
|
|
|
|
<div className="mx_UserMenuButton_contextMenu_name">
|
|
|
|
<span className="mx_UserMenuButton_contextMenu_displayName">
|
|
|
|
{this.displayName}
|
|
|
|
</span>
|
|
|
|
<span className="mx_UserMenuButton_contextMenu_userId">
|
|
|
|
{MatrixClientPeg.get().getUserId()}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="mx_UserMenuButton_contextMenu_themeButton"
|
|
|
|
onClick={this.onSwitchThemeClick}
|
2020-06-08 18:24:08 +03:00
|
|
|
title={this.state.isDarkTheme ? _t("Switch to light mode") : _t("Switch to dark mode")}
|
2020-06-08 07:06:41 +03:00
|
|
|
>
|
|
|
|
<img
|
|
|
|
src={require("../../../res/img/feather-customised/sun.svg")}
|
|
|
|
alt={_t("Switch theme")}
|
|
|
|
width={16}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mx_UserMenuButton_contextMenu_header">
|
|
|
|
TODO: Upgrade prompt
|
|
|
|
</div>
|
|
|
|
<div className="mx_UserMenuButton_contextMenu_optionList">
|
|
|
|
<ul>
|
|
|
|
<li>
|
2020-06-08 17:20:15 +03:00
|
|
|
<a href={"#"} onClick={(e) => this.onSettingsOpen(e, USER_NOTIFICATIONS_TAB)}>
|
2020-06-08 07:06:41 +03:00
|
|
|
<img src={require("../../../res/img/feather-customised/notifications.svg")} width={16} />
|
|
|
|
<span>{_t("Notification settings")}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
2020-06-08 17:20:15 +03:00
|
|
|
<a href={"#"} onClick={(e) => this.onSettingsOpen(e, USER_SECURITY_TAB)}>
|
2020-06-08 07:06:41 +03:00
|
|
|
<img src={require("../../../res/img/feather-customised/lock.svg")} width={16} />
|
|
|
|
<span>{_t("Security & privacy")}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
2020-06-08 17:20:15 +03:00
|
|
|
<a href={"#"} onClick={(e) => this.onSettingsOpen(e, null)}>
|
2020-06-08 07:06:41 +03:00
|
|
|
<img src={require("../../../res/img/feather-customised/settings.svg")} width={16} />
|
|
|
|
<span>{_t("All settings")}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href={"#"} onClick={this.onShowArchived}>
|
|
|
|
<img src={require("../../../res/img/feather-customised/archive.svg")} width={16} />
|
|
|
|
<span>{_t("Archived rooms")}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
2020-06-08 07:17:02 +03:00
|
|
|
<a href={"#"} onClick={this.onProvideFeedback}>
|
2020-06-08 07:06:41 +03:00
|
|
|
<img src={require("../../../res/img/feather-customised/message-circle.svg")} width={16} />
|
|
|
|
<span>{_t("Feedback")}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div className="mx_UserMenuButton_contextMenu_optionList">
|
|
|
|
<ul>
|
|
|
|
<li>
|
2020-06-08 07:17:02 +03:00
|
|
|
<a href={"#"} onClick={this.onSignOutClick}>
|
2020-06-08 07:06:41 +03:00
|
|
|
<img src={require("../../../res/img/feather-customised/sign-out.svg")} width={16} />
|
|
|
|
<span>{_t("Sign out")}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ContextMenu>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<ContextMenuButton
|
|
|
|
className="mx_UserMenuButton"
|
|
|
|
onClick={this.onOpenMenuClick}
|
|
|
|
inputRef={this.buttonRef}
|
|
|
|
label={_t("Account settings")}
|
|
|
|
isExpanded={this.state.menuDisplayed}
|
|
|
|
>
|
|
|
|
<img src={require("../../../res/img/feather-customised/more-horizontal.svg")} alt="..." width={14} />
|
|
|
|
</ContextMenuButton>
|
|
|
|
{contextMenu}
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|