2022-05-26 06:49:30 +02:00
|
|
|
/* eslint-disable react/no-danger */
|
2022-05-24 08:47:22 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
2022-06-25 06:30:54 +02:00
|
|
|
import { Highlight } from 'react-highlighter-ts';
|
|
|
|
import he from 'he';
|
2022-06-28 09:05:04 +02:00
|
|
|
import cn from 'classnames';
|
2022-05-22 16:10:34 +02:00
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
2022-06-25 06:30:54 +02:00
|
|
|
import { formatTimestamp } from './messageFmt';
|
2022-05-22 16:10:34 +02:00
|
|
|
import s from './ChatUserMessage.module.scss';
|
2022-04-28 08:19:20 +02:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
message: ChatMessage;
|
2022-04-30 00:09:53 +02:00
|
|
|
showModeratorMenu: boolean;
|
2022-06-25 06:30:54 +02:00
|
|
|
highlightString: string;
|
|
|
|
renderAsPersonallySent: boolean;
|
2022-04-28 08:19:20 +02:00
|
|
|
}
|
|
|
|
|
2022-06-25 06:30:54 +02:00
|
|
|
export default function ChatUserMessage({
|
|
|
|
message,
|
|
|
|
highlightString,
|
|
|
|
showModeratorMenu,
|
|
|
|
renderAsPersonallySent, // Move the border to the right and render a background
|
|
|
|
}: Props) {
|
2022-04-30 00:09:53 +02:00
|
|
|
const { body, user, timestamp } = message;
|
|
|
|
const { displayName, displayColor } = user;
|
2022-06-29 03:55:21 +02:00
|
|
|
const color = `var(--theme-user-colors-${displayColor})`;
|
|
|
|
// TODO: Need to convert the above color to a background color.
|
|
|
|
const bgColor = `hsl(100, 20%, 25%)`;
|
2022-05-24 08:47:22 +02:00
|
|
|
const formattedTimestamp = `Sent at ${formatTimestamp(timestamp)}`;
|
|
|
|
const [formattedMessage, setFormattedMessage] = useState<string>(body);
|
|
|
|
|
|
|
|
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-06-28 09:05:04 +02:00
|
|
|
<div
|
|
|
|
className={cn(s.root, {
|
|
|
|
[s.ownMessage]: renderAsPersonallySent,
|
|
|
|
})}
|
|
|
|
style={{ borderColor: color, backgroundColor: bgColor }}
|
|
|
|
title={formattedTimestamp}
|
|
|
|
>
|
2022-05-22 16:10:34 +02:00
|
|
|
<div className={s.user} style={{ color }}>
|
|
|
|
{displayName}
|
|
|
|
</div>
|
2022-06-25 06:30:54 +02:00
|
|
|
<Highlight search={highlightString}>
|
|
|
|
<div className={s.message}>{formattedMessage}</div>
|
|
|
|
</Highlight>
|
2022-04-30 00:09:53 +02:00
|
|
|
{showModeratorMenu && <div>Moderator menu</div>}
|
|
|
|
</div>
|
|
|
|
);
|
2022-04-28 08:19:20 +02:00
|
|
|
}
|