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

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

98 lines
3.2 KiB
TypeScript
Raw Normal View History

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";
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";
interface IProps {
event: MatrixEvent;
redact: () => Promise<void>;
onFinished: (success?: boolean) => void;
}
interface IState {
isRedacting: boolean;
redactionErrorCode: string | number | null;
}
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.
*/
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,
};
}
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) {
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);
}
};
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}
title={_t("common|error")}
description={_t("redact|error", { code })}
2019-07-04 15:39:36 +02:00
/>
);
} else {
2019-07-08 10:12:26 +02:00
return (
<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 {
return <ConfirmRedactDialog event={this.props.event} onFinished={this.onParentFinished} />;
2019-07-04 15:39:36 +02:00
}
}
}