owncast/web/components/chat/ChatUserMessage/ChatUserMessage.tsx

53 lines
1.6 KiB
TypeScript
Raw Normal View History

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