2019-07-04 15:39:36 +02:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-07-04 15:39:36 +02:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-07-04 15:39:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2023-08-07 09:24:58 +01:00
|
|
|
import { MatrixEvent, HTTPError, MatrixError } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2019-07-04 15:39:36 +02:00
|
|
|
import { _t } from "../../../languageHandler";
|
2021-07-03 11:24:33 +02:00
|
|
|
import ConfirmRedactDialog from "./ConfirmRedactDialog";
|
|
|
|
|
import ErrorDialog from "./ErrorDialog";
|
|
|
|
|
import BaseDialog from "./BaseDialog";
|
|
|
|
|
import Spinner from "../elements/Spinner";
|
2021-06-14 21:30:28 +01:00
|
|
|
|
|
|
|
|
interface IProps {
|
2023-06-14 09:49:17 +01:00
|
|
|
event: MatrixEvent;
|
2021-06-14 21:30:28 +01:00
|
|
|
redact: () => Promise<void>;
|
2023-02-28 10:31:48 +00:00
|
|
|
onFinished: (success?: boolean) => void;
|
2021-06-14 21:30:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
|
isRedacting: boolean;
|
2023-04-04 11:41:46 +01:00
|
|
|
redactionErrorCode: string | number | null;
|
2021-06-14 21:30:28 +01:00
|
|
|
}
|
2019-07-04 15:39:36 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A dialog for confirming a redaction.
|
|
|
|
|
* Also shows a spinner (and possible error) while the redaction is ongoing,
|
|
|
|
|
* and only closes the dialog when the redaction is done or failed.
|
|
|
|
|
*
|
|
|
|
|
* This is done to prevent the edit history dialog racing with the redaction:
|
|
|
|
|
* if this dialog closes and the MessageEditHistoryDialog is shown again,
|
|
|
|
|
* it will fetch the relations again, which will race with the ongoing /redact request.
|
|
|
|
|
* which will cause the edit to appear unredacted.
|
|
|
|
|
*
|
|
|
|
|
* To avoid this, we keep the dialog open as long as /redact is in progress.
|
|
|
|
|
*/
|
2021-06-14 21:30:28 +01:00
|
|
|
export default class ConfirmAndWaitRedactDialog extends React.PureComponent<IProps, IState> {
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(props: IProps) {
|
2019-07-04 15:39:36 +02:00
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
isRedacting: false,
|
|
|
|
|
redactionErrorCode: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 09:02:40 +01:00
|
|
|
public onParentFinished = async (proceed?: boolean): Promise<void> => {
|
2019-07-04 15:39:36 +02:00
|
|
|
if (proceed) {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ isRedacting: true });
|
2019-07-04 15:39:36 +02:00
|
|
|
try {
|
|
|
|
|
await this.props.redact();
|
|
|
|
|
this.props.onFinished(true);
|
|
|
|
|
} catch (error) {
|
2023-06-22 14:39:36 +01:00
|
|
|
let code: string | number | undefined;
|
|
|
|
|
if (error instanceof MatrixError) {
|
|
|
|
|
code = error.errcode;
|
|
|
|
|
} else if (error instanceof HTTPError) {
|
|
|
|
|
code = error.httpStatus;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-04 15:39:36 +02:00
|
|
|
if (typeof code !== "undefined") {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ redactionErrorCode: code });
|
2019-07-04 15:39:36 +02:00
|
|
|
} else {
|
|
|
|
|
this.props.onFinished(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.props.onFinished(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2019-07-04 15:39:36 +02:00
|
|
|
if (this.state.isRedacting) {
|
|
|
|
|
if (this.state.redactionErrorCode) {
|
|
|
|
|
const code = this.state.redactionErrorCode;
|
|
|
|
|
return (
|
|
|
|
|
<ErrorDialog
|
|
|
|
|
onFinished={this.props.onFinished}
|
2023-08-22 18:47:33 +01:00
|
|
|
title={_t("common|error")}
|
2023-10-03 19:17:26 +01:00
|
|
|
description={_t("redact|error", { code })}
|
2019-07-04 15:39:36 +02:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2019-07-08 10:12:26 +02:00
|
|
|
return (
|
2023-10-03 19:17:26 +01:00
|
|
|
<BaseDialog onFinished={this.props.onFinished} hasCancel={false} title={_t("redact|ongoing")}>
|
2019-07-08 10:12:26 +02:00
|
|
|
<Spinner />
|
|
|
|
|
</BaseDialog>
|
2019-07-04 15:39:36 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-06-14 09:49:17 +01:00
|
|
|
return <ConfirmRedactDialog event={this.props.event} onFinished={this.onParentFinished} />;
|
2019-07-04 15:39:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|