index.tsx
63 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { |
| 5 | Icon, |
| 6 | warning, |
| 7 | info, |
| 8 | cancelCircleFilled as error, |
| 9 | check as success, |
| 10 | } from '@wordpress/icons'; |
| 11 | /** |
| 12 | * Types |
| 13 | */ |
| 14 | import type React from 'react'; |
| 15 | |
| 16 | import './style.scss'; |
| 17 | |
| 18 | export const MESSAGE_SEVERITY_WARNING = 'warning'; |
| 19 | export const MESSAGE_SEVERITY_ERROR = 'error'; |
| 20 | export const MESSAGE_SEVERITY_SUCCESS = 'success'; |
| 21 | export const MESSAGE_SEVERITY_INFO = 'info'; |
| 22 | |
| 23 | const messageSeverityTypes = [ |
| 24 | MESSAGE_SEVERITY_WARNING, |
| 25 | MESSAGE_SEVERITY_ERROR, |
| 26 | MESSAGE_SEVERITY_SUCCESS, |
| 27 | MESSAGE_SEVERITY_INFO, |
| 28 | ] as const; |
| 29 | |
| 30 | export type MessageSeverityProp = ( typeof messageSeverityTypes )[ number ] | null; |
| 31 | |
| 32 | export type MessageProps = { |
| 33 | icon?: React.ReactNode; |
| 34 | children: React.ReactNode; |
| 35 | severity: MessageSeverityProp; |
| 36 | }; |
| 37 | |
| 38 | const messageIconsMap = { |
| 39 | [ MESSAGE_SEVERITY_WARNING ]: warning, |
| 40 | [ MESSAGE_SEVERITY_ERROR ]: error, |
| 41 | [ MESSAGE_SEVERITY_SUCCESS ]: success, |
| 42 | [ MESSAGE_SEVERITY_INFO ]: info, |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * React component to render a block message. |
| 47 | * |
| 48 | * @param {MessageProps} props - Component props. |
| 49 | * @returns {React.ReactElement } Banner component. |
| 50 | */ |
| 51 | export default function Message( { |
| 52 | severity = null, |
| 53 | icon = null, |
| 54 | children, |
| 55 | }: MessageProps ): React.ReactElement { |
| 56 | return ( |
| 57 | <div className="jetpack-ai-assistant__message"> |
| 58 | { ( severity || icon ) && <Icon icon={ messageIconsMap[ severity ] || icon } /> } |
| 59 | <div className="jetpack-ai-assistant__message-content">{ children }</div> |
| 60 | </div> |
| 61 | ); |
| 62 | } |
| 63 |