2020-10-05 04:43:31 +03:00
|
|
|
import { h, Component, createRef } from '/js/web_modules/preact.js';
|
|
|
|
import htm from '/js/web_modules/htm.js';
|
2020-08-13 11:28:25 +03:00
|
|
|
const html = htm.bind(h);
|
|
|
|
|
2020-08-13 12:08:14 +03:00
|
|
|
import Message from './message.js';
|
2020-08-14 14:19:19 +03:00
|
|
|
import ChatInput from './chat-input.js';
|
2020-08-24 05:06:58 +03:00
|
|
|
import { CALLBACKS, SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
2021-08-30 02:59:14 +03:00
|
|
|
import { jumpToBottom, debounce } from '../../utils/helpers.js';
|
2021-11-03 05:27:41 +03:00
|
|
|
import {
|
|
|
|
extraUserNamesFromMessageHistory,
|
|
|
|
checkIsModerator,
|
|
|
|
} from '../../utils/chat.js';
|
2021-06-08 03:37:20 +03:00
|
|
|
import {
|
|
|
|
URL_CHAT_HISTORY,
|
|
|
|
MESSAGE_JUMPTOBOTTOM_BUFFER,
|
|
|
|
} from '../../utils/constants.js';
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2021-12-28 10:00:10 +03:00
|
|
|
const MAX_RENDER_BACKLOG = 300;
|
|
|
|
|
2021-12-12 03:44:39 +03:00
|
|
|
// Add message types that should be displayed in chat to this array.
|
|
|
|
const renderableChatStyleMessages = [
|
|
|
|
SOCKET_MESSAGE_TYPES.NAME_CHANGE,
|
|
|
|
SOCKET_MESSAGE_TYPES.CONNECTED_USER_INFO,
|
|
|
|
SOCKET_MESSAGE_TYPES.USER_JOINED,
|
2021-12-27 22:29:43 +03:00
|
|
|
SOCKET_MESSAGE_TYPES.CHAT_ACTION,
|
2021-12-12 03:44:39 +03:00
|
|
|
SOCKET_MESSAGE_TYPES.SYSTEM,
|
|
|
|
SOCKET_MESSAGE_TYPES.CHAT,
|
2022-01-13 00:53:10 +03:00
|
|
|
SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_FOLLOW,
|
|
|
|
SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_LIKE,
|
|
|
|
SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_REPOST,
|
2021-12-12 03:44:39 +03:00
|
|
|
];
|
2020-08-13 11:28:25 +03:00
|
|
|
export default class Chat extends Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
chatUserNames: [],
|
2021-12-28 10:00:10 +03:00
|
|
|
// Ordered array of messages sorted by timestamp.
|
|
|
|
sortedMessages: [],
|
|
|
|
|
2020-12-07 08:01:52 +03:00
|
|
|
newMessagesReceived: false,
|
2020-12-23 04:43:35 +03:00
|
|
|
webSocketConnected: true,
|
2021-11-03 05:27:41 +03:00
|
|
|
isModerator: false,
|
2020-08-16 04:28:29 +03:00
|
|
|
};
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2020-08-24 08:21:42 +03:00
|
|
|
this.scrollableMessagesContainer = createRef();
|
|
|
|
|
2020-08-13 19:28:47 +03:00
|
|
|
this.websocket = null;
|
2020-12-04 09:43:31 +03:00
|
|
|
this.receivedFirstMessages = false;
|
2020-12-30 00:35:33 +03:00
|
|
|
this.receivedMessageUpdate = false;
|
2021-07-20 05:22:29 +03:00
|
|
|
this.hasFetchedHistory = false;
|
2021-12-28 10:00:10 +03:00
|
|
|
|
|
|
|
// Unordered dictionary of messages keyed by ID.
|
|
|
|
this.messages = {};
|
|
|
|
|
2020-12-23 04:43:35 +03:00
|
|
|
this.windowBlurred = false;
|
|
|
|
this.numMessagesSinceBlur = 0;
|
|
|
|
|
2020-08-13 19:28:47 +03:00
|
|
|
this.getChatHistory = this.getChatHistory.bind(this);
|
2020-12-23 04:43:35 +03:00
|
|
|
this.handleNetworkingError = this.handleNetworkingError.bind(this);
|
|
|
|
this.handleWindowBlur = this.handleWindowBlur.bind(this);
|
|
|
|
this.handleWindowFocus = this.handleWindowFocus.bind(this);
|
|
|
|
this.handleWindowResize = debounce(this.handleWindowResize.bind(this), 500);
|
|
|
|
this.messageListCallback = this.messageListCallback.bind(this);
|
2020-08-13 19:28:47 +03:00
|
|
|
this.receivedWebsocketMessage = this.receivedWebsocketMessage.bind(this);
|
2020-12-23 04:43:35 +03:00
|
|
|
this.scrollToBottom = this.scrollToBottom.bind(this);
|
|
|
|
this.submitChat = this.submitChat.bind(this);
|
2020-09-22 11:38:43 +03:00
|
|
|
this.websocketConnected = this.websocketConnected.bind(this);
|
2020-08-13 19:28:47 +03:00
|
|
|
this.websocketDisconnected = this.websocketDisconnected.bind(this);
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2021-06-08 03:37:20 +03:00
|
|
|
this.setupWebSocketCallbacks();
|
2020-12-07 08:01:52 +03:00
|
|
|
|
2021-06-08 03:37:20 +03:00
|
|
|
window.addEventListener('resize', this.handleWindowResize);
|
2020-11-18 03:59:27 +03:00
|
|
|
|
2021-08-01 02:21:30 +03:00
|
|
|
if (!this.props.readonly) {
|
2021-06-08 03:37:20 +03:00
|
|
|
window.addEventListener('blur', this.handleWindowBlur);
|
|
|
|
window.addEventListener('focus', this.handleWindowFocus);
|
|
|
|
}
|
2020-12-23 04:43:35 +03:00
|
|
|
|
2021-06-08 03:37:20 +03:00
|
|
|
this.messageListObserver = new MutationObserver(this.messageListCallback);
|
|
|
|
this.messageListObserver.observe(this.scrollableMessagesContainer.current, {
|
|
|
|
childList: true,
|
|
|
|
});
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
2020-12-07 08:01:52 +03:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { username, chatInputEnabled } = this.props;
|
2021-06-08 03:37:20 +03:00
|
|
|
const { username: nextUserName, chatInputEnabled: nextChatEnabled } =
|
|
|
|
nextProps;
|
2020-12-07 08:01:52 +03:00
|
|
|
|
2021-12-28 10:00:10 +03:00
|
|
|
const {
|
|
|
|
webSocketConnected,
|
|
|
|
chatUserNames,
|
|
|
|
newMessagesReceived,
|
|
|
|
sortedMessages,
|
|
|
|
} = this.state;
|
2021-11-03 04:00:15 +03:00
|
|
|
|
2021-06-08 03:37:20 +03:00
|
|
|
const {
|
|
|
|
webSocketConnected: nextSocket,
|
|
|
|
chatUserNames: nextUserNames,
|
|
|
|
newMessagesReceived: nextMessagesReceived,
|
|
|
|
} = nextState;
|
2020-12-07 08:01:52 +03:00
|
|
|
|
2021-12-28 10:00:10 +03:00
|
|
|
// If there are an updated number of sorted message then a render pass
|
|
|
|
// needs to take place to render these new messages.
|
|
|
|
if (
|
|
|
|
Object.keys(sortedMessages).length !==
|
|
|
|
Object.keys(nextState.sortedMessages).length
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newMessagesReceived) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:01:52 +03:00
|
|
|
return (
|
|
|
|
username !== nextUserName ||
|
|
|
|
chatInputEnabled !== nextChatEnabled ||
|
|
|
|
webSocketConnected !== nextSocket ||
|
2021-06-08 03:37:20 +03:00
|
|
|
chatUserNames.length !== nextUserNames.length ||
|
|
|
|
newMessagesReceived !== nextMessagesReceived
|
2020-12-07 08:01:52 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-24 08:21:42 +03:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2021-12-28 10:00:10 +03:00
|
|
|
const { accessToken } = this.props;
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
// Fetch chat history
|
|
|
|
if (!this.hasFetchedHistory && accessToken) {
|
|
|
|
this.hasFetchedHistory = true;
|
|
|
|
this.getChatHistory(accessToken);
|
|
|
|
}
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
|
2020-08-24 22:34:36 +03:00
|
|
|
componentWillUnmount() {
|
2020-09-22 06:11:09 +03:00
|
|
|
window.removeEventListener('resize', this.handleWindowResize);
|
2021-08-01 02:21:30 +03:00
|
|
|
if (!this.props.readonly) {
|
2020-12-23 04:43:35 +03:00
|
|
|
window.removeEventListener('blur', this.handleWindowBlur);
|
|
|
|
window.removeEventListener('focus', this.handleWindowFocus);
|
2021-06-08 03:37:20 +03:00
|
|
|
}
|
2020-11-18 03:59:27 +03:00
|
|
|
this.messageListObserver.disconnect();
|
2020-08-24 22:34:36 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:19:19 +03:00
|
|
|
setupWebSocketCallbacks() {
|
|
|
|
this.websocket = this.props.websocket;
|
|
|
|
if (this.websocket) {
|
2021-06-08 03:37:20 +03:00
|
|
|
this.websocket.addListener(
|
|
|
|
CALLBACKS.RAW_WEBSOCKET_MESSAGE_RECEIVED,
|
|
|
|
this.receivedWebsocketMessage
|
|
|
|
);
|
|
|
|
this.websocket.addListener(
|
|
|
|
CALLBACKS.WEBSOCKET_CONNECTED,
|
|
|
|
this.websocketConnected
|
|
|
|
);
|
|
|
|
this.websocket.addListener(
|
|
|
|
CALLBACKS.WEBSOCKET_DISCONNECTED,
|
|
|
|
this.websocketDisconnected
|
|
|
|
);
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch chat history
|
2021-12-28 10:00:10 +03:00
|
|
|
async getChatHistory(accessToken) {
|
2021-07-26 08:26:27 +03:00
|
|
|
const { username } = this.props;
|
2021-12-28 10:00:10 +03:00
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
URL_CHAT_HISTORY + `?accessToken=${accessToken}`
|
|
|
|
);
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
// Backlog of usernames from history
|
|
|
|
const allChatUserNames = extraUserNamesFromMessageHistory(data);
|
|
|
|
const chatUserNames = allChatUserNames.filter((name) => name != username);
|
2021-12-12 11:11:38 +03:00
|
|
|
|
2021-12-28 10:00:10 +03:00
|
|
|
this.addNewRenderableMessages(data);
|
|
|
|
|
|
|
|
this.setState((previousState) => {
|
|
|
|
return {
|
|
|
|
...previousState,
|
|
|
|
chatUserNames,
|
|
|
|
};
|
2020-08-13 19:28:47 +03:00
|
|
|
});
|
2021-12-28 10:00:10 +03:00
|
|
|
} catch (error) {
|
|
|
|
this.handleNetworkingError(`Fetch getChatHistory: ${error}`);
|
|
|
|
}
|
|
|
|
|
2022-04-19 09:46:31 +03:00
|
|
|
jumpToBottom(this.scrollableMessagesContainer.current, 'instant');
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
receivedWebsocketMessage(message) {
|
2020-12-30 00:35:33 +03:00
|
|
|
this.handleMessage(message);
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
|
|
|
|
2020-12-07 08:01:52 +03:00
|
|
|
handleNetworkingError(error) {
|
|
|
|
// todo: something more useful
|
2021-07-20 05:22:29 +03:00
|
|
|
console.error('chat error', error);
|
2020-12-07 08:01:52 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 10:00:10 +03:00
|
|
|
// Give a list of message IDs and the visibility state they should change to.
|
|
|
|
updateMessagesVisibility(idsToUpdate, visible) {
|
|
|
|
let messageList = { ...this.messages };
|
|
|
|
|
|
|
|
// Iterate through each ID and mark the associated ID in our messages
|
|
|
|
// dictionary with the new visibility.
|
|
|
|
for (const id of idsToUpdate) {
|
|
|
|
const message = messageList[id];
|
|
|
|
if (message) {
|
|
|
|
message.visible = visible;
|
|
|
|
messageList[id] = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedMessagesList = {
|
|
|
|
...this.messages,
|
|
|
|
...messageList,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.messages = updatedMessagesList;
|
2022-01-13 00:53:10 +03:00
|
|
|
|
|
|
|
this.resortAndRenderMessages();
|
2021-12-28 10:00:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChangeModeratorStatus(isModerator) {
|
|
|
|
if (isModerator !== this.state.isModerator) {
|
|
|
|
this.setState((previousState) => {
|
|
|
|
return { ...previousState, isModerator: isModerator };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWindowFocusNotificationCount(readonly, messageType) {
|
|
|
|
// if window is blurred and we get a new message, add 1 to title
|
|
|
|
if (
|
|
|
|
!readonly &&
|
|
|
|
messageType === SOCKET_MESSAGE_TYPES.CHAT &&
|
|
|
|
this.windowBlurred
|
|
|
|
) {
|
|
|
|
this.numMessagesSinceBlur += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addNewRenderableMessages(messagesArray) {
|
|
|
|
// Convert the array of chat history messages into an object
|
|
|
|
// to be merged with the existing chat messages.
|
|
|
|
const newMessages = messagesArray.reduce(
|
|
|
|
(o, message) => ({ ...o, [message.id]: message }),
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Keep our unsorted collection of messages keyed by ID.
|
|
|
|
const updatedMessagesList = {
|
|
|
|
...newMessages,
|
|
|
|
...this.messages,
|
|
|
|
};
|
|
|
|
this.messages = updatedMessagesList;
|
|
|
|
|
2022-01-13 00:53:10 +03:00
|
|
|
this.resortAndRenderMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
resortAndRenderMessages() {
|
2021-12-28 10:00:10 +03:00
|
|
|
// Convert the unordered dictionary of messages to an ordered array.
|
|
|
|
// NOTE: This sorts the entire collection of messages on every new message
|
|
|
|
// because the order a message comes in cannot be trusted that it's the order
|
|
|
|
// it was sent, you need to sort by timestamp. I don't know if there
|
|
|
|
// is a performance problem waiting to occur here for larger chat feeds.
|
2022-01-13 00:53:10 +03:00
|
|
|
var sortedMessages = Object.values(this.messages)
|
2021-12-28 10:00:10 +03:00
|
|
|
// Filter out messages set to not be visible
|
|
|
|
.filter((message) => message.visible !== false)
|
|
|
|
.sort((a, b) => {
|
|
|
|
return Date.parse(a.timestamp) - Date.parse(b.timestamp);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Cap this list to 300 items to improve browser performance.
|
|
|
|
if (sortedMessages.length >= MAX_RENDER_BACKLOG) {
|
|
|
|
sortedMessages = sortedMessages.slice(
|
|
|
|
sortedMessages.length - MAX_RENDER_BACKLOG
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState((previousState) => {
|
|
|
|
return {
|
|
|
|
...previousState,
|
|
|
|
newMessagesReceived: true,
|
|
|
|
sortedMessages,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-30 00:35:33 +03:00
|
|
|
// handle any incoming message
|
|
|
|
handleMessage(message) {
|
2021-12-28 10:00:10 +03:00
|
|
|
const { type: messageType } = message;
|
|
|
|
const { readonly, username } = this.props;
|
2020-08-26 10:51:40 +03:00
|
|
|
|
2021-12-16 04:28:56 +03:00
|
|
|
// Allow non-user chat messages to be visible by default.
|
|
|
|
const messageVisible =
|
|
|
|
message.visible || messageType !== SOCKET_MESSAGE_TYPES.CHAT;
|
|
|
|
|
2021-12-28 10:00:10 +03:00
|
|
|
// Show moderator status
|
2021-11-03 05:27:41 +03:00
|
|
|
if (messageType === SOCKET_MESSAGE_TYPES.CONNECTED_USER_INFO) {
|
|
|
|
const modStatusUpdate = checkIsModerator(message);
|
2021-12-28 10:00:10 +03:00
|
|
|
this.handleChangeModeratorStatus(modStatusUpdate);
|
2021-11-03 05:27:41 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 04:00:15 +03:00
|
|
|
// Change the visibility of messages by ID.
|
2021-12-28 10:00:10 +03:00
|
|
|
if (messageType === SOCKET_MESSAGE_TYPES.VISIBILITY_UPDATE) {
|
2021-11-03 04:00:15 +03:00
|
|
|
const idsToUpdate = message.ids;
|
|
|
|
const visible = message.visible;
|
2021-12-28 10:00:10 +03:00
|
|
|
this.updateMessagesVisibility(idsToUpdate, visible);
|
2021-11-17 04:46:05 +03:00
|
|
|
} else if (
|
2021-11-18 04:19:55 +03:00
|
|
|
renderableChatStyleMessages.includes(messageType) &&
|
2021-11-17 04:46:05 +03:00
|
|
|
messageVisible
|
|
|
|
) {
|
2021-12-28 10:00:10 +03:00
|
|
|
// Add new message to the chat feed.
|
|
|
|
this.addNewRenderableMessages([message]);
|
|
|
|
|
|
|
|
// Update the usernames list, filtering out our own name.
|
2021-07-26 08:26:27 +03:00
|
|
|
const updatedAllChatUserNames = this.updateAuthorList(message);
|
|
|
|
if (updatedAllChatUserNames.length) {
|
2021-07-26 08:27:02 +03:00
|
|
|
const updatedChatUserNames = updatedAllChatUserNames.filter(
|
|
|
|
(name) => name != username
|
|
|
|
);
|
2021-12-28 10:00:10 +03:00
|
|
|
this.setState((previousState) => {
|
|
|
|
return {
|
|
|
|
...previousState,
|
|
|
|
chatUserNames: [...updatedChatUserNames],
|
|
|
|
};
|
|
|
|
});
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
2020-12-23 04:43:35 +03:00
|
|
|
|
2021-12-28 10:00:10 +03:00
|
|
|
// Update the window title if needed.
|
|
|
|
this.handleWindowFocusNotificationCount(readonly, messageType);
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
2020-09-22 11:22:49 +03:00
|
|
|
|
2020-09-22 11:38:43 +03:00
|
|
|
websocketConnected() {
|
2021-12-28 10:00:10 +03:00
|
|
|
this.setState((previousState) => {
|
|
|
|
return {
|
|
|
|
...previousState,
|
|
|
|
webSocketConnected: true,
|
|
|
|
};
|
2020-09-22 11:38:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:28:47 +03:00
|
|
|
websocketDisconnected() {
|
2021-12-28 10:00:10 +03:00
|
|
|
this.setState((previousState) => {
|
|
|
|
return {
|
|
|
|
...previousState,
|
|
|
|
webSocketConnected: false,
|
|
|
|
};
|
2020-09-22 11:22:49 +03:00
|
|
|
});
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
submitChat(content) {
|
2021-06-08 03:37:20 +03:00
|
|
|
if (!content) {
|
|
|
|
return;
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
2020-08-16 04:28:29 +03:00
|
|
|
const message = {
|
|
|
|
body: content,
|
2021-07-20 05:23:06 +03:00
|
|
|
type: SOCKET_MESSAGE_TYPES.CHAT,
|
2020-08-16 04:28:29 +03:00
|
|
|
};
|
2021-06-08 03:37:20 +03:00
|
|
|
this.websocket.send(message);
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateAuthorList(message) {
|
|
|
|
const { type } = message;
|
2021-07-26 08:26:27 +03:00
|
|
|
let nameList = this.state.chatUserNames;
|
2020-08-14 14:19:19 +03:00
|
|
|
|
|
|
|
if (
|
|
|
|
type === SOCKET_MESSAGE_TYPES.CHAT &&
|
2021-07-20 05:22:29 +03:00
|
|
|
!nameList.includes(message.user.displayName)
|
2020-08-14 14:19:19 +03:00
|
|
|
) {
|
2021-07-26 08:27:02 +03:00
|
|
|
nameList.push(message.user.displayName);
|
2021-07-26 08:26:27 +03:00
|
|
|
return nameList;
|
2020-08-14 14:19:19 +03:00
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) {
|
2021-07-28 02:55:19 +03:00
|
|
|
const { oldName, user } = message;
|
2020-08-14 14:19:19 +03:00
|
|
|
const oldNameIndex = nameList.indexOf(oldName);
|
2021-07-28 02:55:19 +03:00
|
|
|
nameList.splice(oldNameIndex, 1, user.displayName);
|
2021-07-26 08:26:27 +03:00
|
|
|
return nameList;
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
2021-12-28 10:00:10 +03:00
|
|
|
|
2020-08-14 14:19:19 +03:00
|
|
|
return [];
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2020-09-14 08:02:04 +03:00
|
|
|
scrollToBottom() {
|
|
|
|
jumpToBottom(this.scrollableMessagesContainer.current);
|
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-09-22 06:11:09 +03:00
|
|
|
checkShouldScroll() {
|
2021-06-08 03:37:20 +03:00
|
|
|
const { scrollTop, scrollHeight, clientHeight } =
|
|
|
|
this.scrollableMessagesContainer.current;
|
2020-09-22 06:11:09 +03:00
|
|
|
const fullyScrolled = scrollHeight - clientHeight;
|
2021-06-08 03:37:20 +03:00
|
|
|
const shouldScroll =
|
|
|
|
scrollHeight >= clientHeight &&
|
|
|
|
fullyScrolled - scrollTop < MESSAGE_JUMPTOBOTTOM_BUFFER;
|
2021-08-30 02:58:37 +03:00
|
|
|
|
2020-12-04 09:43:31 +03:00
|
|
|
return shouldScroll;
|
2020-09-22 06:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleWindowResize() {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
|
2020-12-23 04:43:35 +03:00
|
|
|
handleWindowBlur() {
|
|
|
|
this.windowBlurred = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWindowFocus() {
|
|
|
|
this.windowBlurred = false;
|
|
|
|
this.numMessagesSinceBlur = 0;
|
|
|
|
window.document.title = this.props.instanceTitle;
|
|
|
|
}
|
|
|
|
|
2020-11-18 03:59:27 +03:00
|
|
|
// if the messages list grows in number of child message nodes due to new messages received, scroll to bottom.
|
|
|
|
messageListCallback(mutations) {
|
|
|
|
const numMutations = mutations.length;
|
2021-08-30 02:58:37 +03:00
|
|
|
|
2020-11-18 03:59:27 +03:00
|
|
|
if (numMutations) {
|
|
|
|
const item = mutations[numMutations - 1];
|
|
|
|
if (item.type === 'childList' && item.addedNodes.length) {
|
2020-12-07 08:01:52 +03:00
|
|
|
if (this.state.newMessagesReceived) {
|
2020-12-04 09:43:31 +03:00
|
|
|
if (!this.receivedFirstMessages) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
this.receivedFirstMessages = true;
|
|
|
|
} else if (this.checkShouldScroll()) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
2021-12-28 10:00:10 +03:00
|
|
|
|
|
|
|
this.setState((previousState) => {
|
|
|
|
return {
|
|
|
|
...previousState,
|
|
|
|
newMessagesReceived: false,
|
|
|
|
};
|
2020-12-07 08:01:52 +03:00
|
|
|
});
|
2020-11-18 03:59:27 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-23 04:43:35 +03:00
|
|
|
// update document title if window blurred
|
2021-06-08 03:37:20 +03:00
|
|
|
if (
|
|
|
|
this.numMessagesSinceBlur &&
|
2021-08-01 02:21:30 +03:00
|
|
|
!this.props.readonly &&
|
2021-06-08 03:37:20 +03:00
|
|
|
this.windowBlurred
|
|
|
|
) {
|
2020-12-23 04:43:35 +03:00
|
|
|
this.updateDocumentTitle();
|
|
|
|
}
|
2020-11-18 03:59:27 +03:00
|
|
|
}
|
2021-06-08 03:37:20 +03:00
|
|
|
}
|
2020-11-18 03:59:27 +03:00
|
|
|
|
2020-12-23 04:43:35 +03:00
|
|
|
updateDocumentTitle() {
|
2021-06-08 03:37:20 +03:00
|
|
|
const num =
|
|
|
|
this.numMessagesSinceBlur > 10 ? '10+' : this.numMessagesSinceBlur;
|
2020-12-23 04:43:35 +03:00
|
|
|
window.document.title = `${num} 💬 :: ${this.props.instanceTitle}`;
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:08:14 +03:00
|
|
|
render(props, state) {
|
2021-11-03 05:27:41 +03:00
|
|
|
const { username, readonly, chatInputEnabled, inputMaxBytes, accessToken } =
|
|
|
|
props;
|
2021-12-28 10:00:10 +03:00
|
|
|
const { sortedMessages, chatUserNames, webSocketConnected, isModerator } =
|
|
|
|
state;
|
|
|
|
|
|
|
|
const messageList = sortedMessages.map(
|
|
|
|
(message) =>
|
|
|
|
html`<${Message}
|
|
|
|
message=${message}
|
|
|
|
username=${username}
|
|
|
|
key=${message.id}
|
|
|
|
isModerator=${isModerator}
|
|
|
|
accessToken=${accessToken}
|
|
|
|
/>`
|
|
|
|
);
|
2020-09-14 08:02:04 +03:00
|
|
|
|
2021-08-01 02:21:30 +03:00
|
|
|
if (readonly) {
|
2020-09-14 08:02:04 +03:00
|
|
|
return html`
|
|
|
|
<div
|
|
|
|
id="messages-container"
|
|
|
|
ref=${this.scrollableMessagesContainer}
|
2020-09-22 06:11:09 +03:00
|
|
|
class="scrollbar-hidden py-1 overflow-auto"
|
2020-09-14 08:02:04 +03:00
|
|
|
>
|
|
|
|
${messageList}
|
|
|
|
</div>
|
|
|
|
`;
|
2020-08-19 10:16:35 +03:00
|
|
|
}
|
|
|
|
|
2020-09-13 04:46:08 +03:00
|
|
|
return html`
|
|
|
|
<section id="chat-container-wrap" class="flex flex-col">
|
|
|
|
<div
|
|
|
|
id="chat-container"
|
|
|
|
class="bg-gray-800 flex flex-col justify-end overflow-auto"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
id="messages-container"
|
|
|
|
ref=${this.scrollableMessagesContainer}
|
2020-09-22 06:11:09 +03:00
|
|
|
class="scrollbar-hidden py-1 overflow-auto z-10"
|
2020-09-13 04:46:08 +03:00
|
|
|
>
|
|
|
|
${messageList}
|
2020-08-13 11:28:25 +03:00
|
|
|
</div>
|
2020-09-13 04:46:08 +03:00
|
|
|
<${ChatInput}
|
|
|
|
chatUserNames=${chatUserNames}
|
2020-09-22 11:38:43 +03:00
|
|
|
inputEnabled=${webSocketConnected && chatInputEnabled}
|
2020-09-13 04:46:08 +03:00
|
|
|
handleSendMessage=${this.submitChat}
|
2021-07-20 05:22:29 +03:00
|
|
|
inputMaxBytes=${inputMaxBytes}
|
2020-09-13 04:46:08 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
`;
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
}
|