2022-05-26 07:49:30 +03:00
|
|
|
/* eslint-disable react/no-danger */
|
2022-05-24 09:47:22 +03:00
|
|
|
import { 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-07-01 23:49:42 +03:00
|
|
|
import { Button, Dropdown, Menu } from 'antd';
|
|
|
|
import { DeleteOutlined, EllipsisOutlined, StopOutlined } from '@ant-design/icons';
|
2022-05-22 17:10:34 +03:00
|
|
|
import s from './ChatUserMessage.module.scss';
|
2022-07-01 23:49:42 +03:00
|
|
|
import { formatTimestamp } from './messageFmt';
|
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
2022-07-02 10:08:36 +03:00
|
|
|
import { ModIcon } from '../../ui';
|
2022-04-28 09:19:20 +03:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
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-04-28 09:19:20 +03:00
|
|
|
}
|
|
|
|
|
2022-06-25 07:30:54 +03:00
|
|
|
export default function ChatUserMessage({
|
|
|
|
message,
|
|
|
|
highlightString,
|
|
|
|
showModeratorMenu,
|
2022-07-01 23:49:42 +03:00
|
|
|
sentBySelf, // Move the border to the right and render a background
|
|
|
|
sameUserAsLast,
|
2022-06-25 07:30:54 +03:00
|
|
|
}: Props) {
|
2022-04-30 01:09:53 +03:00
|
|
|
const { body, user, timestamp } = message;
|
|
|
|
const { displayName, displayColor } = user;
|
2022-06-29 09:22:22 +03:00
|
|
|
|
2022-06-29 04:55:21 +03:00
|
|
|
const color = `var(--theme-user-colors-${displayColor})`;
|
2022-05-24 09:47:22 +03:00
|
|
|
const formattedTimestamp = `Sent at ${formatTimestamp(timestamp)}`;
|
|
|
|
const [formattedMessage, setFormattedMessage] = useState<string>(body);
|
|
|
|
|
|
|
|
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-07-01 23:49:42 +03:00
|
|
|
<div style={{ padding: 3.5 }}>
|
2022-07-01 20:35:14 +03:00
|
|
|
<div
|
|
|
|
className={cn(s.root, {
|
2022-07-01 23:49:42 +03:00
|
|
|
[s.ownMessage]: sentBySelf,
|
2022-07-01 20:35:14 +03:00
|
|
|
})}
|
|
|
|
style={{ borderColor: color }}
|
|
|
|
title={formattedTimestamp}
|
|
|
|
>
|
2022-07-01 23:49:42 +03:00
|
|
|
{!sameUserAsLast && (
|
|
|
|
<div className={s.user} style={{ color }}>
|
2022-07-02 10:56:03 +03:00
|
|
|
{showModeratorMenu && <ModIcon />}
|
2022-07-02 10:08:36 +03:00
|
|
|
<span className={s.userName}>{displayName}</span>
|
2022-07-01 23:49:42 +03:00
|
|
|
</div>
|
|
|
|
)}
|
2022-07-01 20:35:14 +03:00
|
|
|
<Highlight search={highlightString}>
|
|
|
|
<div className={s.message}>{formattedMessage}</div>
|
|
|
|
</Highlight>
|
2022-07-01 23:49:42 +03:00
|
|
|
{showModeratorMenu && <ModeratorMenu />}
|
2022-07-01 20:35:14 +03:00
|
|
|
<div className={s.customBorder} style={{ color }} />
|
|
|
|
<div className={s.background} style={{ color }} />
|
2022-05-22 17:10:34 +03:00
|
|
|
</div>
|
2022-04-30 01:09:53 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|
2022-07-01 23:49:42 +03:00
|
|
|
|
|
|
|
function ModeratorMenu() {
|
|
|
|
const menu = (
|
|
|
|
<Menu
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
label: (
|
|
|
|
<div>
|
|
|
|
<DeleteOutlined style={{ marginRight: 5 }} />
|
|
|
|
Hide message
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
key: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
label: (
|
|
|
|
<div>
|
|
|
|
<StopOutlined style={{ marginRight: 5 }} />
|
|
|
|
Ban User
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
key: 1,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={s.modMenuWrapper}>
|
|
|
|
<Dropdown overlay={menu} trigger={['click']}>
|
|
|
|
<Button ghost icon={<EllipsisOutlined />} />
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|