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-10-14 02:45:52 +03:00
|
|
|
import '/js/web_modules/@justinribeiro/lite-youtube.js';
|
|
|
|
|
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';
|
2020-09-22 06:11:09 +03:00
|
|
|
import { jumpToBottom, debounce } from '../../utils/helpers.js';
|
2020-08-24 05:06:58 +03:00
|
|
|
import { extraUserNamesFromMessageHistory } from '../../utils/chat.js';
|
2020-09-22 06:11:09 +03:00
|
|
|
import { URL_CHAT_HISTORY, MESSAGE_JUMPTOBOTTOM_BUFFER } from '../../utils/constants.js';
|
2020-08-13 11:28:25 +03:00
|
|
|
|
|
|
|
export default class Chat extends Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
2020-09-22 11:38:43 +03:00
|
|
|
webSocketConnected: true,
|
2020-08-13 11:28:25 +03:00
|
|
|
messages: [],
|
|
|
|
chatUserNames: [],
|
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-08-13 19:28:47 +03:00
|
|
|
|
|
|
|
this.getChatHistory = this.getChatHistory.bind(this);
|
|
|
|
this.receivedWebsocketMessage = this.receivedWebsocketMessage.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-16 04:28:29 +03:00
|
|
|
this.submitChat = this.submitChat.bind(this);
|
2020-09-14 08:02:04 +03:00
|
|
|
this.submitChat = this.submitChat.bind(this);
|
|
|
|
this.scrollToBottom = this.scrollToBottom.bind(this);
|
2020-09-22 06:11:09 +03:00
|
|
|
this.handleWindowResize = debounce(this.handleWindowResize.bind(this), 500);
|
2020-11-18 03:59:27 +03:00
|
|
|
|
|
|
|
this.messageListCallback = this.messageListCallback.bind(this);
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-08-14 14:19:19 +03:00
|
|
|
this.setupWebSocketCallbacks();
|
2020-08-13 19:28:47 +03:00
|
|
|
this.getChatHistory();
|
2020-09-22 06:11:09 +03:00
|
|
|
window.addEventListener('resize', this.handleWindowResize);
|
2020-11-18 03:59:27 +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-08-24 08:21:42 +03:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2020-08-13 11:28:25 +03:00
|
|
|
const { username: prevName } = prevProps;
|
2020-10-14 14:33:55 +03:00
|
|
|
const { username } = this.props;
|
2020-08-24 08:21:42 +03:00
|
|
|
|
|
|
|
const { messages: prevMessages } = prevState;
|
|
|
|
const { messages } = this.state;
|
|
|
|
|
2020-08-13 11:28:25 +03:00
|
|
|
// if username updated, send a message
|
|
|
|
if (prevName !== username) {
|
2020-10-14 14:33:55 +03:00
|
|
|
this.sendUsernameChange(prevName, username);
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
2020-11-18 03:59:27 +03:00
|
|
|
|
2020-08-24 08:21:42 +03:00
|
|
|
// scroll to bottom of messages list when new ones come in
|
|
|
|
if (messages.length > prevMessages.length) {
|
2020-11-18 03:59:27 +03:00
|
|
|
this.newMessagesReceived = true;
|
2020-08-24 08:21:42 +03:00
|
|
|
}
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
2020-08-24 22:34:36 +03:00
|
|
|
componentWillUnmount() {
|
2020-09-22 06:11:09 +03:00
|
|
|
window.removeEventListener('resize', this.handleWindowResize);
|
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) {
|
|
|
|
this.websocket.addListener(CALLBACKS.RAW_WEBSOCKET_MESSAGE_RECEIVED, this.receivedWebsocketMessage);
|
2020-09-22 11:38:43 +03:00
|
|
|
this.websocket.addListener(CALLBACKS.WEBSOCKET_CONNECTED, this.websocketConnected);
|
2020-08-14 14:19:19 +03:00
|
|
|
this.websocket.addListener(CALLBACKS.WEBSOCKET_DISCONNECTED, this.websocketDisconnected);
|
|
|
|
}
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch chat history
|
|
|
|
getChatHistory() {
|
|
|
|
fetch(URL_CHAT_HISTORY)
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network response was not ok ${response.ok}`);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(data => {
|
2020-08-16 04:41:31 +03:00
|
|
|
// extra user names
|
|
|
|
const chatUserNames = extraUserNamesFromMessageHistory(data);
|
2020-08-13 19:28:47 +03:00
|
|
|
this.setState({
|
|
|
|
messages: data,
|
2020-08-16 04:41:31 +03:00
|
|
|
chatUserNames,
|
2020-08-13 19:28:47 +03:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2020-08-14 14:19:19 +03:00
|
|
|
// this.handleNetworkingError(`Fetch getChatHistory: ${error}`);
|
2020-08-13 19:28:47 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:33:55 +03:00
|
|
|
sendUsernameChange(oldName, newName) {
|
2020-08-13 11:28:25 +03:00
|
|
|
const nameChange = {
|
|
|
|
type: SOCKET_MESSAGE_TYPES.NAME_CHANGE,
|
2020-08-26 10:51:40 +03:00
|
|
|
oldName,
|
|
|
|
newName,
|
2020-08-13 11:28:25 +03:00
|
|
|
};
|
2020-08-14 14:19:19 +03:00
|
|
|
this.websocket.send(nameChange);
|
2020-08-13 19:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
receivedWebsocketMessage(message) {
|
|
|
|
this.addMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
addMessage(message) {
|
|
|
|
const { messages: curMessages } = this.state;
|
2020-08-26 10:51:40 +03:00
|
|
|
|
|
|
|
// if incoming message has same id as existing message, don't add it
|
2020-08-13 19:28:47 +03:00
|
|
|
const existing = curMessages.filter(function (item) {
|
|
|
|
return item.id === message.id;
|
|
|
|
})
|
2020-08-26 10:51:40 +03:00
|
|
|
|
2020-08-13 19:28:47 +03:00
|
|
|
if (existing.length === 0 || !existing) {
|
2020-08-14 14:19:19 +03:00
|
|
|
const newState = {
|
2020-08-13 19:28:47 +03:00
|
|
|
messages: [...curMessages, message],
|
2020-08-14 14:19:19 +03:00
|
|
|
};
|
|
|
|
const updatedChatUserNames = this.updateAuthorList(message);
|
|
|
|
if (updatedChatUserNames.length) {
|
|
|
|
newState.chatUserNames = [...updatedChatUserNames];
|
|
|
|
}
|
|
|
|
this.setState(newState);
|
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() {
|
|
|
|
this.setState({
|
|
|
|
webSocketConnected: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:28:47 +03:00
|
|
|
websocketDisconnected() {
|
2020-09-22 11:22:49 +03:00
|
|
|
this.setState({
|
2020-09-22 11:38:43 +03:00
|
|
|
webSocketConnected: false,
|
2020-09-22 11:22:49 +03:00
|
|
|
});
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
submitChat(content) {
|
|
|
|
if (!content) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-14 14:33:55 +03:00
|
|
|
const { username } = this.props;
|
2020-08-16 04:28:29 +03:00
|
|
|
const message = {
|
|
|
|
body: content,
|
2020-08-14 14:19:19 +03:00
|
|
|
author: username,
|
|
|
|
type: SOCKET_MESSAGE_TYPES.CHAT,
|
2020-08-16 04:28:29 +03:00
|
|
|
};
|
2020-08-14 14:19:19 +03:00
|
|
|
this.websocket.send(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAuthorList(message) {
|
|
|
|
const { type } = message;
|
|
|
|
const nameList = this.state.chatUserNames;
|
|
|
|
|
|
|
|
if (
|
|
|
|
type === SOCKET_MESSAGE_TYPES.CHAT &&
|
|
|
|
!nameList.includes(message.author)
|
|
|
|
) {
|
|
|
|
return nameList.push(message.author);
|
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) {
|
|
|
|
const { oldName, newName } = message;
|
|
|
|
const oldNameIndex = nameList.indexOf(oldName);
|
|
|
|
return nameList.splice(oldNameIndex, 1, newName);
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = this.scrollableMessagesContainer.current;
|
|
|
|
const fullyScrolled = scrollHeight - clientHeight;
|
2020-12-04 09:43:31 +03:00
|
|
|
const shouldScroll = scrollHeight >= clientHeight && fullyScrolled - scrollTop < MESSAGE_JUMPTOBOTTOM_BUFFER;
|
|
|
|
return shouldScroll;
|
2020-09-22 06:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleWindowResize() {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (numMutations) {
|
|
|
|
const item = mutations[numMutations - 1];
|
|
|
|
if (item.type === 'childList' && item.addedNodes.length) {
|
2020-12-04 09:43:31 +03:00
|
|
|
if (this.newMessagesReceived) {
|
|
|
|
if (!this.receivedFirstMessages) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
this.receivedFirstMessages = true;
|
|
|
|
} else if (this.checkShouldScroll()) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
2020-11-18 03:59:27 +03:00
|
|
|
this.newMessagesReceived = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-13 12:08:14 +03:00
|
|
|
render(props, state) {
|
2020-09-13 04:46:08 +03:00
|
|
|
const { username, messagesOnly, chatInputEnabled } = props;
|
2020-09-22 11:38:43 +03:00
|
|
|
const { messages, chatUserNames, webSocketConnected } = state;
|
2020-09-14 08:02:04 +03:00
|
|
|
|
|
|
|
const messageList = messages.map(
|
|
|
|
(message) =>
|
|
|
|
html`<${Message}
|
|
|
|
message=${message}
|
|
|
|
username=${username}
|
|
|
|
key=${message.id}
|
|
|
|
/>`
|
|
|
|
);
|
|
|
|
|
2020-08-19 10:16:35 +03:00
|
|
|
if (messagesOnly) {
|
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}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
`;
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 05:06:58 +03:00
|
|
|
|