import React, { useState, useEffect } from 'react'; import moment from 'moment'; import './default.scss'; export interface ChatMessageProps { item: Connector.ChatItemOptions style?: any dateFormat?: string show?: ChatItemShow label?: ChatItemLabel labelSeparator?: string } export type ChatItemShow = Record export type ChatItemLabel = Record export const ChatMessage: React.FC = ({ item, style = {}, dateFormat = 'M/D/YYYY h:mmA', show = { sentAt: true, displayName: true, content: true, }, label = { contentType: true, sentAt: show.readAt ? true : false, readAt: true }, labelSeparator = ': ' }) => { const [msg, setMsg] = useState(item); useEffect(() => { const sentAt = moment(item.sentAt).format(dateFormat); const readAt = item.readAt ? moment(item.readAt).format(dateFormat) : undefined; const direction = item.direction?.toLowerCase() || ''; setMsg({ content: item.content, contentType: item.contentType, displayName: item.participant?.displayName || 'UNKNOWN', // TODO: Add support for attachments //attachments: item.attachments, sentAt, readAt, direction }) },[item]); const attrs = { item: msg, show, label, separator: labelSeparator } return (
) } const Tag = ({name, children = '', ...props }) => { const Name = name; return ({children}) } const LabeledField = ({ label, show, item, field, tagName = 'span', separator = '' }) => { const [labelText, setLabelText] = useState(label[field] || ''); useEffect(() => { if (label[field] && typeof label[field] === 'boolean') { const txt = field.substring(0,1).toUpperCase() + field.substring(1).replaceAll(/[A-Z]/g, m => ` ${m}`); setLabelText(txt); } },[]); return show[field] ? (
{ label[field] && ( {labelText}{separator} )} {item[field]}
) : null }