2022-05-26 06:49:30 +02:00
|
|
|
/* eslint-disable react/no-danger */
|
2022-09-07 09:00:28 +02:00
|
|
|
import { FC, useEffect, useState } from 'react';
|
2022-06-25 06:30:54 +02:00
|
|
|
import he from 'he';
|
2022-06-28 09:05:04 +02:00
|
|
|
import cn from 'classnames';
|
2022-08-22 02:22:24 +02:00
|
|
|
import { Tooltip } from 'antd';
|
2022-09-02 06:41:33 +02:00
|
|
|
import { LinkOutlined } from '@ant-design/icons';
|
2022-09-05 02:58:06 +02:00
|
|
|
import { useRecoilValue } from 'recoil';
|
2022-10-04 06:06:46 +02:00
|
|
|
import dynamic from 'next/dynamic';
|
2022-09-07 09:00:28 +02:00
|
|
|
import styles from './ChatUserMessage.module.scss';
|
2022-07-01 22:49:42 +02:00
|
|
|
import { formatTimestamp } from './messageFmt';
|
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
2022-09-07 09:00:28 +02:00
|
|
|
import { ChatUserBadge } from '../ChatUserBadge/ChatUserBadge';
|
2022-09-05 02:58:06 +02:00
|
|
|
import { accessTokenAtom } from '../../stores/ClientConfigStore';
|
2022-04-28 08:19:20 +02:00
|
|
|
|
2022-10-04 06:06:46 +02:00
|
|
|
// Lazy loaded components
|
|
|
|
|
|
|
|
const ChatModerationActionMenu = dynamic(() =>
|
|
|
|
import('../ChatModerationActionMenu/ChatModerationActionMenu').then(
|
|
|
|
mod => mod.ChatModerationActionMenu,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
const Highlight = dynamic(() => import('react-highlighter-ts').then(mod => mod.Highlight));
|
|
|
|
|
2022-09-07 09:00:28 +02:00
|
|
|
export type ChatUserMessageProps = {
|
2022-04-28 08:19:20 +02:00
|
|
|
message: ChatMessage;
|
2022-04-30 00:09:53 +02:00
|
|
|
showModeratorMenu: boolean;
|
2022-06-25 06:30:54 +02:00
|
|
|
highlightString: string;
|
2022-07-01 22:49:42 +02:00
|
|
|
sentBySelf: boolean;
|
|
|
|
sameUserAsLast: boolean;
|
2022-08-11 06:41:56 +02:00
|
|
|
isAuthorModerator: boolean;
|
2022-08-21 23:04:16 +02:00
|
|
|
isAuthorAuthenticated: boolean;
|
2022-09-07 09:00:28 +02:00
|
|
|
};
|
2022-04-28 08:19:20 +02:00
|
|
|
|
2022-09-07 09:00:28 +02:00
|
|
|
export const ChatUserMessage: FC<ChatUserMessageProps> = ({
|
2022-06-25 06:30:54 +02:00
|
|
|
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-11 06:41:56 +02:00
|
|
|
isAuthorModerator,
|
2022-08-21 23:04:16 +02:00
|
|
|
isAuthorAuthenticated,
|
2022-09-07 09:00:28 +02:00
|
|
|
}) => {
|
2022-08-11 07:13:48 +02:00
|
|
|
const { id: messageId, body, user, timestamp } = message;
|
|
|
|
const { id: userId, displayName, displayColor } = user;
|
2022-09-05 02:58:06 +02:00
|
|
|
const accessToken = useRecoilValue<string>(accessTokenAtom);
|
2022-06-29 08:22:22 +02:00
|
|
|
|
2022-09-02 04:37:11 +02:00
|
|
|
const color = `var(--theme-color-users-${displayColor})`;
|
2022-08-22 02:22:24 +02:00
|
|
|
const formattedTimestamp = `Sent ${formatTimestamp(timestamp)}`;
|
2022-05-24 08:47:22 +02:00
|
|
|
const [formattedMessage, setFormattedMessage] = useState<string>(body);
|
|
|
|
|
2022-09-02 06:41:33 +02: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-21 23:04:16 +02:00
|
|
|
|
2022-10-28 08:20:06 +02:00
|
|
|
// TODO: Find a solution to get rid of or replace "he" library since
|
|
|
|
// it's overly large for only use in this one place.
|
2022-05-24 08:47:22 +02:00
|
|
|
useEffect(() => {
|
2022-06-25 06:30:54 +02:00
|
|
|
setFormattedMessage(he.decode(body));
|
2022-05-24 08:47:22 +02:00
|
|
|
}, [message]);
|
2022-04-30 00:09:53 +02:00
|
|
|
|
|
|
|
return (
|
2022-09-07 09:00:28 +02:00
|
|
|
<div className={cn(styles.messagePadding, sameUserAsLast && styles.messagePaddingCollapsed)}>
|
2022-07-01 19:35:14 +02:00
|
|
|
<div
|
2022-09-07 09:00:28 +02:00
|
|
|
className={cn(styles.root, {
|
|
|
|
[styles.ownMessage]: sentBySelf,
|
2022-07-01 19:35:14 +02:00
|
|
|
})}
|
|
|
|
style={{ borderColor: color }}
|
|
|
|
>
|
2022-07-01 22:49:42 +02:00
|
|
|
{!sameUserAsLast && (
|
2022-08-22 02:22:24 +02:00
|
|
|
<Tooltip title="user info goes here" placement="topLeft" mouseEnterDelay={1}>
|
2022-09-07 09:00:28 +02:00
|
|
|
<div className={styles.user} style={{ color }}>
|
|
|
|
<span className={styles.userName}>{displayName}</span>
|
2022-09-02 06:41:33 +02:00
|
|
|
<span>{badgeNodes}</span>
|
2022-08-22 02:22:24 +02:00
|
|
|
</div>
|
|
|
|
</Tooltip>
|
2022-07-01 22:49:42 +02:00
|
|
|
)}
|
2022-08-22 02:22:24 +02:00
|
|
|
<Tooltip title={formattedTimestamp} mouseEnterDelay={1}>
|
|
|
|
<Highlight search={highlightString}>
|
2022-09-07 09:00:28 +02:00
|
|
|
<div
|
|
|
|
className={styles.message}
|
|
|
|
dangerouslySetInnerHTML={{ __html: formattedMessage }}
|
|
|
|
/>
|
2022-08-22 02:22:24 +02:00
|
|
|
</Highlight>
|
|
|
|
</Tooltip>
|
|
|
|
|
2022-08-11 07:13:48 +02:00
|
|
|
{showModeratorMenu && (
|
2022-09-07 09:00:28 +02:00
|
|
|
<div className={styles.modMenuWrapper}>
|
2022-08-11 07:13:48 +02:00
|
|
|
<ChatModerationActionMenu
|
|
|
|
messageID={messageId}
|
2022-09-05 02:58:06 +02:00
|
|
|
accessToken={accessToken}
|
2022-08-11 07:13:48 +02:00
|
|
|
userID={userId}
|
|
|
|
userDisplayName={displayName}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-09-07 09:00:28 +02:00
|
|
|
<div className={styles.background} style={{ color }} />
|
2022-05-22 16:10:34 +02:00
|
|
|
</div>
|
2022-04-30 00:09:53 +02:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 09:00:28 +02:00
|
|
|
};
|