Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Add sign out button to settings profile section #12666

Merged
merged 11 commits into from
Jul 29, 2024
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion src/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import { defer, sleep } from "matrix-js-sdk/src/utils";
import { TypedEventEmitter } from "matrix-js-sdk/src/matrix";
import { Glass } from "@vector-im/compound-web";

import dis from "./dispatcher/dispatcher";
import dis, { defaultDispatcher } from "./dispatcher/dispatcher";
import AsyncWrapper from "./AsyncWrapper";
import { Defaultize } from "./@types/common";
import { ActionPayload } from "./dispatcher/payloads";

const DIALOG_CONTAINER_ID = "mx_Dialog_Container";
const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer";
Expand Down Expand Up @@ -111,6 +112,19 @@ export class ModalManager extends TypedEventEmitter<ModalManagerEvent, HandlerMa
return container;
}

public constructor() {
super();

// We never unregister this: it's a singleton. An instance could be instantiated, in theory...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An instance could be instantiated, in theory...

Don't quite get this. So we should deregister the listener?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this comment in the split out PR so hopefully its clearer.

defaultDispatcher.register(this.onAction);
}

private onAction = (payload: ActionPayload): void => {
if (payload.action === "logout") {
this.closeCurrentModal();
dbkr marked this conversation as resolved.
Show resolved Hide resolved
}
};

public toggleCurrentDialogVisibility(): void {
const modal = this.getCurrentModal();
if (!modal) return;
Expand Down
25 changes: 2 additions & 23 deletions src/components/structures/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { UserTab } from "../views/dialogs/UserTab";
import { OpenToTabPayload } from "../../dispatcher/payloads/OpenToTabPayload";
import FeedbackDialog from "../views/dialogs/FeedbackDialog";
import Modal from "../../Modal";
import LogoutDialog from "../views/dialogs/LogoutDialog";
import LogoutDialog, { shouldShowLogoutDialog } from "../views/dialogs/LogoutDialog";
import SettingsStore from "../../settings/SettingsStore";
import { findHighContrastTheme, getCustomTheme, isHighContrastTheme } from "../../theme";
import { RovingAccessibleButton } from "../../accessibility/RovingTabIndex";
Expand Down Expand Up @@ -288,7 +288,7 @@ export default class UserMenu extends React.Component<IProps, IState> {
ev.preventDefault();
ev.stopPropagation();

if (await this.shouldShowLogoutDialog()) {
if (await shouldShowLogoutDialog(MatrixClientPeg.safeGet())) {
Modal.createDialog(LogoutDialog);
} else {
defaultDispatcher.dispatch({ action: "logout" });
Expand All @@ -297,27 +297,6 @@ export default class UserMenu extends React.Component<IProps, IState> {
this.setState({ contextMenuPosition: null }); // also close the menu
};

/**
* Checks if the `LogoutDialog` should be shown instead of the simple logout flow.
* The `LogoutDialog` will check the crypto recovery status of the account and
* help the user setup recovery properly if needed.
* @private
*/
private async shouldShowLogoutDialog(): Promise<boolean> {
const cli = MatrixClientPeg.get();
const crypto = cli?.getCrypto();
if (!crypto) return false;

// If any room is encrypted, we need to show the advanced logout flow
const allRooms = cli!.getRooms();
for (const room of allRooms) {
const isE2e = await crypto.isEncryptionEnabledInRoom(room.roomId);
if (isE2e) return true;
}

return false;
}

private onSignInClick = (): void => {
defaultDispatcher.dispatch({ action: "start_login" });
this.setState({ contextMenuPosition: null }); // also close the menu
Expand Down
20 changes: 20 additions & 0 deletions src/components/views/dialogs/LogoutDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.

import React from "react";
import { logger } from "matrix-js-sdk/src/logger";
import { MatrixClient } from "matrix-js-sdk/src/matrix";

import type CreateKeyBackupDialog from "../../../async-components/views/dialogs/security/CreateKeyBackupDialog";
import type ExportE2eKeysDialog from "../../../async-components/views/dialogs/security/ExportE2eKeysDialog";
Expand Down Expand Up @@ -58,6 +59,25 @@ interface IState {
backupStatus: BackupStatus;
}

/**
* Checks if the `LogoutDialog` should be shown instead of the simple logout flow.
* The `LogoutDialog` will check the crypto recovery status of the account and
* help the user setup recovery properly if needed.
*/
export async function shouldShowLogoutDialog(cli: MatrixClient): Promise<boolean> {
const crypto = cli?.getCrypto();
if (!crypto) return false;

// If any room is encrypted, we need to show the advanced logout flow
const allRooms = cli!.getRooms();
for (const room of allRooms) {
const isE2e = await crypto.isEncryptionEnabledInRoom(room.roomId);
if (isE2e) return true;
}

return false;
}

export default class LogoutDialog extends React.Component<IProps, IState> {
public static defaultProps = {
onFinished: function () {},
Expand Down
33 changes: 29 additions & 4 deletions src/components/views/settings/UserProfileSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import React, { ChangeEvent, useCallback, useEffect, useMemo, useState } from "r
import { logger } from "matrix-js-sdk/src/logger";
import { EditInPlace, Alert, ErrorMessage } from "@vector-im/compound-web";
import { Icon as PopOutIcon } from "@vector-im/compound-design-tokens/icons/pop-out.svg";
import { Icon as SignOutIcon } from "@vector-im/compound-design-tokens/icons/sign-out.svg";

import { _t } from "../../../languageHandler";
import { OwnProfileStore } from "../../../stores/OwnProfileStore";
Expand All @@ -31,6 +32,10 @@ import { useId } from "../../../utils/useId";
import CopyableText from "../elements/CopyableText";
import { useMatrixClientContext } from "../../../contexts/MatrixClientContext";
import AccessibleButton from "../elements/AccessibleButton";
import LogoutDialog, { shouldShowLogoutDialog } from "../dialogs/LogoutDialog";
import Modal from "../../../Modal";
import defaultDispatcher from "../../../dispatcher/dispatcher";
import { Flex } from "../../utils/Flex";

const SpinnerToast: React.FC = ({ children }) => (
<>
Expand Down Expand Up @@ -76,6 +81,25 @@ const ManageAccountButton: React.FC<ManageAccountButtonProps> = ({ externalAccou
</AccessibleButton>
);

const SignOutButton: React.FC = () => {
const client = useMatrixClientContext();

const onClick = useCallback(async () => {
if (await shouldShowLogoutDialog(client)) {
Modal.createDialog(LogoutDialog);
} else {
defaultDispatcher.dispatch({ action: "logout" });
}
}, [client]);

return (
<AccessibleButton onClick={onClick} kind="danger_outline">
<SignOutIcon className="mx_UserProfileSettings_accountmanageIcon" width="24" height="24" />
{_t("action|sign_out")}
</AccessibleButton>
);
};

interface UserProfileSettingsProps {
// The URL to redirect the user to in order to manage their account.
externalAccountManagementUrl?: string;
Expand Down Expand Up @@ -219,11 +243,12 @@ const UserProfileSettings: React.FC<UserProfileSettingsProps> = ({
</Alert>
)}
{userIdentifier && <UsernameBox username={userIdentifier} />}
{externalAccountManagementUrl && (
<div className="mx_UserProfileSettings_profile_buttons">
<Flex gap="var(--cpd-space-4x)" className="mx_UserProfileSettings_profile_buttons">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really want this sort of inline CSS? Shouldn't the gap be on the CSS class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really have thought so, yeah, but this seems to be how the Flex component is used, and that's what was suggested.

{externalAccountManagementUrl && (
<ManageAccountButton externalAccountManagementUrl={externalAccountManagementUrl} />
</div>
)}
)}
<SignOutButton />
</Flex>
</div>
);
};
Expand Down
Loading