2022-05-26 07:49:30 +03:00
|
|
|
/* eslint-disable react/no-danger */
|
2022-11-19 04:08:46 +03:00
|
|
|
import { FC, ReactNode, useEffect, useState } from 'react';
|
2022-06-25 07:30:54 +03:00
|
|
|
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-10-04 07:06:46 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
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 { ChatUserBadge } from '../ChatUserBadge/ChatUserBadge';
|
2022-09-05 03:58:06 +03:00
|
|
|
import { accessTokenAtom } from '../../stores/ClientConfigStore';
|
2022-11-19 04:08:46 +03:00
|
|
|
import { User } from '../../../interfaces/user.model';
|
2022-04-28 09:19:20 +03:00
|
|
|
|
2022-10-04 07:06:46 +03: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 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-11-19 04:08:46 +03:00
|
|
|
export type UserTooltipProps = {
|
|
|
|
user: User;
|
|
|
|
children: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
const UserTooltip: FC<UserTooltipProps> = ({ children, user }) => {
|
|
|
|
const { displayName, createdAt } = user;
|
|
|
|
const content = `${displayName} first joined ${formatTimestamp(createdAt)}`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip title={content} placement="topLeft" mouseEnterDelay={1}>
|
|
|
|
{children}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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-10-28 09:20:06 +03: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 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-12-13 09:54:29 +03:00
|
|
|
<div
|
|
|
|
className={cn(
|
|
|
|
styles.messagePadding,
|
|
|
|
sameUserAsLast && styles.messagePaddingCollapsed,
|
|
|
|
'chat-message_user',
|
|
|
|
)}
|
|
|
|
>
|
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-11-19 04:08:46 +03:00
|
|
|
<UserTooltip user={user}>
|
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>
|
2022-11-19 04:08:46 +03:00
|
|
|
</UserTooltip>
|
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.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
|
|
|
};
|