diff --git a/src/components/views/settings/encryption/RecoveryPanel.tsx b/src/components/views/settings/encryption/RecoveryPanel.tsx index ce237aeb1ba..25079b1ca08 100644 --- a/src/components/views/settings/encryption/RecoveryPanel.tsx +++ b/src/components/views/settings/encryption/RecoveryPanel.tsx @@ -5,7 +5,7 @@ * Please see LICENSE files in the repository root for full details. */ -import React, { JSX, MouseEventHandler, useCallback, useEffect, useState } from "react"; +import React, { JSX, useCallback, useEffect, useState } from "react"; import { Button, InlineSpinner } from "@vector-im/compound-web"; import KeyIcon from "@vector-im/compound-design-tokens/assets/web/icons/key"; @@ -27,19 +27,15 @@ type State = "loading" | "missing_backup" | "secrets_not_cached" | "good"; interface RecoveryPanelProps { /** - * Callback for when the user clicks the button to set up their recovery key. + * Callback for when the user wants to set up or change their recovery key. */ - onSetUpRecoveryClick: MouseEventHandler; - /** - * Callback for when the user clicks the button to change their recovery key. - */ - onChangingRecoveryKeyClick: MouseEventHandler; + onChangeRecoveryKeyClick: (setupNewKey: boolean) => void; } /** * This component allows the user to set up or change their recovery key. */ -export function RecoveryPanel({ onSetUpRecoveryClick, onChangingRecoveryKeyClick }: RecoveryPanelProps): JSX.Element { +export function RecoveryPanel({ onChangeRecoveryKeyClick }: RecoveryPanelProps): JSX.Element { const [state, setState] = useState("loading"); const isMissingBackup = state === "missing_backup"; @@ -71,7 +67,7 @@ export function RecoveryPanel({ onSetUpRecoveryClick, onChangingRecoveryKeyClick break; case "missing_backup": content = ( - ); @@ -90,7 +86,7 @@ export function RecoveryPanel({ onSetUpRecoveryClick, onChangingRecoveryKeyClick break; case "good": content = ( - ); diff --git a/src/components/views/settings/tabs/user/EncryptionUserSettingsTab.tsx b/src/components/views/settings/tabs/user/EncryptionUserSettingsTab.tsx index 969d157cdb6..c1d3b268943 100644 --- a/src/components/views/settings/tabs/user/EncryptionUserSettingsTab.tsx +++ b/src/components/views/settings/tabs/user/EncryptionUserSettingsTab.tsx @@ -44,8 +44,9 @@ export function EncryptionUserSettingsTab(): JSX.Element { case "main": content = ( setState("change_recovery_key")} - onSetUpRecoveryClick={() => setState("set_recovery_key")} + onChangeRecoveryKeyClick={(setupNewKey) => + setupNewKey ? setState("set_recovery_key") : setState("change_recovery_key") + } /> ); break; diff --git a/test/unit-tests/components/views/settings/encryption/RecoveryPanel-test.tsx b/test/unit-tests/components/views/settings/encryption/RecoveryPanel-test.tsx index ce81b51b6f4..a4af5964be8 100644 --- a/test/unit-tests/components/views/settings/encryption/RecoveryPanel-test.tsx +++ b/test/unit-tests/components/views/settings/encryption/RecoveryPanel-test.tsx @@ -29,13 +29,11 @@ describe("", () => { mocked(accessSecretStorage).mockClear().mockResolvedValue(); }); - function renderRecoverPanel( - props = { - onSetUpRecoveryClick: jest.fn(), - onChangingRecoveryKeyClick: jest.fn(), - }, - ) { - return render(, withClientContextRenderOptions(matrixClient)); + function renderRecoverPanel(onChangeRecoveryKeyClick = jest.fn()) { + return render( + , + withClientContextRenderOptions(matrixClient), + ); } it("should be in loading state when checking backup and the cached keys", () => { @@ -51,14 +49,14 @@ describe("", () => { it("should ask to set up a recovery key when there is no key backup", async () => { const user = userEvent.setup(); - const onSetUpRecoveryClick = jest.fn(); - const { asFragment } = renderRecoverPanel({ onSetUpRecoveryClick, onChangingRecoveryKeyClick: jest.fn() }); + const onChangeRecoveryKeyClick = jest.fn(); + const { asFragment } = renderRecoverPanel(onChangeRecoveryKeyClick); await waitFor(() => screen.getByRole("button", { name: "Set up recovery" })); expect(asFragment()).toMatchSnapshot(); await user.click(screen.getByRole("button", { name: "Set up recovery" })); - expect(onSetUpRecoveryClick).toHaveBeenCalled(); + expect(onChangeRecoveryKeyClick).toHaveBeenCalledWith(true); }); it("should ask to enter the recovery key when secrets are not cached", async () => { @@ -86,12 +84,12 @@ describe("", () => { }); const user = userEvent.setup(); - const onChangingRecoveryKeyClick = jest.fn(); - const { asFragment } = renderRecoverPanel({ onSetUpRecoveryClick: jest.fn(), onChangingRecoveryKeyClick }); + const onChangeRecoveryKeyClick = jest.fn(); + const { asFragment } = renderRecoverPanel(onChangeRecoveryKeyClick); await waitFor(() => screen.getByRole("button", { name: "Change recovery key" })); expect(asFragment()).toMatchSnapshot(); await user.click(screen.getByRole("button", { name: "Change recovery key" })); - expect(onChangingRecoveryKeyClick).toHaveBeenCalled(); + expect(onChangeRecoveryKeyClick).toHaveBeenCalledWith(false); }); });