2022-04-29 15:09:53 -07:00
|
|
|
import { Spin } from 'antd';
|
2022-05-01 20:56:11 -07:00
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2022-06-24 21:30:54 -07:00
|
|
|
import { useMemo, useRef } from 'react';
|
2022-05-03 14:17:05 -07:00
|
|
|
import { LoadingOutlined } from '@ant-design/icons';
|
2022-05-25 20:38:40 -07:00
|
|
|
|
2022-05-26 13:52:04 -07:00
|
|
|
import { MessageType, NameChangeEvent } from '../../../interfaces/socket-events';
|
2022-05-22 14:55:52 +02:00
|
|
|
import s from './ChatContainer.module.scss';
|
2022-05-25 20:38:40 -07:00
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
2022-05-22 16:10:34 +02:00
|
|
|
import { ChatUserMessage } from '..';
|
2022-04-27 23:19:20 -07:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
messages: ChatMessage[];
|
2022-05-25 20:38:40 -07:00
|
|
|
loading: boolean;
|
2022-06-24 21:30:54 -07:00
|
|
|
usernameToHighlight: string;
|
|
|
|
chatUserId: string;
|
|
|
|
isModerator: boolean;
|
2022-04-27 23:19:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function ChatContainer(props: Props) {
|
2022-06-24 21:30:54 -07:00
|
|
|
const { messages, loading, usernameToHighlight, chatUserId, isModerator } = props;
|
2022-04-29 15:09:53 -07:00
|
|
|
|
2022-05-01 20:56:11 -07:00
|
|
|
const chatContainerRef = useRef(null);
|
2022-05-03 14:17:05 -07:00
|
|
|
const spinIcon = <LoadingOutlined style={{ fontSize: '32px' }} spin />;
|
|
|
|
|
2022-05-26 13:52:04 -07:00
|
|
|
const getNameChangeViewForMessage = (message: NameChangeEvent) => {
|
2022-06-28 20:40:18 -07:00
|
|
|
const { oldName, user } = message;
|
|
|
|
const { displayName, displayColor } = user;
|
|
|
|
const color = `var(--theme-user-colors-${displayColor})`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<span style={{ color }}>{oldName}</span> is now known as {displayName}
|
|
|
|
</div>
|
|
|
|
);
|
2022-05-26 13:52:04 -07:00
|
|
|
};
|
|
|
|
|
2022-05-03 14:17:05 -07:00
|
|
|
const getViewForMessage = message => {
|
|
|
|
switch (message.type) {
|
|
|
|
case MessageType.CHAT:
|
2022-06-24 21:30:54 -07:00
|
|
|
return (
|
|
|
|
<ChatUserMessage
|
|
|
|
message={message}
|
|
|
|
showModeratorMenu={isModerator} // Moderators have access to an additional menu
|
|
|
|
highlightString={usernameToHighlight} // What to highlight in the message
|
|
|
|
renderAsPersonallySent={message.user?.id === chatUserId} // The local user sent this message
|
2022-06-29 11:50:56 -07:00
|
|
|
key={message.id}
|
2022-06-24 21:30:54 -07:00
|
|
|
/>
|
|
|
|
);
|
2022-05-26 13:52:04 -07:00
|
|
|
case MessageType.NAME_CHANGE:
|
|
|
|
return getNameChangeViewForMessage(message);
|
2022-05-03 14:17:05 -07:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2022-05-01 20:56:11 -07:00
|
|
|
|
2022-06-24 21:30:54 -07:00
|
|
|
const MessagesTable = useMemo(
|
|
|
|
() => (
|
2022-05-01 20:56:11 -07:00
|
|
|
<Virtuoso
|
2022-06-29 11:50:56 -07:00
|
|
|
style={{ height: '70vh' }}
|
2022-05-01 20:56:11 -07:00
|
|
|
ref={chatContainerRef}
|
2022-06-29 11:50:56 -07:00
|
|
|
initialTopMostItemIndex={999} // Force alignment to bottom
|
2022-05-01 20:56:11 -07:00
|
|
|
data={messages}
|
2022-05-03 14:17:05 -07:00
|
|
|
itemContent={(index, message) => getViewForMessage(message)}
|
2022-06-29 11:50:56 -07:00
|
|
|
followOutput="auto"
|
|
|
|
alignToBottom
|
2022-05-01 20:56:11 -07:00
|
|
|
/>
|
2022-06-24 21:30:54 -07:00
|
|
|
),
|
|
|
|
[messages, usernameToHighlight, chatUserId, isModerator],
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className={s.chatHeader}>
|
|
|
|
<span>stream chat</span>
|
|
|
|
</div>
|
|
|
|
<Spin spinning={loading} indicator={spinIcon}>
|
|
|
|
{MessagesTable}
|
|
|
|
</Spin>
|
2022-04-29 15:09:53 -07:00
|
|
|
</div>
|
|
|
|
);
|
2022-04-27 23:19:20 -07:00
|
|
|
}
|