2015-09-18 18:39:16 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-18 18:39:16 +01:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
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.
|
2015-09-18 18:39:16 +01:00
|
|
|
*/
|
|
|
|
|
|
2015-11-30 14:11:04 +00:00
|
|
|
/*
|
|
|
|
|
* Usage:
|
2022-06-14 17:51:51 +01:00
|
|
|
* Modal.createDialog(ErrorDialog, {
|
2015-11-30 14:11:04 +00:00
|
|
|
* title: "some text", (default: "Error")
|
|
|
|
|
* description: "some more text",
|
|
|
|
|
* button: "Button Text",
|
2016-03-22 17:20:22 +00:00
|
|
|
* onFinished: someFunction,
|
2015-11-30 14:11:04 +00:00
|
|
|
* focus: true|false (default: true)
|
|
|
|
|
* });
|
|
|
|
|
*/
|
|
|
|
|
|
2017-01-24 15:54:05 +00:00
|
|
|
import React from "react";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2023-04-14 09:40:19 -05:00
|
|
|
import { _t, UserFriendlyError } from "../../../languageHandler";
|
2021-07-02 17:08:27 +02:00
|
|
|
import BaseDialog from "./BaseDialog";
|
2015-09-18 18:39:16 +01:00
|
|
|
|
2023-04-14 09:40:19 -05:00
|
|
|
/**
|
|
|
|
|
* Get a user friendly error message string from a given error. Useful for the
|
|
|
|
|
* `description` prop of the `ErrorDialog`
|
|
|
|
|
* @param err Error object in question to extract a useful message from. To make it easy
|
|
|
|
|
* to use with try/catch, this is typed as `any` because try/catch will type
|
|
|
|
|
* the error as `unknown`. And in any case we can use the fallback message.
|
|
|
|
|
* @param translatedFallbackMessage The fallback message to be used if the error doesn't have any message
|
|
|
|
|
* @returns a user friendly error message string from a given error
|
|
|
|
|
*/
|
|
|
|
|
export function extractErrorMessageFromError(err: any, translatedFallbackMessage: string): string {
|
|
|
|
|
return (
|
|
|
|
|
(err instanceof UserFriendlyError && err.translatedMessage) ||
|
|
|
|
|
(err instanceof Error && err.message) ||
|
|
|
|
|
translatedFallbackMessage
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 21:55:47 +01:00
|
|
|
interface IProps {
|
2023-02-28 10:31:48 +00:00
|
|
|
onFinished: (success?: boolean) => void;
|
2021-06-14 21:55:47 +01:00
|
|
|
title?: string;
|
|
|
|
|
description?: React.ReactNode;
|
|
|
|
|
button?: string;
|
|
|
|
|
focus?: boolean;
|
|
|
|
|
headerImage?: string;
|
|
|
|
|
}
|
2015-09-18 18:39:16 +01:00
|
|
|
|
2021-06-14 21:55:47 +01:00
|
|
|
interface IState {
|
|
|
|
|
onFinished: (success: boolean) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class ErrorDialog extends React.Component<IProps, IState> {
|
2023-02-28 10:31:48 +00:00
|
|
|
public static defaultProps: Partial<IProps> = {
|
2020-08-29 12:14:16 +01:00
|
|
|
focus: true,
|
|
|
|
|
};
|
2015-11-30 14:11:04 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onClick = (): void => {
|
2021-02-01 16:25:50 +00:00
|
|
|
this.props.onFinished(true);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2015-11-30 14:11:04 +00:00
|
|
|
return (
|
2020-01-28 11:13:09 +00:00
|
|
|
<BaseDialog
|
|
|
|
|
className="mx_ErrorDialog"
|
|
|
|
|
onFinished={this.props.onFinished}
|
2023-08-22 18:47:33 +01:00
|
|
|
title={this.props.title || _t("common|error")}
|
2020-01-28 11:13:09 +00:00
|
|
|
headerImage={this.props.headerImage}
|
|
|
|
|
contentId="mx_Dialog_content"
|
2017-12-05 13:52:20 +01:00
|
|
|
>
|
|
|
|
|
<div className="mx_Dialog_content" id="mx_Dialog_content">
|
2023-10-03 19:17:26 +01:00
|
|
|
{this.props.description || _t("error|dialog_description_default")}
|
2015-11-30 14:11:04 +00:00
|
|
|
</div>
|
|
|
|
|
<div className="mx_Dialog_buttons">
|
2021-02-01 16:25:50 +00:00
|
|
|
<button className="mx_Dialog_primary" onClick={this.onClick} autoFocus={this.props.focus}>
|
2023-08-22 20:55:15 +01:00
|
|
|
{this.props.button || _t("action|ok")}
|
2015-11-30 14:11:04 +00:00
|
|
|
</button>
|
|
|
|
|
</div>
|
2017-01-24 15:54:05 +00:00
|
|
|
</BaseDialog>
|
2015-11-30 14:11:04 +00:00
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|