From ca7760789b786c9c45e86012b1f53a85f2f7d66e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 6 Jun 2024 09:19:49 +0100 Subject: [PATCH] Use LegacyRendezvousFailureReason over RendezvousFailureReason (#12578) --- src/components/views/auth/LoginWithQR.tsx | 24 +++++++++++-------- src/components/views/auth/LoginWithQRFlow.tsx | 2 +- .../settings/devices/LoginWithQR-test.tsx | 10 ++++---- .../settings/devices/LoginWithQRFlow-test.tsx | 4 ++-- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/components/views/auth/LoginWithQR.tsx b/src/components/views/auth/LoginWithQR.tsx index 68060f9f28..e1e87104d2 100644 --- a/src/components/views/auth/LoginWithQR.tsx +++ b/src/components/views/auth/LoginWithQR.tsx @@ -15,7 +15,11 @@ limitations under the License. */ import React from "react"; -import { MSC3906Rendezvous, MSC3906RendezvousPayload, RendezvousFailureReason } from "matrix-js-sdk/src/rendezvous"; +import { + MSC3906Rendezvous, + MSC3906RendezvousPayload, + LegacyRendezvousFailureReason, +} from "matrix-js-sdk/src/rendezvous"; import { MSC3886SimpleHttpRendezvousTransport } from "matrix-js-sdk/src/rendezvous/transports"; import { MSC3903ECDHPayload, MSC3903ECDHv2RendezvousChannel } from "matrix-js-sdk/src/rendezvous/channels"; import { logger } from "matrix-js-sdk/src/logger"; @@ -44,7 +48,7 @@ export enum LoginWithQRFailureReason { RateLimited = "rate_limited", } -export type FailureReason = RendezvousFailureReason | LoginWithQRFailureReason; +export type FailureReason = LegacyRendezvousFailureReason | LoginWithQRFailureReason; // n.b MSC3886/MSC3903/MSC3906 that this is based on are now closed. // However, we want to keep this implementation around for some time. @@ -81,7 +85,7 @@ export default class LoginWithQR extends React.Component { if (this.state.rendezvous) { const rendezvous = this.state.rendezvous; rendezvous.onFailure = undefined; - await rendezvous.cancel(RendezvousFailureReason.UserCancelled); + await rendezvous.cancel(LegacyRendezvousFailureReason.UserCancelled); this.setState({ rendezvous: undefined }); } if (mode === Mode.Show) { @@ -94,7 +98,7 @@ export default class LoginWithQR extends React.Component { // eslint-disable-next-line react/no-direct-mutation-state this.state.rendezvous.onFailure = undefined; // calling cancel will call close() as well to clean up the resources - this.state.rendezvous.cancel(RendezvousFailureReason.UserCancelled).then(() => {}); + this.state.rendezvous.cancel(LegacyRendezvousFailureReason.UserCancelled).then(() => {}); } } @@ -140,7 +144,7 @@ export default class LoginWithQR extends React.Component { this.setState({ phase: Phase.Error, failureReason: LoginWithQRFailureReason.RateLimited }); return; } - this.setState({ phase: Phase.Error, failureReason: RendezvousFailureReason.Unknown }); + this.setState({ phase: Phase.Error, failureReason: LegacyRendezvousFailureReason.Unknown }); } }; @@ -170,7 +174,7 @@ export default class LoginWithQR extends React.Component { }); } catch (e) { logger.error("Error whilst generating QR code", e); - this.setState({ phase: Phase.Error, failureReason: RendezvousFailureReason.HomeserverLacksSupport }); + this.setState({ phase: Phase.Error, failureReason: LegacyRendezvousFailureReason.HomeserverLacksSupport }); return; } @@ -181,12 +185,12 @@ export default class LoginWithQR extends React.Component { logger.error("Error whilst doing QR login", e); // only set to error phase if it hasn't already been set by onFailure or similar if (this.state.phase !== Phase.Error) { - this.setState({ phase: Phase.Error, failureReason: RendezvousFailureReason.Unknown }); + this.setState({ phase: Phase.Error, failureReason: LegacyRendezvousFailureReason.Unknown }); } } }; - private onFailure = (reason: RendezvousFailureReason): void => { + private onFailure = (reason: LegacyRendezvousFailureReason): void => { logger.info(`Rendezvous failed: ${reason}`); this.setState({ phase: Phase.Error, failureReason: reason }); }; @@ -202,7 +206,7 @@ export default class LoginWithQR extends React.Component { private onClick = async (type: Click): Promise => { switch (type) { case Click.Cancel: - await this.state.rendezvous?.cancel(RendezvousFailureReason.UserCancelled); + await this.state.rendezvous?.cancel(LegacyRendezvousFailureReason.UserCancelled); this.reset(); this.props.onFinished(false); break; @@ -219,7 +223,7 @@ export default class LoginWithQR extends React.Component { await this.updateMode(this.props.mode); break; case Click.Back: - await this.state.rendezvous?.cancel(RendezvousFailureReason.UserCancelled); + await this.state.rendezvous?.cancel(LegacyRendezvousFailureReason.UserCancelled); this.props.onFinished(false); break; } diff --git a/src/components/views/auth/LoginWithQRFlow.tsx b/src/components/views/auth/LoginWithQRFlow.tsx index 6a6b78a29b..c8a786992a 100644 --- a/src/components/views/auth/LoginWithQRFlow.tsx +++ b/src/components/views/auth/LoginWithQRFlow.tsx @@ -15,7 +15,7 @@ limitations under the License. */ import React, { ReactNode } from "react"; -import { RendezvousFailureReason as LegacyRendezvousFailureReason } from "matrix-js-sdk/src/rendezvous"; +import { LegacyRendezvousFailureReason } from "matrix-js-sdk/src/rendezvous"; import { Icon as ChevronLeftIcon } from "@vector-im/compound-design-tokens/icons/chevron-left.svg"; import { Icon as CheckCircleSolidIcon } from "@vector-im/compound-design-tokens/icons/check-circle-solid.svg"; import { Icon as ErrorIcon } from "@vector-im/compound-design-tokens/icons/error.svg"; diff --git a/test/components/views/settings/devices/LoginWithQR-test.tsx b/test/components/views/settings/devices/LoginWithQR-test.tsx index 7a1a269064..727ab6a017 100644 --- a/test/components/views/settings/devices/LoginWithQR-test.tsx +++ b/test/components/views/settings/devices/LoginWithQR-test.tsx @@ -17,7 +17,7 @@ limitations under the License. import { cleanup, render, waitFor } from "@testing-library/react"; import { MockedObject, mocked } from "jest-mock"; import React from "react"; -import { MSC3906Rendezvous, RendezvousFailureReason } from "matrix-js-sdk/src/rendezvous"; +import { MSC3906Rendezvous, LegacyRendezvousFailureReason } from "matrix-js-sdk/src/rendezvous"; import { HTTPError, LoginTokenPostResponse } from "matrix-js-sdk/src/matrix"; import LoginWithQR from "../../../../../src/components/views/auth/LoginWithQR"; @@ -111,7 +111,7 @@ describe("", () => { await waitFor(() => expect(mockedFlow).toHaveBeenLastCalledWith({ phase: Phase.Error, - failureReason: RendezvousFailureReason.HomeserverLacksSupport, + failureReason: LegacyRendezvousFailureReason.HomeserverLacksSupport, onClick: expect.any(Function), }), ); @@ -125,7 +125,7 @@ describe("", () => { await waitFor(() => expect(mockedFlow).toHaveBeenLastCalledWith({ phase: Phase.Error, - failureReason: RendezvousFailureReason.Unknown, + failureReason: LegacyRendezvousFailureReason.Unknown, onClick: expect.any(Function), }), ); @@ -160,7 +160,7 @@ describe("", () => { const onClick = mockedFlow.mock.calls[0][0].onClick; await onClick(Click.Cancel); expect(onFinished).toHaveBeenCalledWith(false); - expect(rendezvous.cancel).toHaveBeenCalledWith(RendezvousFailureReason.UserCancelled); + expect(rendezvous.cancel).toHaveBeenCalledWith(LegacyRendezvousFailureReason.UserCancelled); // try again onClick(Click.TryAgain); @@ -205,7 +205,7 @@ describe("", () => { const onClick = mockedFlow.mock.calls[0][0].onClick; await onClick(Click.Back); expect(onFinished).toHaveBeenCalledWith(false); - expect(rendezvous.cancel).toHaveBeenCalledWith(RendezvousFailureReason.UserCancelled); + expect(rendezvous.cancel).toHaveBeenCalledWith(LegacyRendezvousFailureReason.UserCancelled); }); test("render QR then decline", async () => { diff --git a/test/components/views/settings/devices/LoginWithQRFlow-test.tsx b/test/components/views/settings/devices/LoginWithQRFlow-test.tsx index 21863c84a4..91004abc26 100644 --- a/test/components/views/settings/devices/LoginWithQRFlow-test.tsx +++ b/test/components/views/settings/devices/LoginWithQRFlow-test.tsx @@ -16,7 +16,7 @@ limitations under the License. import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; import React from "react"; -import { RendezvousFailureReason } from "matrix-js-sdk/src/rendezvous"; +import { LegacyRendezvousFailureReason } from "matrix-js-sdk/src/rendezvous"; import LoginWithQRFlow from "../../../../../src/components/views/auth/LoginWithQRFlow"; import { LoginWithQRFailureReason, FailureReason } from "../../../../../src/components/views/auth/LoginWithQR"; @@ -99,7 +99,7 @@ describe("", () => { describe("errors", () => { for (const failureReason of [ - ...Object.values(RendezvousFailureReason), + ...Object.values(LegacyRendezvousFailureReason), ...Object.values(LoginWithQRFailureReason), ]) { it(`renders ${failureReason}`, async () => {