2022-05-02 06:56:11 +03:00
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2023-01-05 13:16:37 +03:00
|
|
|
import { useState, useMemo, useRef, CSSProperties, FC, useEffect } from 'react';
|
2023-03-13 07:53:02 +03:00
|
|
|
import { ErrorBoundary } from 'react-error-boundary';
|
2022-07-15 06:36:47 +03:00
|
|
|
import {
|
|
|
|
ConnectedClientInfoEvent,
|
2023-02-06 06:58:24 +03:00
|
|
|
FediverseEvent,
|
2022-07-15 06:36:47 +03:00
|
|
|
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';
|
|
|
|
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';
|
2023-02-06 06:58:24 +03:00
|
|
|
import { ChatSocialMessage } from '../ChatSocialMessage/ChatSocialMessage';
|
2023-02-20 03:20:07 +03:00
|
|
|
import { ChatNameChangeMessage } from '../ChatNameChangeMessage/ChatNameChangeMessage';
|
2023-03-04 08:54:01 +03:00
|
|
|
import { User } from '../../../interfaces/user.model';
|
2023-03-13 07:53:02 +03:00
|
|
|
import { ComponentError } from '../../ui/ComponentError/ComponentError';
|
2023-02-18 22:58:46 +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;
|
2023-03-02 03:19:02 +03:00
|
|
|
chatAvailable: boolean;
|
2023-05-23 04:56:44 +03:00
|
|
|
focusInput?: boolean;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
|
|
|
|
2023-07-05 21:51:16 +03:00
|
|
|
function shouldCollapseMessages(message: ChatMessage, previous: ChatMessage): boolean {
|
|
|
|
if (!message || !message.user) {
|
2022-09-07 10:00:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-05 21:51:16 +03:00
|
|
|
if (previous.type !== MessageType.CHAT) {
|
2022-11-14 01:49:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
const {
|
|
|
|
user: { id },
|
|
|
|
} = message;
|
2023-07-05 21:51:16 +03:00
|
|
|
if (id !== previous.user.id) {
|
2022-09-07 10:00:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-05 21:51:16 +03:00
|
|
|
if (!previous.timestamp || !message.timestamp) {
|
2022-09-07 10:00:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-03 05:48:07 +03:00
|
|
|
const maxTimestampDelta = 1000 * 40; // 40 seconds
|
2023-07-05 21:51:16 +03:00
|
|
|
const lastTimestamp = new Date(previous.timestamp).getTime();
|
2022-09-07 10:00:28 +03:00
|
|
|
const thisTimestamp = new Date(message.timestamp).getTime();
|
|
|
|
if (thisTimestamp - lastTimestamp > maxTimestampDelta) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-20 23:00:49 +03:00
|
|
|
return true;
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 10:57:12 +03:00
|
|
|
function checkIsModerator(message: ChatMessage | ConnectedClientInfoEvent) {
|
2023-03-04 08:54:01 +03:00
|
|
|
const { user } = message;
|
2022-09-07 10:00:28 +03:00
|
|
|
|
2023-03-04 08:54:01 +03:00
|
|
|
const u = new User(user);
|
2023-05-31 23:28:06 +03:00
|
|
|
return u.isModerator;
|
2022-09-07 10:00:28 +03:00
|
|
|
}
|
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,
|
2023-03-02 03:19:02 +03:00
|
|
|
chatAvailable: chatEnabled,
|
2023-05-23 04:56:44 +03:00
|
|
|
focusInput = true,
|
2022-09-07 10:00:28 +03:00
|
|
|
}) => {
|
2023-02-18 22:58:46 +03:00
|
|
|
const [showScrollToBottomButton, setShowScrollToBottomButton] = useState(false);
|
|
|
|
const [isAtBottom, setIsAtBottom] = useState(false);
|
|
|
|
|
2022-05-02 06:56:11 +03:00
|
|
|
const chatContainerRef = useRef(null);
|
2023-02-18 22:58:46 +03:00
|
|
|
const scrollToBottomDelay = useRef(null);
|
|
|
|
|
2023-07-05 21:51:16 +03:00
|
|
|
const collapsedIndexes: boolean[] = [];
|
|
|
|
let consecutiveTally: number = 1;
|
|
|
|
|
|
|
|
function calculateCollapsedMessages() {
|
|
|
|
// Limits the number of messages that can be collapsed in a row.
|
|
|
|
const maxCollapsedMessageCount = 5;
|
|
|
|
for (let i = collapsedIndexes.length; i < messages.length; i += 1) {
|
|
|
|
const collapse: boolean =
|
|
|
|
i > 0 &&
|
|
|
|
consecutiveTally < maxCollapsedMessageCount &&
|
|
|
|
shouldCollapseMessages(messages[i], messages[i - 1]);
|
|
|
|
collapsedIndexes.push(collapse);
|
|
|
|
consecutiveTally = 1 + (collapse ? consecutiveTally : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldCollapse(index: number): boolean {
|
|
|
|
if (collapsedIndexes.length <= index) {
|
|
|
|
calculateCollapsedMessages();
|
|
|
|
}
|
|
|
|
return collapsedIndexes[index];
|
|
|
|
}
|
2023-02-20 23:00:49 +03:00
|
|
|
|
2023-02-18 22:58:46 +03:00
|
|
|
useEffect(
|
|
|
|
() =>
|
|
|
|
// Clear the timer when the component unmounts
|
|
|
|
() => {
|
|
|
|
clearTimeout(scrollToBottomDelay.current);
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
2022-05-04 00:17:05 +03:00
|
|
|
|
2023-02-06 06:58:24 +03:00
|
|
|
const getFediverseMessage = (message: FediverseEvent) => <ChatSocialMessage message={message} />;
|
|
|
|
|
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} />;
|
|
|
|
};
|
2023-02-18 22:58:46 +03:00
|
|
|
|
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 />;
|
|
|
|
};
|
|
|
|
|
2023-02-20 23:00:49 +03:00
|
|
|
const getUserChatMessageView = (index: number, message: ChatMessage) => {
|
2023-06-01 02:54:30 +03:00
|
|
|
const isAuthorModerator = checkIsModerator(message);
|
|
|
|
|
2023-02-20 23:00:49 +03:00
|
|
|
return (
|
|
|
|
<ChatUserMessage
|
|
|
|
message={message}
|
|
|
|
showModeratorMenu={isModerator} // Moderators have access to an additional menu
|
|
|
|
highlightString={usernameToHighlight} // What to highlight in the message
|
|
|
|
sentBySelf={message.user?.id === chatUserId} // The local user sent this message
|
2023-07-05 21:51:16 +03:00
|
|
|
sameUserAsLast={shouldCollapse(index)}
|
2023-06-01 02:54:30 +03:00
|
|
|
isAuthorModerator={isAuthorModerator}
|
2023-05-31 23:28:06 +03:00
|
|
|
isAuthorBot={message.user?.isBot}
|
2023-02-20 23:00:49 +03:00
|
|
|
isAuthorAuthenticated={message.user?.authenticated}
|
|
|
|
key={message.id}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2022-07-15 06:36:47 +03:00
|
|
|
const getViewForMessage = (
|
|
|
|
index: number,
|
2023-02-06 06:58:24 +03:00
|
|
|
message: ChatMessage | NameChangeEvent | ConnectedClientInfoEvent | FediverseEvent,
|
2022-07-15 06:36:47 +03:00
|
|
|
) => {
|
2022-05-04 00:17:05 +03:00
|
|
|
switch (message.type) {
|
|
|
|
case MessageType.CHAT:
|
2023-02-20 23:00:49 +03:00
|
|
|
return getUserChatMessageView(index, message as ChatMessage);
|
2022-05-26 23:52:04 +03:00
|
|
|
case MessageType.NAME_CHANGE:
|
2023-02-20 03:20:07 +03:00
|
|
|
return <ChatNameChangeMessage message={message as NameChangeEvent} />;
|
2022-07-15 06:36:47 +03:00
|
|
|
case MessageType.CONNECTED_USER_INFO:
|
2023-02-06 06:58:24 +03:00
|
|
|
return getConnectedInfoMessage(message as ConnectedClientInfoEvent);
|
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}
|
|
|
|
/>
|
|
|
|
);
|
2023-02-06 06:58:24 +03:00
|
|
|
case MessageType.FEDIVERSE_ENGAGEMENT_FOLLOW:
|
|
|
|
return getFediverseMessage(message as FediverseEvent);
|
|
|
|
case MessageType.FEDIVERSE_ENGAGEMENT_LIKE:
|
|
|
|
return getFediverseMessage(message as FediverseEvent);
|
|
|
|
case MessageType.FEDIVERSE_ENGAGEMENT_REPOST:
|
|
|
|
return getFediverseMessage(message as FediverseEvent);
|
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
|
|
|
|
2023-06-13 01:44:11 +03:00
|
|
|
const scrollChatToBottom = ref => {
|
2023-02-18 22:58:46 +03:00
|
|
|
clearTimeout(scrollToBottomDelay.current);
|
|
|
|
scrollToBottomDelay.current = setTimeout(() => {
|
2023-06-13 01:44:11 +03:00
|
|
|
ref.current?.scrollTo({ top: Infinity, left: 0, behavior: 'auto' });
|
|
|
|
|
2023-02-18 22:58:46 +03:00
|
|
|
setIsAtBottom(true);
|
2023-05-08 06:29:29 +03:00
|
|
|
}, 150);
|
2023-06-13 01:44:11 +03:00
|
|
|
setShowScrollToBottomButton(false);
|
2023-01-05 13:16:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is a hack to force a scroll to the very bottom of the chat messages
|
|
|
|
// on initial mount of the component.
|
|
|
|
// For https://github.com/owncast/owncast/issues/2500
|
|
|
|
useEffect(() => {
|
|
|
|
setTimeout(() => {
|
2023-06-13 01:44:11 +03:00
|
|
|
scrollChatToBottom(chatContainerRef);
|
2023-01-05 13:16:37 +03:00
|
|
|
}, 500);
|
|
|
|
}, []);
|
|
|
|
|
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
|
2023-01-11 11:53:18 +03:00
|
|
|
id="virtuoso"
|
2022-09-13 09:43:59 +03:00
|
|
|
style={{ height }}
|
|
|
|
className={styles.virtuoso}
|
2022-07-01 20:35:14 +03:00
|
|
|
ref={chatContainerRef}
|
|
|
|
data={messages}
|
2022-07-02 10:08:36 +03:00
|
|
|
itemContent={(index, message) => getViewForMessage(index, message)}
|
2023-02-18 22:58:46 +03:00
|
|
|
initialTopMostItemIndex={messages.length - 1}
|
2023-02-20 05:43:17 +03:00
|
|
|
followOutput={() => {
|
2022-12-20 11:37:17 +03:00
|
|
|
if (isAtBottom) {
|
2023-02-18 22:58:46 +03:00
|
|
|
setShowScrollToBottomButton(false);
|
2023-06-13 01:44:11 +03:00
|
|
|
scrollChatToBottom(chatContainerRef);
|
2023-02-18 22:58:46 +03:00
|
|
|
return 'smooth';
|
2022-12-20 11:37:17 +03:00
|
|
|
}
|
2023-02-18 22:58:46 +03:00
|
|
|
|
2022-12-20 11:37:17 +03:00
|
|
|
return false;
|
|
|
|
}}
|
2022-07-01 20:35:14 +03:00
|
|
|
alignToBottom
|
2023-01-11 11:53:18 +03:00
|
|
|
atBottomThreshold={70}
|
2022-12-20 11:37:17 +03:00
|
|
|
atBottomStateChange={bottom => {
|
2023-02-18 22:58:46 +03:00
|
|
|
setIsAtBottom(bottom);
|
|
|
|
|
|
|
|
if (bottom) {
|
|
|
|
setShowScrollToBottomButton(false);
|
|
|
|
} else {
|
2023-03-04 02:25:22 +03:00
|
|
|
setShowScrollToBottomButton(true);
|
2023-02-18 22:58:46 +03:00
|
|
|
}
|
2022-12-20 11:37:17 +03:00
|
|
|
}}
|
2022-07-01 20:35:14 +03:00
|
|
|
/>
|
2023-02-18 22:58:46 +03:00
|
|
|
{showScrollToBottomButton && (
|
|
|
|
<ScrollToBotBtn
|
|
|
|
onClick={() => {
|
2023-06-13 01:44:11 +03:00
|
|
|
scrollChatToBottom(chatContainerRef);
|
2023-02-18 22:58:46 +03:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2022-07-10 09:56:31 +03:00
|
|
|
</>
|
2022-06-25 07:30:54 +03:00
|
|
|
),
|
2023-02-18 22:58:46 +03:00
|
|
|
[messages, usernameToHighlight, chatUserId, isModerator, showScrollToBottomButton, isAtBottom],
|
2022-06-25 07:30:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2023-03-13 07:53:02 +03:00
|
|
|
<ErrorBoundary
|
|
|
|
// eslint-disable-next-line react/no-unstable-nested-components
|
|
|
|
fallbackRender={({ error, resetErrorBoundary }) => (
|
|
|
|
<ComponentError
|
|
|
|
componentName="ChatContainer"
|
|
|
|
message={error.message}
|
|
|
|
retryFunction={resetErrorBoundary}
|
|
|
|
/>
|
2022-11-13 23:37:31 +03:00
|
|
|
)}
|
2023-03-13 07:53:02 +03:00
|
|
|
>
|
|
|
|
<div id="chat-container" className={styles.chatContainer}>
|
|
|
|
{MessagesTable}
|
|
|
|
{showInput && (
|
|
|
|
<div className={styles.chatTextField}>
|
2023-05-23 04:56:44 +03:00
|
|
|
<ChatTextField enabled={chatEnabled} focusInput={focusInput} />
|
2023-03-13 07:53:02 +03:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</ErrorBoundary>
|
2022-04-30 01:09:53 +03:00
|
|
|
);
|
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
|
|
|
};
|