block-message.tsx
89 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { ExternalLink } from '@wordpress/components'; |
| 5 | import { createInterpolateElement } from '@wordpress/element'; |
| 6 | import { __ } from '@wordpress/i18n'; |
| 7 | /** |
| 8 | * Internal dependencies |
| 9 | */ |
| 10 | import Message, { MESSAGE_SEVERITY_INFO } from '.'; |
| 11 | import './style.scss'; |
| 12 | /** |
| 13 | * Types |
| 14 | */ |
| 15 | import type { MessageSeverityProp, MessageProps } from '.'; |
| 16 | import type React from 'react'; |
| 17 | |
| 18 | export const ASSISTANT_STATE_INIT = 'init'; |
| 19 | export const ASSISTANT_STATE_READY_TO_GENERATE = 'ready-to-generate'; |
| 20 | export const ASSISTANT_STATE_GENERATING = 'generating-content'; |
| 21 | export const ASSISTANT_STATE_CONTENT_GENERATED = 'content-generated'; |
| 22 | |
| 23 | const blockStateTypes = [ |
| 24 | ASSISTANT_STATE_INIT, |
| 25 | ASSISTANT_STATE_READY_TO_GENERATE, |
| 26 | ASSISTANT_STATE_GENERATING, |
| 27 | ASSISTANT_STATE_CONTENT_GENERATED, |
| 28 | ] as const; |
| 29 | |
| 30 | export type BlockMessageProps = MessageProps & { |
| 31 | state: ( typeof blockStateTypes )[ number ]; |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * React component to render a block message. |
| 36 | * |
| 37 | * @param {BlockMessageProps} props - Component props. |
| 38 | * @returns {React.ReactElement } Banner component. |
| 39 | */ |
| 40 | export default function BlockMessage( props: BlockMessageProps ): React.ReactElement { |
| 41 | const { state } = props; |
| 42 | if ( ! state ) { |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | // Ready to generate message |
| 47 | let messageText = null; |
| 48 | // eslint-disable-next-line prefer-const |
| 49 | let severity: MessageSeverityProp = MESSAGE_SEVERITY_INFO; |
| 50 | |
| 51 | switch ( state ) { |
| 52 | case ASSISTANT_STATE_INIT: |
| 53 | messageText = __( 'Ask AI Assistant for anything…', 'jetpack' ); // 'Ask for content suggestions. |
| 54 | break; |
| 55 | |
| 56 | case ASSISTANT_STATE_READY_TO_GENERATE: |
| 57 | messageText = createInterpolateElement( |
| 58 | __( 'Press <em>Enter</em> to send your request.', 'jetpack' ), |
| 59 | { |
| 60 | em: <em />, |
| 61 | } |
| 62 | ); |
| 63 | |
| 64 | break; |
| 65 | |
| 66 | case ASSISTANT_STATE_GENERATING: |
| 67 | messageText = __( 'Generating content…', 'jetpack' ); |
| 68 | break; |
| 69 | |
| 70 | case ASSISTANT_STATE_CONTENT_GENERATED: |
| 71 | messageText = createInterpolateElement( |
| 72 | __( |
| 73 | 'AI-generated content could be inaccurate or biased. <link>Learn more</link>', |
| 74 | 'jetpack' |
| 75 | ), |
| 76 | { |
| 77 | link: <ExternalLink href="https://automattic.com/ai-guidelines" />, |
| 78 | } |
| 79 | ); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return ( |
| 84 | <Message { ...props } severity={ severity }> |
| 85 | { messageText } |
| 86 | </Message> |
| 87 | ); |
| 88 | } |
| 89 |