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