2020-06-08 04:06:41 +00: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.
|
|
|
|
*/
|
|
|
|
|
2020-07-07 14:01:27 +00:00
|
|
|
import React, { createRef } from "react";
|
2020-06-08 04:06:41 +00:00
|
|
|
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
|
|
|
import defaultDispatcher from "../../dispatcher/dispatcher";
|
|
|
|
import { ActionPayload } from "../../dispatcher/payloads";
|
|
|
|
import { Action } from "../../dispatcher/actions";
|
|
|
|
import { _t } from "../../languageHandler";
|
2020-08-04 20:42:39 +00:00
|
|
|
import { ContextMenuButton } from "./ContextMenu";
|
2020-06-08 14:20:15 +00:00
|
|
|
import {USER_NOTIFICATIONS_TAB, USER_SECURITY_TAB} from "../views/dialogs/UserSettingsDialog";
|
|
|
|
import { OpenToTabPayload } from "../../dispatcher/payloads/OpenToTabPayload";
|
2020-06-08 15:04:43 +00:00
|
|
|
import RedesignFeedbackDialog from "../views/dialogs/RedesignFeedbackDialog";
|
|
|
|
import Modal from "../../Modal";
|
|
|
|
import LogoutDialog from "../views/dialogs/LogoutDialog";
|
2020-07-28 17:53:43 +00:00
|
|
|
import SettingsStore from "../../settings/SettingsStore";
|
2020-06-08 15:24:08 +00:00
|
|
|
import {getCustomTheme} from "../../theme";
|
2020-06-08 15:32:16 +00:00
|
|
|
import {getHostingLink} from "../../utils/HostingLink";
|
2020-07-03 13:34:43 +00:00
|
|
|
import {ButtonEvent} from "../views/elements/AccessibleButton";
|
2020-06-16 14:46:48 +00:00
|
|
|
import SdkConfig from "../../SdkConfig";
|
|
|
|
import {getHomePageUrl} from "../../utils/pages";
|
2020-06-24 02:59:26 +00:00
|
|
|
import { OwnProfileStore } from "../../stores/OwnProfileStore";
|
|
|
|
import { UPDATE_EVENT } from "../../stores/AsyncStore";
|
2020-06-26 01:38:11 +00:00
|
|
|
import BaseAvatar from '../views/avatars/BaseAvatar';
|
2020-06-26 01:54:17 +00:00
|
|
|
import classNames from "classnames";
|
2020-07-01 00:50:31 +00:00
|
|
|
import AccessibleTooltipButton from "../views/elements/AccessibleTooltipButton";
|
2020-07-28 17:53:43 +00:00
|
|
|
import { SettingLevel } from "../../settings/SettingLevel";
|
2020-08-04 20:42:39 +00:00
|
|
|
import IconizedContextMenu, {
|
|
|
|
IconizedContextMenuOption,
|
|
|
|
IconizedContextMenuOptionList
|
|
|
|
} from "../views/context_menus/IconizedContextMenu";
|
2020-06-08 04:06:41 +00:00
|
|
|
|
|
|
|
interface IProps {
|
2020-06-26 01:38:11 +00:00
|
|
|
isMinimized: boolean;
|
2020-06-08 04:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 22:06:26 +00:00
|
|
|
type PartialDOMRect = Pick<DOMRect, "width" | "left" | "top" | "height">;
|
|
|
|
|
2020-06-08 04:06:41 +00:00
|
|
|
interface IState {
|
2020-07-01 22:06:26 +00:00
|
|
|
contextMenuPosition: PartialDOMRect;
|
2020-06-08 15:24:08 +00:00
|
|
|
isDarkTheme: boolean;
|
2020-06-08 04:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 01:29:12 +00:00
|
|
|
export default class UserMenu extends React.Component<IProps, IState> {
|
2020-06-08 04:06:41 +00:00
|
|
|
private dispatcherRef: string;
|
2020-06-08 15:24:08 +00:00
|
|
|
private themeWatcherRef: string;
|
2020-06-08 04:06:41 +00:00
|
|
|
private buttonRef: React.RefObject<HTMLButtonElement> = createRef();
|
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2020-07-01 22:06:26 +00:00
|
|
|
contextMenuPosition: null,
|
2020-06-08 15:24:08 +00:00
|
|
|
isDarkTheme: this.isUserOnDarkTheme(),
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-06-24 02:59:26 +00:00
|
|
|
OwnProfileStore.instance.on(UPDATE_EVENT, this.onProfileUpdate);
|
2020-06-08 04:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 14:46:48 +00:00
|
|
|
private get hasHomePage(): boolean {
|
|
|
|
return !!getHomePageUrl(SdkConfig.get());
|
|
|
|
}
|
|
|
|
|
2020-06-08 04:06:41 +00:00
|
|
|
public componentDidMount() {
|
|
|
|
this.dispatcherRef = defaultDispatcher.register(this.onAction);
|
2020-06-08 15:24:08 +00:00
|
|
|
this.themeWatcherRef = SettingsStore.watchSetting("theme", null, this.onThemeChanged);
|
2020-06-08 04:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 15:24:08 +00:00
|
|
|
public componentWillUnmount() {
|
|
|
|
if (this.themeWatcherRef) SettingsStore.unwatchSetting(this.themeWatcherRef);
|
|
|
|
if (this.dispatcherRef) defaultDispatcher.unregister(this.dispatcherRef);
|
2020-06-24 02:59:26 +00:00
|
|
|
OwnProfileStore.instance.off(UPDATE_EVENT, this.onProfileUpdate);
|
2020-06-08 15:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private isUserOnDarkTheme(): boolean {
|
|
|
|
const theme = SettingsStore.getValue("theme");
|
|
|
|
if (theme.startsWith("custom-")) {
|
2020-06-09 21:23:34 +00:00
|
|
|
return getCustomTheme(theme.substring("custom-".length)).is_dark;
|
2020-06-08 15:24:08 +00:00
|
|
|
}
|
2020-07-14 16:40:32 +00:00
|
|
|
return theme === "dark";
|
2020-06-08 15:24:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 02:59:26 +00: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 15:24:08 +00:00
|
|
|
private onThemeChanged = () => {
|
|
|
|
this.setState({isDarkTheme: this.isUserOnDarkTheme()});
|
|
|
|
};
|
|
|
|
|
2020-06-08 04:06:41 +00:00
|
|
|
private onAction = (ev: ActionPayload) => {
|
|
|
|
if (ev.action !== Action.ToggleUserMenu) return; // not interested
|
|
|
|
|
2020-07-03 13:34:43 +00:00
|
|
|
if (this.state.contextMenuPosition) {
|
|
|
|
this.setState({contextMenuPosition: null});
|
|
|
|
} else {
|
|
|
|
if (this.buttonRef.current) this.buttonRef.current.click();
|
|
|
|
}
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 22:56:57 +00:00
|
|
|
private onOpenMenuClick = (ev: React.MouseEvent) => {
|
2020-06-08 04:06:41 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2020-07-01 22:06:26 +00:00
|
|
|
const target = ev.target as HTMLButtonElement;
|
|
|
|
this.setState({contextMenuPosition: target.getBoundingClientRect()});
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 22:06:26 +00:00
|
|
|
private onContextMenu = (ev: React.MouseEvent) => {
|
2020-06-26 01:54:17 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2020-07-01 22:06:26 +00:00
|
|
|
this.setState({
|
|
|
|
contextMenuPosition: {
|
|
|
|
left: ev.clientX,
|
|
|
|
top: ev.clientY,
|
|
|
|
width: 20,
|
|
|
|
height: 0,
|
|
|
|
},
|
|
|
|
});
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 22:13:54 +00:00
|
|
|
private onCloseMenu = () => {
|
2020-07-01 22:06:26 +00:00
|
|
|
this.setState({contextMenuPosition: null});
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-07-03 15:29:48 +00:00
|
|
|
private onSwitchThemeClick = (ev: React.MouseEvent) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 15:24:08 +00:00
|
|
|
// Disable system theme matching if the user hits this button
|
|
|
|
SettingsStore.setValue("use_system_theme", null, SettingLevel.DEVICE, false);
|
|
|
|
|
2020-07-14 16:40:32 +00:00
|
|
|
const newTheme = this.state.isDarkTheme ? "light" : "dark";
|
2020-06-22 19:34:49 +00:00
|
|
|
SettingsStore.setValue("theme", null, SettingLevel.DEVICE, newTheme); // set at same level as Appearance tab
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-06-10 21:05:29 +00:00
|
|
|
private onSettingsOpen = (ev: ButtonEvent, tabId: string) => {
|
2020-06-08 04:06:41 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 14:20:15 +00:00
|
|
|
const payload: OpenToTabPayload = {action: Action.ViewUserSettings, initialTabId: tabId};
|
|
|
|
defaultDispatcher.dispatch(payload);
|
2020-07-01 22:06:26 +00:00
|
|
|
this.setState({contextMenuPosition: null}); // also close the menu
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-06-10 21:05:29 +00:00
|
|
|
private onShowArchived = (ev: ButtonEvent) => {
|
2020-06-08 04:06:41 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-08-03 15:02:26 +00:00
|
|
|
// TODO: Archived room view: https://github.com/vector-im/element-web/issues/14038
|
2020-07-14 02:25:02 +00:00
|
|
|
// Note: You'll need to uncomment the button too.
|
2020-06-08 04:06:41 +00:00
|
|
|
console.log("TODO: Show archived rooms");
|
|
|
|
};
|
|
|
|
|
2020-06-10 21:05:29 +00:00
|
|
|
private onProvideFeedback = (ev: ButtonEvent) => {
|
2020-06-08 04:06:41 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 15:04:43 +00:00
|
|
|
Modal.createTrackedDialog('Report bugs & give feedback', '', RedesignFeedbackDialog);
|
2020-07-01 22:06:26 +00:00
|
|
|
this.setState({contextMenuPosition: null}); // also close the menu
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-06-10 21:05:29 +00:00
|
|
|
private onSignOutClick = (ev: ButtonEvent) => {
|
2020-06-08 04:06:41 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-06-08 15:04:43 +00:00
|
|
|
Modal.createTrackedDialog('Logout from LeftPanel', '', LogoutDialog);
|
2020-07-01 22:06:26 +00:00
|
|
|
this.setState({contextMenuPosition: null}); // also close the menu
|
2020-06-08 04:06:41 +00:00
|
|
|
};
|
|
|
|
|
2020-06-16 14:46:48 +00:00
|
|
|
private onHomeClick = (ev: ButtonEvent) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
|
|
defaultDispatcher.dispatch({action: 'view_home_page'});
|
|
|
|
};
|
|
|
|
|
2020-06-26 01:54:17 +00:00
|
|
|
private renderContextMenu = (): React.ReactNode => {
|
2020-07-01 22:06:26 +00:00
|
|
|
if (!this.state.contextMenuPosition) return null;
|
2020-06-26 01:54:17 +00:00
|
|
|
|
|
|
|
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 04:06:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:54:17 +00:00
|
|
|
let homeButton = null;
|
|
|
|
if (this.hasHomePage) {
|
|
|
|
homeButton = (
|
2020-08-04 20:42:39 +00:00
|
|
|
<IconizedContextMenuOption
|
2020-07-03 13:34:43 +00:00
|
|
|
iconClassName="mx_UserMenu_iconHome"
|
|
|
|
label={_t("Home")}
|
|
|
|
onClick={this.onHomeClick}
|
|
|
|
/>
|
2020-06-26 01:54:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-04 20:42:39 +00:00
|
|
|
return <IconizedContextMenu
|
|
|
|
// -20 to overlap the context menu by just over the width of the `...` icon and make it look connected
|
|
|
|
left={this.state.contextMenuPosition.width + this.state.contextMenuPosition.left - 20}
|
|
|
|
top={this.state.contextMenuPosition.top + this.state.contextMenuPosition.height}
|
|
|
|
onFinished={this.onCloseMenu}
|
|
|
|
className="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>
|
2020-06-26 01:54:17 +00:00
|
|
|
</div>
|
2020-08-04 20:42:39 +00:00
|
|
|
<AccessibleTooltipButton
|
|
|
|
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/element-icons/roomlist/dark-light-mode.svg")}
|
|
|
|
alt={_t("Switch theme")}
|
|
|
|
width={16}
|
|
|
|
/>
|
|
|
|
</AccessibleTooltipButton>
|
|
|
|
</div>
|
|
|
|
{hostingLink}
|
|
|
|
<IconizedContextMenuOptionList>
|
|
|
|
{homeButton}
|
|
|
|
<IconizedContextMenuOption
|
|
|
|
iconClassName="mx_UserMenu_iconBell"
|
|
|
|
label={_t("Notification settings")}
|
|
|
|
onClick={(e) => this.onSettingsOpen(e, USER_NOTIFICATIONS_TAB)}
|
|
|
|
/>
|
|
|
|
<IconizedContextMenuOption
|
|
|
|
iconClassName="mx_UserMenu_iconLock"
|
|
|
|
label={_t("Security & privacy")}
|
|
|
|
onClick={(e) => this.onSettingsOpen(e, USER_SECURITY_TAB)}
|
|
|
|
/>
|
|
|
|
<IconizedContextMenuOption
|
|
|
|
iconClassName="mx_UserMenu_iconSettings"
|
|
|
|
label={_t("All settings")}
|
|
|
|
onClick={(e) => this.onSettingsOpen(e, null)}
|
|
|
|
/>
|
|
|
|
{/* <IconizedContextMenuOption
|
2020-08-08 10:16:28 +00:00
|
|
|
iconClassName="mx_UserMenu_iconArchive"
|
|
|
|
label={_t("Archived rooms")}
|
|
|
|
onClick={this.onShowArchived}
|
|
|
|
/> */}
|
2020-08-04 20:42:39 +00:00
|
|
|
<IconizedContextMenuOption
|
|
|
|
iconClassName="mx_UserMenu_iconMessage"
|
|
|
|
label={_t("Feedback")}
|
|
|
|
onClick={this.onProvideFeedback}
|
|
|
|
/>
|
|
|
|
</IconizedContextMenuOptionList>
|
|
|
|
<IconizedContextMenuOptionList red>
|
|
|
|
<IconizedContextMenuOption
|
|
|
|
iconClassName="mx_UserMenu_iconSignOut"
|
|
|
|
label={_t("Sign out")}
|
|
|
|
onClick={this.onSignOutClick}
|
|
|
|
/>
|
|
|
|
</IconizedContextMenuOptionList>
|
|
|
|
</IconizedContextMenu>;
|
2020-06-26 01:54:17 +00:00
|
|
|
};
|
2020-06-26 01:38:11 +00:00
|
|
|
|
|
|
|
public render() {
|
|
|
|
const avatarSize = 32; // should match border-radius of the avatar
|
|
|
|
|
2020-08-25 20:23:18 +00:00
|
|
|
const displayName = OwnProfileStore.instance.displayName || MatrixClientPeg.get().getUserId();
|
|
|
|
const avatarUrl = OwnProfileStore.instance.getHttpAvatarUrl(avatarSize);
|
2020-08-21 01:45:54 +00:00
|
|
|
|
|
|
|
let name = <span className="mx_UserMenu_userName">{displayName}</span>;
|
2020-06-26 01:38:11 +00:00
|
|
|
let buttons = (
|
|
|
|
<span className="mx_UserMenu_headerButtons">
|
2020-06-26 01:54:17 +00:00
|
|
|
{/* masked image in CSS */}
|
2020-06-26 01:38:11 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
if (this.props.isMinimized) {
|
|
|
|
name = null;
|
|
|
|
buttons = null;
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:54:17 +00:00
|
|
|
const classes = classNames({
|
|
|
|
'mx_UserMenu': true,
|
|
|
|
'mx_UserMenu_minimized': this.props.isMinimized,
|
|
|
|
});
|
|
|
|
|
2020-06-26 01:38:11 +00:00
|
|
|
return (
|
2020-06-26 01:54:17 +00:00
|
|
|
<React.Fragment>
|
|
|
|
<ContextMenuButton
|
|
|
|
className={classes}
|
|
|
|
onClick={this.onOpenMenuClick}
|
|
|
|
inputRef={this.buttonRef}
|
2020-07-06 00:01:40 +00:00
|
|
|
label={_t("User menu")}
|
2020-07-01 22:06:26 +00:00
|
|
|
isExpanded={!!this.state.contextMenuPosition}
|
|
|
|
onContextMenu={this.onContextMenu}
|
2020-06-26 01:54:17 +00:00
|
|
|
>
|
|
|
|
<div className="mx_UserMenu_row">
|
|
|
|
<span className="mx_UserMenu_userAvatarContainer">
|
|
|
|
<BaseAvatar
|
2020-08-21 01:45:54 +00:00
|
|
|
idName={displayName}
|
|
|
|
name={displayName}
|
|
|
|
url={avatarUrl}
|
2020-06-26 01:54:17 +00:00
|
|
|
width={avatarSize}
|
|
|
|
height={avatarSize}
|
|
|
|
resizeMethod="crop"
|
|
|
|
className="mx_UserMenu_userAvatar"
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
{name}
|
|
|
|
{buttons}
|
|
|
|
</div>
|
|
|
|
</ContextMenuButton>
|
2020-07-08 17:59:27 +00:00
|
|
|
{this.renderContextMenu()}
|
2020-06-26 01:54:17 +00:00
|
|
|
</React.Fragment>
|
2020-06-26 01:38:11 +00:00
|
|
|
);
|
|
|
|
}
|
2020-06-08 04:06:41 +00:00
|
|
|
}
|