|
| 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 { ContextMenuProps } from "./../ContextOverlay/ContextMenu"; |
| 8 | +import { DepictionProps } from "./../Depiction/Depiction"; |
| 9 | +import { FlexibleLayoutContainer, FlexibleLayoutItem } from "./../FlexibleLayout"; |
| 10 | +import { IconButtonProps } from "./../Icon/IconButton"; |
| 11 | +import { Spacing } from "./../Separation/Spacing"; |
| 12 | +import { HtmlContentBlock, OverflowTextProps } from "./../Typography"; |
| 13 | + |
| 14 | +export interface ChatContentProps extends React.HTMLAttributes<HTMLDivElement>, TestableComponent { |
| 15 | + /** |
| 16 | + * Should be a line of text, e.g. username, timestamp, ... |
| 17 | + */ |
| 18 | + statusLine?: React.ReactElement<OverflowTextProps>; |
| 19 | + /** |
| 20 | + * How the chat content box is displayed. |
| 21 | + */ |
| 22 | + displayType?: "free" | "simple" | "bubble"; |
| 23 | + /** |
| 24 | + * A depiction used as avatar next to the content box. |
| 25 | + */ |
| 26 | + avatar?: React.ReactElement<DepictionProps>; |
| 27 | + /** |
| 28 | + * If indented then the content box has some white space on the opposite side to the alignment |
| 29 | + */ |
| 30 | + indentationSize?: "small" | "medium" | "large"; |
| 31 | + /** |
| 32 | + * How the content box and avatar is aligned. |
| 33 | + * If `left` is set then the avatar is on the left side, and the indentation on the right side. |
| 34 | + */ |
| 35 | + alignment?: "left" | "right"; |
| 36 | + /** |
| 37 | + * If set then the chat bubble only grows to a height of 50% of the viewport. |
| 38 | + * In case you need to set other maximum heights then use the `style` property directly. |
| 39 | + */ |
| 40 | + limitHeight?: boolean; |
| 41 | + /** |
| 42 | + * If given then the content is automatically parsed and displayed by our `<Markdown />` component. |
| 43 | + * `children` need to a `string` then, otherwise it cannot be parsed. |
| 44 | + */ |
| 45 | + markdownProps?: Omit<MarkdownProps, "children">; |
| 46 | + /** |
| 47 | + * Could be used to add some type of toggle button or to include a context menu. |
| 48 | + */ |
| 49 | + actionButton?: React.ReactElement<IconButtonProps> | React.ReactElement<ContextMenuProps>; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Component to display single chat contents, including avatar and status line. |
| 54 | + */ |
| 55 | +export const ChatContent = ({ |
| 56 | + className, |
| 57 | + children, |
| 58 | + statusLine, |
| 59 | + avatar, |
| 60 | + displayType = "bubble", |
| 61 | + indentationSize, |
| 62 | + alignment = "left", |
| 63 | + limitHeight, |
| 64 | + markdownProps, |
| 65 | + actionButton, |
| 66 | + ...otherDivProps |
| 67 | +}: ChatContentProps) => { |
| 68 | + const content = |
| 69 | + markdownProps && typeof children === "string" ? <Markdown {...markdownProps}>{children}</Markdown> : children; |
| 70 | + |
| 71 | + const chatitem = ( |
| 72 | + <div |
| 73 | + className={ |
| 74 | + `${eccgui}-chat__content` + |
| 75 | + ` ${eccgui}-chat__content--display-${displayType}` + |
| 76 | + ` ${eccgui}-chat__content--align-${alignment}` + |
| 77 | + (limitHeight ? ` ${eccgui}-chat__content--limitheight` : "") + |
| 78 | + (className ? ` ${className}` : "") |
| 79 | + } |
| 80 | + {...otherDivProps} |
| 81 | + > |
| 82 | + {statusLine && ( |
| 83 | + <HtmlContentBlock small> |
| 84 | + {statusLine} |
| 85 | + <Spacing size="tiny" /> |
| 86 | + </HtmlContentBlock> |
| 87 | + )} |
| 88 | + {content} |
| 89 | + </div> |
| 90 | + ); |
| 91 | + |
| 92 | + const indentationSizes = { |
| 93 | + small: "8%", |
| 94 | + medium: "21%", |
| 95 | + large: "34%", |
| 96 | + }; |
| 97 | + |
| 98 | + return ( |
| 99 | + <div |
| 100 | + style={{ |
| 101 | + marginLeft: alignment === "right" && indentationSize ? indentationSizes[indentationSize] : undefined, |
| 102 | + marginRight: alignment === "left" && indentationSize ? indentationSizes[indentationSize] : undefined, |
| 103 | + }} |
| 104 | + > |
| 105 | + <FlexibleLayoutContainer noEqualItemSpace gapSize="tiny"> |
| 106 | + {avatar && ( |
| 107 | + <FlexibleLayoutItem |
| 108 | + className={`${eccgui}-chat__content-avatar`} |
| 109 | + growFactor={0} |
| 110 | + shrinkFactor={0} |
| 111 | + style={alignment === "right" ? { order: 1 } : undefined} |
| 112 | + > |
| 113 | + {React.cloneElement(avatar, { size: "small", ratio: "1:1", rounded: true, resizing: "cover" })} |
| 114 | + </FlexibleLayoutItem> |
| 115 | + )} |
| 116 | + <FlexibleLayoutItem className={`${eccgui}-chat__content-wrapper`}>{chatitem}</FlexibleLayoutItem> |
| 117 | + {actionButton && ( |
| 118 | + <FlexibleLayoutItem |
| 119 | + className={`${eccgui}-chat__content-sizetoggle`} |
| 120 | + growFactor={0} |
| 121 | + shrinkFactor={0} |
| 122 | + style={alignment === "right" ? { order: -1 } : undefined} |
| 123 | + > |
| 124 | + {actionButton} |
| 125 | + </FlexibleLayoutItem> |
| 126 | + )} |
| 127 | + </FlexibleLayoutContainer> |
| 128 | + </div> |
| 129 | + ); |
| 130 | +}; |
| 131 | + |
| 132 | +export default ChatContent; |
0 commit comments