2022-04-30 01:09:53 +03:00
|
|
|
import { Spin } from 'antd';
|
2022-05-02 06:56:11 +03:00
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2022-06-25 07:30:54 +03:00
|
|
|
import { useMemo, useRef } from 'react';
|
2022-05-04 00:17:05 +03:00
|
|
|
import { LoadingOutlined } from '@ant-design/icons';
|
2022-05-26 06:38:40 +03:00
|
|
|
|
2022-05-26 23:52:04 +03:00
|
|
|
import { MessageType, NameChangeEvent } from '../../../interfaces/socket-events';
|
2022-05-22 15:55:52 +03:00
|
|
|
import s from './ChatContainer.module.scss';
|
2022-05-26 06:38:40 +03:00
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
2022-05-22 17:10:34 +03:00
|
|
|
import { ChatUserMessage } from '..';
|
2022-04-28 09:19:20 +03:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
messages: ChatMessage[];
|
2022-05-26 06:38:40 +03:00
|
|
|
loading: boolean;
|
2022-06-25 07:30:54 +03:00
|
|
|
usernameToHighlight: string;
|
|
|
|
chatUserId: string;
|
|
|
|
isModerator: boolean;
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function ChatContainer(props: Props) {
|
2022-06-25 07:30:54 +03:00
|
|
|
const { messages, loading, usernameToHighlight, chatUserId, isModerator } = props;
|
2022-04-30 01:09:53 +03:00
|
|
|
|
2022-05-02 06:56:11 +03:00
|
|
|
const chatContainerRef = useRef(null);
|
2022-05-04 00:17:05 +03:00
|
|
|
const spinIcon = <LoadingOutlined style={{ fontSize: '32px' }} spin />;
|
|
|
|
|
2022-05-26 23:52:04 +03:00
|
|
|
const getNameChangeViewForMessage = (message: NameChangeEvent) => {
|
2022-06-29 06:40:18 +03: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 23:52:04 +03:00
|
|
|
};
|
|
|
|
|
2022-05-04 00:17:05 +03:00
|
|
|
const getViewForMessage = message => {
|
|
|
|
switch (message.type) {
|
|
|
|
case MessageType.CHAT:
|
2022-06-25 07:30:54 +03: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-05-26 23:52:04 +03:00
|
|
|
case MessageType.NAME_CHANGE:
|
|
|
|
return getNameChangeViewForMessage(message);
|
2022-05-04 00:17:05 +03:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2022-05-02 06:56:11 +03:00
|
|
|
|
2022-06-25 07:30:54 +03:00
|
|
|
const MessagesTable = useMemo(
|
|
|
|
() => (
|
2022-05-02 06:56:11 +03:00
|
|
|
<Virtuoso
|
2022-05-05 02:55:54 +03:00
|
|
|
style={{ height: '80vh' }}
|
2022-05-02 06:56:11 +03:00
|
|
|
ref={chatContainerRef}
|
|
|
|
initialTopMostItemIndex={999}
|
|
|
|
data={messages}
|
2022-05-04 00:17:05 +03:00
|
|
|
itemContent={(index, message) => getViewForMessage(message)}
|
2022-05-02 08:51:57 +03:00
|
|
|
followOutput="smooth"
|
2022-05-02 06:56:11 +03:00
|
|
|
/>
|
2022-06-25 07:30:54 +03: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-30 01:09:53 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|