|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { TestableComponent } from "../../components/interfaces"; |
| 4 | +import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 5 | + |
| 6 | +import { Markdown, MarkdownProps } from "./../../cmem/markdown/Markdown"; |
| 7 | +import { DepictionProps } from "./../Depiction/Depiction"; |
| 8 | +import { FlexibleLayoutContainer, FlexibleLayoutItem } from "./../FlexibleLayout"; |
| 9 | +import { Spacing } from "./../Separation/Spacing"; |
| 10 | +import { HtmlContentBlock, OverflowTextProps, WhiteSpaceContainer } from "./../Typography"; |
| 11 | + |
| 12 | +export interface ChatContentProps extends React.HTMLAttributes<HTMLDivElement>, TestableComponent { |
| 13 | + /** |
| 14 | + * Should be a line of text, e.g. username, timestamp, ... |
| 15 | + */ |
| 16 | + statusLine?: React.ReactElement<OverflowTextProps>; |
| 17 | + /** |
| 18 | + * How the chat content box is displayed. |
| 19 | + */ |
| 20 | + displayType?: "free" | "simple" | "bubble"; |
| 21 | + /** |
| 22 | + * A depiction used as avatar next to the content box. |
| 23 | + */ |
| 24 | + avatar?: React.ReactElement<DepictionProps>; |
| 25 | + /** |
| 26 | + * If indented then the content box has some white space on one side. |
| 27 | + */ |
| 28 | + indentation?: boolean; |
| 29 | + /** |
| 30 | + * How the content box and avatar is aligned. |
| 31 | + * If `left` is set then the avatar is on the left side, and the indentation on the right side. |
| 32 | + */ |
| 33 | + alignment?: "left" | "right" | "block"; |
| 34 | + /** |
| 35 | + * If given then the content is automatically parsed and displayed by our `<Markdown />` component. |
| 36 | + * `children` need to a `string` then, otherwise it cannot be parsed. |
| 37 | + */ |
| 38 | + markdownProps?: Omit<MarkdownProps, "children">; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Component to display singe chat contents, including avatar and status line. |
| 43 | + */ |
| 44 | +export const ChatContent = ({ |
| 45 | + className, |
| 46 | + children, |
| 47 | + statusLine, |
| 48 | + avatar, |
| 49 | + displayType = "bubble", |
| 50 | + indentation = true, |
| 51 | + alignment = "left", |
| 52 | + markdownProps, |
| 53 | + ...otherDivProps |
| 54 | +}: ChatContentProps) => { |
| 55 | + const content = ( |
| 56 | + <div |
| 57 | + className={ |
| 58 | + `${eccgui}-chat__content` + |
| 59 | + ` ${eccgui}-chat__content--display-${displayType}` + |
| 60 | + ` ${eccgui}-chat__content--align-${alignment}` + |
| 61 | + (className ? ` ${className}` : "") |
| 62 | + } |
| 63 | + {...otherDivProps} |
| 64 | + > |
| 65 | + {statusLine && ( |
| 66 | + <HtmlContentBlock small> |
| 67 | + {statusLine} |
| 68 | + <Spacing size="small" /> |
| 69 | + </HtmlContentBlock> |
| 70 | + )} |
| 71 | + {markdownProps && typeof children === "string" ? ( |
| 72 | + <Markdown {...markdownProps}>{children}</Markdown> |
| 73 | + ) : ( |
| 74 | + children |
| 75 | + )} |
| 76 | + </div> |
| 77 | + ); |
| 78 | + |
| 79 | + return ( |
| 80 | + <WhiteSpaceContainer |
| 81 | + marginLeft={alignment === "right" && indentation ? "xlarge" : undefined} |
| 82 | + marginRight={alignment === "left" && indentation ? "xlarge" : undefined} |
| 83 | + > |
| 84 | + {avatar ? ( |
| 85 | + <FlexibleLayoutContainer noEqualItemSpace gapSize="tiny"> |
| 86 | + <FlexibleLayoutItem |
| 87 | + growFactor={0} |
| 88 | + shrinkFactor={0} |
| 89 | + style={alignment === "right" ? { order: 1 } : undefined} |
| 90 | + > |
| 91 | + {React.cloneElement(avatar, { size: "small", ratio: "1:1", rounded: true, resizing: "cover" })} |
| 92 | + </FlexibleLayoutItem> |
| 93 | + <FlexibleLayoutItem>{content}</FlexibleLayoutItem> |
| 94 | + </FlexibleLayoutContainer> |
| 95 | + ) : ( |
| 96 | + content |
| 97 | + )} |
| 98 | + </WhiteSpaceContainer> |
| 99 | + ); |
| 100 | +}; |
| 101 | + |
| 102 | +export default ChatContent; |
0 commit comments