2022-05-25 21:49:30 -07:00
|
|
|
/* eslint-disable react/no-danger */
|
2022-05-24 08:47:22 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
2022-06-24 21:30:54 -07:00
|
|
|
import { Highlight } from 'react-highlighter-ts';
|
|
|
|
import he from 'he';
|
2022-06-28 09:05:04 +02:00
|
|
|
import cn from 'classnames';
|
2022-08-21 17:22:24 -07:00
|
|
|
import { Tooltip } from 'antd';
|
2022-05-22 16:10:34 +02:00
|
|
|
import s from './ChatUserMessage.module.scss';
|
2022-07-01 22:49:42 +02:00
|
|
|
import { formatTimestamp } from './messageFmt';
|
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
2022-08-10 22:13:48 -07:00
|
|
|
import ChatModerationActionMenu from '../ChatModerationActionMenu/ChatModerationActionMenu';
|
2022-08-21 15:50:22 -07:00
|
|
|
import ChatUserBadge from '../ChatUserBadge/ChatUserBadge';
|
2022-04-27 23:19:20 -07:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
message: ChatMessage;
|
2022-04-29 15:09:53 -07:00
|
|
|
showModeratorMenu: boolean;
|
2022-06-24 21:30:54 -07:00
|
|
|
highlightString: string;
|
2022-07-01 22:49:42 +02:00
|
|
|
sentBySelf: boolean;
|
|
|
|
sameUserAsLast: boolean;
|
2022-08-10 21:41:56 -07:00
|
|
|
isAuthorModerator: boolean;
|
2022-08-21 14:04:16 -07:00
|
|
|
isAuthorAuthenticated: boolean;
|
2022-04-27 23:19:20 -07:00
|
|
|
}
|
|
|
|
|
2022-06-24 21:30:54 -07:00
|
|
|
export default function ChatUserMessage({
|
|
|
|
message,
|
|
|
|
highlightString,
|
|
|
|
showModeratorMenu,
|
2022-07-01 22:49:42 +02:00
|
|
|
sentBySelf, // Move the border to the right and render a background
|
|
|
|
sameUserAsLast,
|
2022-08-10 21:41:56 -07:00
|
|
|
isAuthorModerator,
|
2022-08-21 14:04:16 -07:00
|
|
|
isAuthorAuthenticated,
|
2022-06-24 21:30:54 -07:00
|
|
|
}: Props) {
|
2022-08-10 22:13:48 -07:00
|
|
|
const { id: messageId, body, user, timestamp } = message;
|
|
|
|
const { id: userId, displayName, displayColor } = user;
|
2022-06-29 08:22:22 +02:00
|
|
|
|
2022-09-01 19:37:11 -07:00
|
|
|
const color = `var(--theme-color-users-${displayColor})`;
|
2022-08-21 17:22:24 -07:00
|
|
|
const formattedTimestamp = `Sent ${formatTimestamp(timestamp)}`;
|
2022-05-24 08:47:22 +02:00
|
|
|
const [formattedMessage, setFormattedMessage] = useState<string>(body);
|
|
|
|
|
2022-08-21 14:04:16 -07:00
|
|
|
const badgeStrings = [isAuthorModerator && 'mod', isAuthorAuthenticated && 'auth'];
|
|
|
|
const badges = badgeStrings
|
|
|
|
.filter(badge => !!badge)
|
|
|
|
.map(badge => <ChatUserBadge key={badge} badge={badge} userColor={displayColor} />);
|
|
|
|
|
2022-05-24 08:47:22 +02:00
|
|
|
useEffect(() => {
|
2022-06-24 21:30:54 -07:00
|
|
|
setFormattedMessage(he.decode(body));
|
2022-05-24 08:47:22 +02:00
|
|
|
}, [message]);
|
2022-04-29 15:09:53 -07:00
|
|
|
|
|
|
|
return (
|
2022-08-21 17:22:24 -07:00
|
|
|
<div className={cn(s.messagePadding, sameUserAsLast && s.messagePaddingCollapsed)}>
|
2022-07-01 19:35:14 +02:00
|
|
|
<div
|
|
|
|
className={cn(s.root, {
|
2022-07-01 22:49:42 +02:00
|
|
|
[s.ownMessage]: sentBySelf,
|
2022-07-01 19:35:14 +02:00
|
|
|
})}
|
|
|
|
style={{ borderColor: color }}
|
|
|
|
>
|
2022-07-01 22:49:42 +02:00
|
|
|
{!sameUserAsLast && (
|
2022-08-21 17:22:24 -07:00
|
|
|
<Tooltip title="user info goes here" placement="topLeft" mouseEnterDelay={1}>
|
|
|
|
<div className={s.user} style={{ color }}>
|
|
|
|
<span className={s.userName}>{displayName}</span>
|
|
|
|
<span>{badges}</span>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
2022-07-01 22:49:42 +02:00
|
|
|
)}
|
2022-08-21 17:22:24 -07:00
|
|
|
<Tooltip title={formattedTimestamp} mouseEnterDelay={1}>
|
|
|
|
<Highlight search={highlightString}>
|
|
|
|
<div className={s.message}>{formattedMessage}</div>
|
|
|
|
</Highlight>
|
|
|
|
</Tooltip>
|
|
|
|
|
2022-08-10 22:13:48 -07:00
|
|
|
{showModeratorMenu && (
|
|
|
|
<div className={s.modMenuWrapper}>
|
|
|
|
<ChatModerationActionMenu
|
|
|
|
messageID={messageId}
|
|
|
|
accessToken=""
|
|
|
|
userID={userId}
|
|
|
|
userDisplayName={displayName}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-07-01 19:35:14 +02:00
|
|
|
<div className={s.customBorder} style={{ color }} />
|
|
|
|
<div className={s.background} style={{ color }} />
|
2022-05-22 16:10:34 +02:00
|
|
|
</div>
|
2022-04-29 15:09:53 -07:00
|
|
|
</div>
|
|
|
|
);
|
2022-04-27 23:19:20 -07:00
|
|
|
}
|