Files
ThreadNet-Web/apps/web/src/components/views/dialogs/ErrorDialog.tsx
T

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

85 lines
2.7 KiB
TypeScript
Raw Normal View History

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
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
*/
/*
* Usage:
2022-06-14 17:51:51 +01:00
* Modal.createDialog(ErrorDialog, {
* title: "some text", (default: "Error")
* description: "some more text",
* button: "Button Text",
* onFinished: someFunction,
* 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
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
/**
* 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 {
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> {
public static defaultProps: Partial<IProps> = {
2020-08-29 12:14:16 +01:00
focus: true,
};
private onClick = (): void => {
2021-02-01 16:25:50 +00:00
this.props.onFinished(true);
};
public render(): React.ReactNode {
return (
2020-01-28 11:13:09 +00:00
<BaseDialog
className="mx_ErrorDialog"
onFinished={this.props.onFinished}
title={this.props.title || _t("common|error")}
2020-01-28 11:13:09 +00:00
headerImage={this.props.headerImage}
contentId="mx_Dialog_content"
>
<div className="mx_Dialog_content" id="mx_Dialog_content">
{this.props.description || _t("error|dialog_description_default")}
</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}>
{this.props.button || _t("action|ok")}
</button>
</div>
2017-01-24 15:54:05 +00:00
</BaseDialog>
);
2020-08-29 12:14:16 +01:00
}
}