2022-05-02 06:56:11 +03:00
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { useState, useMemo, useRef, CSSProperties, FC } from 'react';
|
2022-09-30 14:17:22 +03:00
|
|
|
import { EditFilled } from '@ant-design/icons';
|
2022-07-15 06:36:47 +03:00
|
|
|
import {
|
|
|
|
ConnectedClientInfoEvent,
|
|
|
|
MessageType,
|
|
|
|
NameChangeEvent,
|
|
|
|
} from '../../../interfaces/socket-events';
|
2022-09-07 10:00:28 +03:00
|
|
|
import styles from './ChatContainer.module.scss';
|
2022-05-26 06:38:40 +03:00
|
|
|
import { ChatMessage } from '../../../interfaces/chat-message.model';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { ChatUserMessage } from '../ChatUserMessage/ChatUserMessage';
|
|
|
|
import { ChatTextField } from '../ChatTextField/ChatTextField';
|
|
|
|
import { ChatModeratorNotification } from '../ChatModeratorNotification/ChatModeratorNotification';
|
2022-08-22 01:50:22 +03:00
|
|
|
// import ChatActionMessage from '../ChatAction/ChatActionMessage';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { ChatSystemMessage } from '../ChatSystemMessage/ChatSystemMessage';
|
|
|
|
import { ChatJoinMessage } from '../ChatJoinMessage/ChatJoinMessage';
|
2022-09-30 14:17:22 +03:00
|
|
|
import { ScrollToBotBtn } from './ScrollToBotBtn';
|
2022-10-19 05:44:42 +03:00
|
|
|
import { ChatActionMessage } from '../ChatActionMessage/ChatActionMessage';
|
2022-04-28 09:19:20 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type ChatContainerProps = {
|
2022-04-28 09:19:20 +03:00
|
|
|
messages: ChatMessage[];
|
2022-06-25 07:30:54 +03:00
|
|
|
usernameToHighlight: string;
|
|
|
|
chatUserId: string;
|
|
|
|
isModerator: boolean;
|
2022-09-05 07:46:54 +03:00
|
|
|
showInput?: boolean;
|
|
|
|
height?: string;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function shouldCollapseMessages(messages: ChatMessage[], index: number): boolean {
|
|
|
|
if (messages.length < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = messages[index];
|
2022-11-14 01:49:49 +03:00
|
|
|
if (!message || !message.user) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
const {
|
|
|
|
user: { id },
|
|
|
|
} = message;
|
|
|
|
const lastMessage = messages[index - 1];
|
|
|
|
if (lastMessage?.type !== MessageType.CHAT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastMessage.timestamp || !message.timestamp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxTimestampDelta = 1000 * 60 * 2; // 2 minutes
|
|
|
|
const lastTimestamp = new Date(lastMessage.timestamp).getTime();
|
|
|
|
const thisTimestamp = new Date(message.timestamp).getTime();
|
|
|
|
if (thisTimestamp - lastTimestamp > maxTimestampDelta) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id === lastMessage?.user.id;
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 10:57:12 +03:00
|
|
|
function checkIsModerator(message: ChatMessage | ConnectedClientInfoEvent) {
|
|
|
|
const {
|
|
|
|
user: { scopes },
|
|
|
|
} = message;
|
2022-09-07 10:00:28 +03:00
|
|
|
|
|
|
|
if (!scopes || scopes.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scopes.includes('MODERATOR');
|
|
|
|
}
|
2022-04-30 01:09:53 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export const ChatContainer: FC<ChatContainerProps> = ({
|
|
|
|
messages,
|
|
|
|
usernameToHighlight,
|
|
|
|
chatUserId,
|
|
|
|
isModerator,
|
|
|
|
showInput,
|
|
|
|
height,
|
|
|
|
}) => {
|
2022-07-01 20:35:14 +03:00
|
|
|
const [atBottom, setAtBottom] = useState(false);
|
|
|
|
// const [showButton, setShowButton] = useState(false);
|
2022-05-02 06:56:11 +03:00
|
|
|
const chatContainerRef = useRef(null);
|
2022-07-10 09:56:31 +03:00
|
|
|
// const spinIcon = <LoadingOutlined style={{ fontSize: '32px' }} spin />;
|
2022-05-04 00:17:05 +03:00
|
|
|
|
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;
|
2022-09-02 05:37:11 +03:00
|
|
|
const color = `var(--theme-color-users-${displayColor})`;
|
2022-06-29 06:40:18 +03:00
|
|
|
|
|
|
|
return (
|
2022-09-07 10:00:28 +03:00
|
|
|
<div className={styles.nameChangeView}>
|
2022-07-02 10:13:50 +03:00
|
|
|
<div style={{ marginRight: 5, height: 'max-content', margin: 'auto 5px auto 0' }}>
|
2022-07-02 10:08:36 +03:00
|
|
|
<EditFilled />
|
|
|
|
</div>
|
2022-09-07 10:00:28 +03:00
|
|
|
<div className={styles.nameChangeText}>
|
2022-07-02 10:08:36 +03:00
|
|
|
<span style={{ color }}>{oldName}</span>
|
2022-09-07 10:00:28 +03:00
|
|
|
<span className={styles.plain}> is now known as </span>
|
2022-07-02 10:08:36 +03:00
|
|
|
<span style={{ color }}>{displayName}</span>
|
|
|
|
</div>
|
2022-06-29 06:40:18 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-05-26 23:52:04 +03:00
|
|
|
};
|
|
|
|
|
2022-07-15 07:05:34 +03:00
|
|
|
const getUserJoinedMessage = (message: ChatMessage) => {
|
2022-09-30 10:57:12 +03:00
|
|
|
const {
|
|
|
|
user: { displayName, displayColor },
|
|
|
|
} = message;
|
2022-08-22 01:50:22 +03:00
|
|
|
const isAuthorModerator = checkIsModerator(message);
|
2022-07-15 07:05:34 +03:00
|
|
|
return (
|
2022-08-22 01:50:22 +03:00
|
|
|
<ChatJoinMessage
|
|
|
|
displayName={displayName}
|
|
|
|
userColor={displayColor}
|
|
|
|
isAuthorModerator={isAuthorModerator}
|
2022-07-15 07:05:34 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-19 05:44:42 +03:00
|
|
|
const getActionMessage = (message: ChatMessage) => {
|
|
|
|
const { body } = message;
|
|
|
|
return <ChatActionMessage body={body} />;
|
|
|
|
};
|
2022-07-15 06:36:47 +03:00
|
|
|
const getConnectedInfoMessage = (message: ConnectedClientInfoEvent) => {
|
|
|
|
const modStatusUpdate = checkIsModerator(message);
|
|
|
|
if (!modStatusUpdate) {
|
2022-08-10 06:58:10 +03:00
|
|
|
// Important note: We can't return null or an element with zero width
|
|
|
|
// or zero height. So to work around this we return a very small 1x1 div.
|
|
|
|
const st: CSSProperties = { width: '1px', height: '1px' };
|
|
|
|
return <div style={st} />;
|
2022-07-15 06:36:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alert the user that they are a moderator.
|
|
|
|
return <ChatModeratorNotification />;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getViewForMessage = (
|
|
|
|
index: number,
|
|
|
|
message: ChatMessage | NameChangeEvent | ConnectedClientInfoEvent,
|
|
|
|
) => {
|
2022-05-04 00:17:05 +03:00
|
|
|
switch (message.type) {
|
|
|
|
case MessageType.CHAT:
|
2022-06-25 07:30:54 +03:00
|
|
|
return (
|
|
|
|
<ChatUserMessage
|
2022-07-01 20:35:14 +03:00
|
|
|
message={message as ChatMessage}
|
2022-06-25 07:30:54 +03:00
|
|
|
showModeratorMenu={isModerator} // Moderators have access to an additional menu
|
|
|
|
highlightString={usernameToHighlight} // What to highlight in the message
|
2022-07-01 23:49:42 +03:00
|
|
|
sentBySelf={message.user?.id === chatUserId} // The local user sent this message
|
2022-08-16 05:44:29 +03:00
|
|
|
sameUserAsLast={shouldCollapseMessages(messages, index)}
|
|
|
|
isAuthorModerator={(message as ChatMessage).user.scopes?.includes('MODERATOR')}
|
2022-08-22 00:04:16 +03:00
|
|
|
isAuthorAuthenticated={message.user?.authenticated}
|
2022-06-29 21:50:56 +03:00
|
|
|
key={message.id}
|
2022-06-25 07:30:54 +03:00
|
|
|
/>
|
|
|
|
);
|
2022-05-26 23:52:04 +03:00
|
|
|
case MessageType.NAME_CHANGE:
|
2022-07-01 20:35:14 +03:00
|
|
|
return getNameChangeViewForMessage(message as NameChangeEvent);
|
2022-07-15 06:36:47 +03:00
|
|
|
case MessageType.CONNECTED_USER_INFO:
|
|
|
|
return getConnectedInfoMessage(message);
|
2022-07-15 07:05:34 +03:00
|
|
|
case MessageType.USER_JOINED:
|
|
|
|
return getUserJoinedMessage(message as ChatMessage);
|
2022-10-19 05:44:42 +03:00
|
|
|
case MessageType.CHAT_ACTION:
|
|
|
|
return getActionMessage(message as ChatMessage);
|
2022-08-11 06:22:00 +03:00
|
|
|
case MessageType.SYSTEM:
|
|
|
|
return (
|
|
|
|
<ChatSystemMessage
|
|
|
|
message={message as ChatMessage}
|
|
|
|
highlightString={usernameToHighlight} // What to highlight in the message
|
|
|
|
key={message.id}
|
|
|
|
/>
|
|
|
|
);
|
2022-07-15 06:36:47 +03:00
|
|
|
|
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-07-10 09:56:31 +03:00
|
|
|
<>
|
2022-07-01 20:35:14 +03:00
|
|
|
<Virtuoso
|
2022-09-13 09:43:59 +03:00
|
|
|
style={{ height }}
|
|
|
|
className={styles.virtuoso}
|
2022-07-01 20:35:14 +03:00
|
|
|
ref={chatContainerRef}
|
|
|
|
initialTopMostItemIndex={messages.length - 1} // Force alignment to bottom
|
|
|
|
data={messages}
|
2022-07-02 10:08:36 +03:00
|
|
|
itemContent={(index, message) => getViewForMessage(index, message)}
|
2022-07-01 20:35:14 +03:00
|
|
|
followOutput="auto"
|
|
|
|
alignToBottom
|
|
|
|
atBottomStateChange={bottom => setAtBottom(bottom)}
|
|
|
|
/>
|
2022-09-30 14:17:22 +03:00
|
|
|
{!atBottom && <ScrollToBotBtn chatContainerRef={chatContainerRef} messages={messages} />}
|
2022-07-10 09:56:31 +03:00
|
|
|
</>
|
2022-06-25 07:30:54 +03:00
|
|
|
),
|
2022-08-23 05:23:06 +03:00
|
|
|
[messages, usernameToHighlight, chatUserId, isModerator, atBottom],
|
2022-06-25 07:30:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-11-05 06:04:13 +03:00
|
|
|
<div id="chat-container" className={styles.chatContainer}>
|
2022-07-10 09:56:31 +03:00
|
|
|
{MessagesTable}
|
2022-11-13 23:37:31 +03:00
|
|
|
{showInput && (
|
|
|
|
<div className={styles.chatTextField}>
|
|
|
|
<ChatTextField />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-04-30 01:09:53 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2022-09-05 07:46:54 +03:00
|
|
|
|
|
|
|
ChatContainer.defaultProps = {
|
|
|
|
showInput: true,
|
2022-09-13 09:43:59 +03:00
|
|
|
height: 'auto',
|
2022-09-05 07:46:54 +03:00
|
|
|
};
|