Unverified Commit ed0e0485 authored by gaoyan's avatar gaoyan Committed by GitHub

Add error show modal (#1948)

parent 21c1dec9
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import React, {useState} from 'react';
import {Button, Modal, ModalFuncProps, Typography} from 'antd';
import CodeShow, {CodeShowFormProps} from "@/components/CustomEditor/CodeShow";
import {l} from "@/utils/intl";
import {MonacoEditorOptions} from "@/types/Public/data";
const {Paragraph} = Typography;
/**
* In this interface, we define the props for the ShowModal component, which extends the ModalFuncProps interface. Here are the details of the props:
*
* codeShowProps (optional): An object containing properties for configuring the code editor that displays in the modal.
* This prop is optional, so it's marked with the ? character.
* content: The content to display in the modal. This prop is required and doesn't have a default value.
* */
export interface ShowModalProps extends ModalFuncProps {
/** Properties for configuring the code editor that displays in the modal. */
codeShowProps?: CodeShowFormProps;
/** The content to display in the code editor. */
content: string;
}
const ErrorShowModal = (props: ShowModalProps) => {
const {
/** The content of the code editor. */
content = "no code to show",
/** The width of the Modal. */
width = "80vh",
/** Control the Modal show or close */
open = true,
/** The title of the Modal.*/
title = l('global.error'),
/** Default disable close icon at top right, it's Not needed */
closable=false,
/**
* The properties of the code editor, including its default options.
*/
codeShowProps = {
/** Whether line numbers should be shown in the code editor. Default is "off". */
lineNumbers: "off",
/** The height of the code editor. */
height: "50vh",
/**
* The options to configure the Monaco Editor instance. The default options are specified
* in a separate object named MonacoEditorOptions, and the minimap is disabled by default.
*/
options: {
...MonacoEditorOptions, // set default options
minimap: {enabled: false}
},
},
} = props;
const [showModal, setShowModal] = useState<boolean>(open);
return (
<Modal
{...props}
width={width}
open={showModal}
title={title}
closable={closable}
footer={<Button type="primary" onClick={()=>setShowModal(false)}>{l('global.notifaction.iknow')}</Button>}
>
<Paragraph ellipsis={{rows: 1, expandable: false}}>
<blockquote>{content}</blockquote>
</Paragraph>
<CodeShow
{...codeShowProps}
code={content}
/>
</Modal>
);
};
export default ErrorShowModal;
...@@ -147,4 +147,8 @@ export default { ...@@ -147,4 +147,8 @@ export default {
'global.error': 'Error', 'global.error': 'Error',
'global.warning': 'Warning', 'global.warning': 'Warning',
'global.success': 'Success', 'global.success': 'Success',
'global.notifaction.iknow': 'I understand',
'global.notifaction.full-error': 'Complete error message',
}; };
...@@ -142,4 +142,8 @@ export default { ...@@ -142,4 +142,8 @@ export default {
'global.error': '错误', 'global.error': '错误',
'global.warning': '警告', 'global.warning': '警告',
'global.success': '成功', 'global.success': '成功',
'global.notifaction.iknow': '我知道了',
'global.notifaction.full-error': '完整错误信息',
}; };
...@@ -16,9 +16,13 @@ ...@@ -16,9 +16,13 @@
* *
*/ */
import {notification,message, App} from "antd" import {notification, message, Button} from "antd"
import {l} from "@/utils/intl"; import {l} from "@/utils/intl";
import {Typography} from 'antd';
import {InfoCircleOutlined} from "@ant-design/icons";
import {ErrorModelWithCode} from "@/utils/modals";
const {Paragraph} = Typography;
/** /**
* default params * default params
*/ */
...@@ -29,18 +33,46 @@ const AsyncDuration = 0.5 ...@@ -29,18 +33,46 @@ const AsyncDuration = 0.5
// ================================ About Notification Component ================================ // ================================ About Notification Component ================================
/** /**
* Error handler message * A function that displays an error notification with a short message and an option to view the full error message in a modal.
* @param message *
* @param description * @param description The short error message to display in the notification.
* @param duration * @param message The title of the notification. This parameter is optional, and if no value is provided, it defaults to a string.
* @param placement * @param duration The duration (in milliseconds) that the notification should be displayed for.
* @constructor * This parameter is optional, and if no value is provided, it defaults to a default duration.
*/ */
export const ErrorNotification = (description: string, message = l('global.error'), duration = defaultDuration) => notification.error({ export const ErrorNotification = (description: string, message = l('global.error'), duration = defaultDuration) => {
message,
description, /** The title of the notification, with an icon and the provided message. */
duration, const title = (
}) <div style={{display: 'flex'}}>
<InfoCircleOutlined style={{marginRight: 10, color: "red"}}/>
<b>{message}</b>
</div>
)
/**
* A button that is displayed in the notification
* if the error message is longer than 40 characters, click it will create a Modal to show more information detail
*/
const moreBtn = description.length > 40 ? (
<Button type="primary" danger onClick={() => ErrorModelWithCode(title, description)}>
{l('global.notifaction.full-error')}
</Button>
) : (<></>);
/** Display the notification. */
notification.open({
/** The title of the notification. */
message: title,
/**
* The short error message to display in the notification.
* if the error message is longer than 40 characters,Parts beyond the length are hidden
*/
description: (<Paragraph ellipsis={{rows: 2}}>{description}</Paragraph>),
duration: duration,
btn: moreBtn,
})
}
/** /**
* Success handler message * Success handler message
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// ================================ About Modal Compents ================================
import {l} from "@/utils/intl";
import React from "react";
import ErrorShowModal from "@/components/Modal/ErrorModalShow";
import {createRoot} from "react-dom/client";
/**
* A function that displays a modal containing an error message in code editor.
*
* @param title The title of the modal.
* @param content The content to display in the modal.
*/
export const ErrorModelWithCode = (title: any = l('global.error'), content: string) => {
/** Create a new div element to mount the modal. */
const modalRoot = document.createElement('div');
/** Add the div element to the body of the document. */
document.body.appendChild(modalRoot);
const root = createRoot(modalRoot);
root.render(
<ErrorShowModal
/** Function that runs after the modal is closed, to unmount the component and remove the modal from the DOM. */
afterClose={() => root.unmount()}
title={title}
content={content}
/>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment