2023-02-05 04:21:06 +03:00
|
|
|
import { FC, ReactNode } from 'react';
|
2022-06-28 10:05:04 +03:00
|
|
|
import cn from 'classnames';
|
2023-01-11 03:39:12 +03:00
|
|
|
import { Tooltip } from 'antd';
|
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';
|
2023-02-05 04:21:06 +03:00
|
|
|
import { Interweave } from 'interweave';
|
|
|
|
import { UrlMatcher } from 'interweave-autolink';
|
|
|
|
import { ChatMessageHighlightMatcher } from './customMatcher';
|
2023-07-06 22:47:38 +03:00
|
|
|
import { ChatMessageEmojiMatcher } from './emojiMatcher';
|
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-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';
|
2023-01-30 02:39:50 +03:00
|
|
|
import { AuthedUserBadge } from '../ChatUserBadge/AuthedUserBadge';
|
|
|
|
import { ModerationBadge } from '../ChatUserBadge/ModerationBadge';
|
2023-02-06 02:37:16 +03:00
|
|
|
import { BotUserBadge } from '../ChatUserBadge/BotUserBadge';
|
2022-04-28 09:19:20 +03:00
|
|
|
|
2022-10-04 07:06:46 +03:00
|
|
|
// Lazy loaded components
|
|
|
|
|
2023-01-11 03:39:12 +03:00
|
|
|
const ChatModerationActionMenu = dynamic(
|
|
|
|
() =>
|
|
|
|
import('../ChatModerationActionMenu/ChatModerationActionMenu').then(
|
|
|
|
mod => mod.ChatModerationActionMenu,
|
|
|
|
),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
},
|
2022-10-04 07:06:46 +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;
|
2023-02-06 02:37:16 +03:00
|
|
|
isAuthorBot: 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,
|
2023-02-06 02:37:16 +03:00
|
|
|
isAuthorBot,
|
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
|
|
|
|
2022-09-02 07:41:33 +03:00
|
|
|
const badgeNodes = [];
|
|
|
|
if (isAuthorModerator) {
|
2023-01-30 02:39:50 +03:00
|
|
|
badgeNodes.push(<ModerationBadge key="mod" userColor={displayColor} />);
|
2022-09-02 07:41:33 +03:00
|
|
|
}
|
|
|
|
if (isAuthorAuthenticated) {
|
2023-01-30 02:39:50 +03:00
|
|
|
badgeNodes.push(<AuthedUserBadge key="auth" userColor={displayColor} />);
|
2022-09-02 07:41:33 +03:00
|
|
|
}
|
2023-02-06 02:37:16 +03:00
|
|
|
if (isAuthorBot) {
|
|
|
|
badgeNodes.push(<BotUserBadge key="bot" userColor={displayColor} />);
|
|
|
|
}
|
2023-06-26 21:37:34 +03:00
|
|
|
|
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 }}
|
|
|
|
>
|
2023-01-27 23:54:43 +03:00
|
|
|
<div className={styles.background} style={{ color }} />
|
|
|
|
|
2023-06-28 19:58:07 +03:00
|
|
|
<UserTooltip user={user}>
|
|
|
|
<div className={sameUserAsLast ? styles.repeatUser : styles.user} style={{ color }}>
|
|
|
|
<span className={styles.userName}>{displayName}</span>
|
|
|
|
<span className={styles.userBadges}>{badgeNodes}</span>
|
|
|
|
</div>
|
|
|
|
</UserTooltip>
|
2022-08-22 03:22:24 +03:00
|
|
|
<Tooltip title={formattedTimestamp} mouseEnterDelay={1}>
|
2023-02-05 04:21:06 +03:00
|
|
|
<Interweave
|
|
|
|
className={styles.message}
|
|
|
|
content={body}
|
|
|
|
matchers={[
|
2023-06-15 15:40:59 +03:00
|
|
|
new UrlMatcher('url', { customTLDs: ['online'] }),
|
2023-02-05 04:21:06 +03:00
|
|
|
new ChatMessageHighlightMatcher('highlight', { highlightString }),
|
2023-07-06 22:47:38 +03:00
|
|
|
new ChatMessageEmojiMatcher('emoji', { className: 'emoji' }),
|
2023-02-05 04:21:06 +03:00
|
|
|
]}
|
|
|
|
/>
|
2022-08-22 03:22:24 +03:00
|
|
|
</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-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
|
|
|
};
|