Files
ThreadNet-Web/src/components/views/dialogs/VerificationRequestDialog.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
4.5 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2020, 2021 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
*/
import React from "react";
import { VerificationPhase, VerificationRequestEvent, type VerificationRequest } from "matrix-js-sdk/src/crypto-api";
2025-02-05 13:25:06 +00:00
import { type User } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../../MatrixClientPeg";
import { _t } from "../../../languageHandler";
2021-07-01 14:22:58 +01:00
import BaseDialog from "./BaseDialog";
import EncryptionPanel from "../right_panel/EncryptionPanel";
interface IProps {
verificationRequest?: VerificationRequest;
verificationRequestPromise?: Promise<VerificationRequest>;
2021-07-01 14:22:58 +01:00
onFinished: () => void;
member?: User;
2021-07-01 14:22:58 +01:00
}
interface IState {
// The VerificationRequest that is ongoing. This can be replaced if a
// promise was supplied in the props and it completes.
verificationRequest?: VerificationRequest;
// What phase the VerificationRequest is at. This is part of
// verificationRequest but we have it as independent state because we need
// to update when it changes.
//
// We listen to the `Change` event on verificationRequest and update phase
// when that fires.
phase?: VerificationPhase;
2021-07-01 14:22:58 +01:00
}
2021-07-01 14:22:58 +01:00
export default class VerificationRequestDialog extends React.Component<IProps, IState> {
2023-02-13 11:39:16 +00:00
public constructor(props: IProps) {
2021-07-01 14:22:58 +01:00
super(props);
this.state = {
verificationRequest: this.props.verificationRequest,
phase: this.props.verificationRequest?.phase,
2021-07-01 14:22:58 +01:00
};
}
public componentDidMount(): void {
// Listen to when the verificationRequest changes, so we can keep our
// phase up-to-date.
this.state.verificationRequest?.on(VerificationRequestEvent.Change, this.onRequestChange);
this.props.verificationRequestPromise?.then((r) => {
// The request promise completed, so we have a new request
// Stop listening to the old request (if we have one, which normally we won't)
this.state.verificationRequest?.off(VerificationRequestEvent.Change, this.onRequestChange);
// And start listening to the new one
r.on(VerificationRequestEvent.Change, this.onRequestChange);
this.setState({ verificationRequest: r, phase: r.phase });
});
2020-02-18 11:14:16 +01:00
}
public componentWillUnmount(): void {
// Stop listening for changes to the request when we close
this.state.verificationRequest?.off(VerificationRequestEvent.Change, this.onRequestChange);
}
/**
* The verificationRequest changed, so we need to make sure we update our
* state to have the correct phase.
*
* Note: this is called when verificationRequest changes in some way, not
* when we replace verificationRequest with some new request.
*/
private readonly onRequestChange = (): void => {
this.setState((prevState) => ({
phase: prevState.verificationRequest?.phase,
}));
};
public render(): React.ReactNode {
2020-04-02 17:12:10 +02:00
const request = this.state.verificationRequest;
const otherUserId = request?.otherUserId;
const member = this.props.member || (otherUserId ? MatrixClientPeg.safeGet().getUser(otherUserId) : null);
const title = this.dialogTitle(request);
2020-04-02 17:12:10 +02:00
if (!member) return null;
2021-04-27 17:23:27 +02:00
return (
<BaseDialog
className="mx_InfoDialog"
onFinished={this.props.onFinished}
contentId="mx_Dialog_content"
title={title}
hasCancel={true}
>
<EncryptionPanel
layout="dialog"
verificationRequest={this.state.verificationRequest}
verificationRequestPromise={this.props.verificationRequestPromise}
onClose={this.props.onFinished}
member={member}
2021-07-01 14:22:58 +01:00
isRoomEncrypted={false}
/>
</BaseDialog>
);
}
private dialogTitle(request?: VerificationRequest): string {
if (request?.isSelfVerification) {
if (request.phase === VerificationPhase.Cancelled) {
return _t("encryption|verification|verification_dialog_title_failed");
} else {
return _t("encryption|verification|verification_dialog_title_device");
}
} else {
return _t("encryption|verification|verification_dialog_title_user");
}
}
}