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-05-12 09:31:31 +03:00
|
|
|
import { useRef } from 'react';
|
2022-05-04 00:17:05 +03:00
|
|
|
import { LoadingOutlined } from '@ant-design/icons';
|
2022-05-22 16:05:40 +03:00
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
|
|
|
import { ChatState } from '../../../interfaces/application-state';
|
|
|
|
import { MessageType } from '../../../interfaces/socket-events';
|
2022-05-22 15:55:52 +03:00
|
|
|
import s from './ChatContainer.module.scss';
|
2022-05-22 17:10:34 +03:00
|
|
|
import { ChatUserMessage } from '..';
|
2022-04-28 09:19:20 +03:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
messages: ChatMessage[];
|
2022-04-30 01:09:53 +03:00
|
|
|
state: ChatState;
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function ChatContainer(props: Props) {
|
2022-04-30 01:09:53 +03:00
|
|
|
const { messages, state } = props;
|
|
|
|
const loading = state === ChatState.Loading;
|
|
|
|
|
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 />;
|
|
|
|
|
|
|
|
const getViewForMessage = message => {
|
|
|
|
switch (message.type) {
|
|
|
|
case MessageType.CHAT:
|
|
|
|
return <ChatUserMessage message={message} showModeratorMenu={false} />;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2022-05-02 06:56:11 +03:00
|
|
|
|
2022-04-30 01:09:53 +03:00
|
|
|
return (
|
2022-05-04 10:55:44 +03:00
|
|
|
<div>
|
2022-05-22 15:55:52 +03:00
|
|
|
<div className={s.chatHeader}>
|
|
|
|
<span>stream chat</span>
|
|
|
|
</div>
|
2022-05-04 10:55:44 +03:00
|
|
|
<Spin spinning={loading} indicator={spinIcon} />
|
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-04-30 01:09:53 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|