|
| 1 | +import {Fragment} from 'react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | + |
| 4 | +// Match the Python types from notifications/platform/types.py |
| 5 | +enum NotificationBodyFormattingBlockType { |
| 6 | + PARAGRAPH = 'paragraph', |
| 7 | + CODE_BLOCK = 'code_block', |
| 8 | +} |
| 9 | + |
| 10 | +enum NotificationBodyTextBlockType { |
| 11 | + PLAIN_TEXT = 'plain_text', |
| 12 | + BOLD_TEXT = 'bold_text', |
| 13 | + CODE = 'code', |
| 14 | +} |
| 15 | + |
| 16 | +interface NotificationBodyTextBlock { |
| 17 | + text: string; |
| 18 | + type: NotificationBodyTextBlockType; |
| 19 | +} |
| 20 | + |
| 21 | +export interface NotificationBodyFormattingBlock { |
| 22 | + blocks: NotificationBodyTextBlock[]; |
| 23 | + type: NotificationBodyFormattingBlockType; |
| 24 | +} |
| 25 | + |
| 26 | +interface NotificationBodyRendererProps { |
| 27 | + body: NotificationBodyFormattingBlock[]; |
| 28 | + codeBlockBackground?: string; |
| 29 | + codeBlockBorder?: string; |
| 30 | + codeBlockTextColor?: string; |
| 31 | +} |
| 32 | + |
| 33 | +function renderTextBlock(block: NotificationBodyTextBlock, index: number) { |
| 34 | + switch (block.type) { |
| 35 | + case NotificationBodyTextBlockType.PLAIN_TEXT: |
| 36 | + return <Fragment key={index}>{block.text} </Fragment>; |
| 37 | + case NotificationBodyTextBlockType.BOLD_TEXT: |
| 38 | + return <strong key={index}>{block.text} </strong>; |
| 39 | + case NotificationBodyTextBlockType.CODE: |
| 40 | + return <code key={index}>{block.text} </code>; |
| 41 | + default: |
| 42 | + return <Fragment key={index}>{block.text} </Fragment>; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function renderFormattingBlock( |
| 47 | + block: NotificationBodyFormattingBlock, |
| 48 | + index: number, |
| 49 | + codeBlockBg: string, |
| 50 | + codeBlockBorder: string, |
| 51 | + codeBlockTextColor: string |
| 52 | +) { |
| 53 | + if (block.type === NotificationBodyFormattingBlockType.PARAGRAPH) { |
| 54 | + return ( |
| 55 | + <div key={index} style={{marginBottom: '8px'}}> |
| 56 | + {block.blocks.map((textBlock, i) => renderTextBlock(textBlock, i))} |
| 57 | + </div> |
| 58 | + ); |
| 59 | + } |
| 60 | + if (block.type === NotificationBodyFormattingBlockType.CODE_BLOCK) { |
| 61 | + return ( |
| 62 | + <div key={index} style={{marginBottom: '8px'}}> |
| 63 | + <StyledCodeBlock |
| 64 | + backgroundColor={codeBlockBg} |
| 65 | + borderColor={codeBlockBorder} |
| 66 | + textColor={codeBlockTextColor} |
| 67 | + > |
| 68 | + {block.blocks.map((textBlock, i) => renderTextBlock(textBlock, i))} |
| 69 | + </StyledCodeBlock> |
| 70 | + </div> |
| 71 | + ); |
| 72 | + } |
| 73 | + return null; |
| 74 | +} |
| 75 | + |
| 76 | +const StyledCodeBlock = styled('code')<{ |
| 77 | + backgroundColor: string; |
| 78 | + borderColor: string; |
| 79 | + textColor: string; |
| 80 | +}>` |
| 81 | + display: block; |
| 82 | + padding: 12px; |
| 83 | + background-color: ${p => p.backgroundColor}; |
| 84 | + border: 1px solid ${p => p.borderColor}; |
| 85 | + border-radius: 6px; |
| 86 | + font-family: monospace; |
| 87 | + font-size: 13px; |
| 88 | + white-space: pre-wrap; |
| 89 | + word-break: break-word; |
| 90 | + color: ${p => p.textColor}; |
| 91 | +`; |
| 92 | + |
| 93 | +export function NotificationBodyRenderer({ |
| 94 | + body, |
| 95 | + codeBlockBackground = '#f6f8fa', |
| 96 | + codeBlockBorder = '#e1e4e8', |
| 97 | + codeBlockTextColor = '#24292e', |
| 98 | +}: NotificationBodyRendererProps) { |
| 99 | + return ( |
| 100 | + <div> |
| 101 | + {body.map((block, index) => |
| 102 | + renderFormattingBlock( |
| 103 | + block, |
| 104 | + index, |
| 105 | + codeBlockBackground, |
| 106 | + codeBlockBorder, |
| 107 | + codeBlockTextColor |
| 108 | + ) |
| 109 | + )} |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
0 commit comments