2021-02-09 10:37:48 -05:00
|
|
|
import React, {
|
|
|
|
createContext,
|
|
|
|
useCallback,
|
|
|
|
useContext,
|
2021-04-23 05:01:16 -04:00
|
|
|
useEffect,
|
|
|
|
useReducer,
|
2021-04-27 03:52:58 -04:00
|
|
|
useMemo,
|
2021-02-09 10:37:48 -05:00
|
|
|
} from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2021-04-23 05:01:16 -04:00
|
|
|
import { v4 as uuid } from 'uuid'
|
|
|
|
|
2021-02-09 10:37:48 -05:00
|
|
|
import { useApplicationContext } from '../../../shared/context/application-context'
|
|
|
|
import { useEditorContext } from '../../../shared/context/editor-context'
|
2021-04-23 05:01:16 -04:00
|
|
|
import { getJSON, postJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import { appendMessage, prependMessages } from '../utils/message-list-appender'
|
2021-05-18 06:56:56 -04:00
|
|
|
import useBrowserWindow from '../../../shared/hooks/use-browser-window'
|
2021-02-23 05:17:41 -05:00
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
const PAGE_SIZE = 50
|
|
|
|
|
|
|
|
export function chatReducer(state, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'INITIAL_FETCH_MESSAGES':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
status: 'pending',
|
2021-04-27 03:52:58 -04:00
|
|
|
initialMessagesLoaded: true,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'FETCH_MESSAGES':
|
|
|
|
return {
|
|
|
|
...state,
|
2021-04-27 03:52:58 -04:00
|
|
|
status: 'pending',
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'FETCH_MESSAGES_SUCCESS':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
status: 'idle',
|
|
|
|
messages: prependMessages(state.messages, action.messages),
|
|
|
|
lastTimestamp: action.messages[0] ? action.messages[0].timestamp : null,
|
2021-04-27 03:52:58 -04:00
|
|
|
atEnd: action.messages.length < PAGE_SIZE,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'SEND_MESSAGE':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
messages: appendMessage(state.messages, {
|
|
|
|
// Messages are sent optimistically, so don't have an id (used for
|
|
|
|
// React keys). The uuid is valid for this session, and ensures all
|
|
|
|
// messages have an id. It will be overwritten by the actual ids on
|
|
|
|
// refresh
|
|
|
|
id: uuid(),
|
|
|
|
user: action.user,
|
|
|
|
content: action.content,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp: Date.now(),
|
2021-04-23 05:01:16 -04:00
|
|
|
}),
|
2021-04-27 03:52:58 -04:00
|
|
|
messageWasJustSent: true,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'RECEIVE_MESSAGE':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
messages: appendMessage(state.messages, action.message),
|
|
|
|
messageWasJustSent: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
unreadMessageCount: state.unreadMessageCount + 1,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'MARK_MESSAGES_AS_READ':
|
|
|
|
return {
|
|
|
|
...state,
|
2021-04-27 03:52:58 -04:00
|
|
|
unreadMessageCount: 0,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-07 05:51:27 -04:00
|
|
|
case 'CLEAR':
|
|
|
|
return { ...initialState }
|
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
case 'ERROR':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
status: 'error',
|
2021-04-27 03:52:58 -04:00
|
|
|
error: action.error,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown action')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
status: 'idle',
|
|
|
|
messages: [],
|
|
|
|
initialMessagesLoaded: false,
|
|
|
|
lastTimestamp: null,
|
|
|
|
atEnd: false,
|
|
|
|
messageWasJustSent: false,
|
|
|
|
unreadMessageCount: 0,
|
2021-04-27 03:52:58 -04:00
|
|
|
error: null,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
2021-02-09 10:37:48 -05:00
|
|
|
export const ChatContext = createContext()
|
|
|
|
|
2021-02-23 05:17:41 -05:00
|
|
|
ChatContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
2021-04-23 05:01:16 -04:00
|
|
|
status: PropTypes.string.isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
messages: PropTypes.array.isRequired,
|
2021-04-23 05:01:16 -04:00
|
|
|
initialMessagesLoaded: PropTypes.bool.isRequired,
|
|
|
|
atEnd: PropTypes.bool.isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
unreadMessageCount: PropTypes.number.isRequired,
|
2021-04-23 05:01:16 -04:00
|
|
|
loadInitialMessages: PropTypes.func.isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
loadMoreMessages: PropTypes.func.isRequired,
|
2021-04-23 05:01:16 -04:00
|
|
|
sendMessage: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
markMessagesAsRead: PropTypes.func.isRequired,
|
2021-06-07 05:51:27 -04:00
|
|
|
reset: PropTypes.func.isRequired,
|
|
|
|
error: PropTypes.object,
|
2021-04-27 03:52:58 -04:00
|
|
|
}).isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
}
|
|
|
|
|
2021-02-09 10:37:48 -05:00
|
|
|
export function ChatProvider({ children }) {
|
2021-02-23 05:17:41 -05:00
|
|
|
const { user } = useApplicationContext({
|
2021-04-27 03:52:58 -04:00
|
|
|
user: PropTypes.shape({ id: PropTypes.string.isRequired }.isRequired),
|
2021-02-23 05:17:41 -05:00
|
|
|
})
|
|
|
|
const { projectId } = useEditorContext({
|
2021-04-27 03:52:58 -04:00
|
|
|
projectId: PropTypes.string.isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
const { chatIsOpen } = useLayoutContext({ chatIsOpen: PropTypes.bool })
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
const {
|
|
|
|
hasFocus: windowHasFocus,
|
|
|
|
flashTitle,
|
2021-04-27 03:52:58 -04:00
|
|
|
stopFlashingTitle,
|
2021-04-23 05:01:16 -04:00
|
|
|
} = useBrowserWindow()
|
|
|
|
|
|
|
|
const [state, dispatch] = useReducer(chatReducer, initialState)
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2021-06-07 05:51:27 -04:00
|
|
|
const { loadInitialMessages, loadMoreMessages, reset } = useMemo(() => {
|
2021-04-23 05:01:16 -04:00
|
|
|
function fetchMessages() {
|
|
|
|
if (state.atEnd) return
|
2021-04-22 04:37:26 -04:00
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
const query = { limit: PAGE_SIZE }
|
|
|
|
|
|
|
|
if (state.lastTimestamp) {
|
|
|
|
query.before = state.lastTimestamp
|
|
|
|
}
|
2021-04-22 04:37:26 -04:00
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
const queryString = new URLSearchParams(query)
|
|
|
|
const url = `/project/${projectId}/messages?${queryString.toString()}`
|
|
|
|
|
2021-06-07 05:51:27 -04:00
|
|
|
getJSON(url)
|
|
|
|
.then((messages = []) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'FETCH_MESSAGES_SUCCESS',
|
|
|
|
messages: messages.reverse(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
dispatch({
|
|
|
|
type: 'ERROR',
|
|
|
|
error: error,
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadInitialMessages() {
|
|
|
|
if (state.initialMessagesLoaded) return
|
|
|
|
|
|
|
|
dispatch({ type: 'INITIAL_FETCH_MESSAGES' })
|
|
|
|
fetchMessages()
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadMoreMessages() {
|
|
|
|
dispatch({ type: 'FETCH_MESSAGES' })
|
|
|
|
fetchMessages()
|
|
|
|
}
|
|
|
|
|
2021-06-07 05:51:27 -04:00
|
|
|
function reset() {
|
|
|
|
dispatch({ type: 'CLEAR' })
|
|
|
|
fetchMessages()
|
|
|
|
}
|
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
return {
|
|
|
|
loadInitialMessages,
|
2021-04-27 03:52:58 -04:00
|
|
|
loadMoreMessages,
|
2021-06-07 05:51:27 -04:00
|
|
|
reset,
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
}, [projectId, state.atEnd, state.initialMessagesLoaded, state.lastTimestamp])
|
|
|
|
|
|
|
|
const sendMessage = useCallback(
|
|
|
|
content => {
|
|
|
|
if (!content) return
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'SEND_MESSAGE',
|
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
content,
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const url = `/project/${projectId}/messages`
|
|
|
|
postJSON(url, {
|
2021-04-27 03:52:58 -04:00
|
|
|
body: { content },
|
2021-06-07 05:51:27 -04:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({
|
|
|
|
type: 'ERROR',
|
|
|
|
error: error,
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
[projectId, user]
|
|
|
|
)
|
|
|
|
|
|
|
|
const markMessagesAsRead = useCallback(() => {
|
|
|
|
dispatch({ type: 'MARK_MESSAGES_AS_READ' })
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
// Handling receiving messages over the socket
|
|
|
|
const socket = window._ide?.socket
|
|
|
|
useEffect(() => {
|
|
|
|
if (!socket) return
|
|
|
|
|
|
|
|
function receivedMessage(message) {
|
|
|
|
// If the message is from the current user and they just sent a message,
|
|
|
|
// then we are receiving the sent message back from the socket. Ignore it
|
|
|
|
// to prevent double message
|
|
|
|
const messageIsFromSelf = message?.user?.id === user.id
|
|
|
|
if (messageIsFromSelf && state.messageWasJustSent) return
|
|
|
|
|
|
|
|
dispatch({ type: 'RECEIVE_MESSAGE', message })
|
|
|
|
|
|
|
|
// Temporary workaround to pass state to unread message balloon in Angular
|
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent('Chat.MessageReceived', { detail: { message } })
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('new-chat-message', receivedMessage)
|
|
|
|
return () => {
|
|
|
|
if (!socket) return
|
|
|
|
|
|
|
|
socket.removeListener('new-chat-message', receivedMessage)
|
|
|
|
}
|
|
|
|
// We're adding and removing the socket listener every time we send a
|
|
|
|
// message (and messageWasJustSent changes). Not great, but no good way
|
|
|
|
// around it
|
|
|
|
}, [socket, state.messageWasJustSent, state.unreadMessageCount, user.id])
|
|
|
|
|
|
|
|
// Handle unread messages
|
2021-02-09 10:37:48 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (windowHasFocus) {
|
|
|
|
stopFlashingTitle()
|
|
|
|
if (chatIsOpen) {
|
2021-04-23 05:01:16 -04:00
|
|
|
markMessagesAsRead()
|
2021-02-09 10:37:48 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 05:01:16 -04:00
|
|
|
if (!windowHasFocus && state.unreadMessageCount > 0) {
|
2021-02-09 10:37:48 -05:00
|
|
|
flashTitle('New Message')
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
windowHasFocus,
|
|
|
|
chatIsOpen,
|
2021-04-23 05:01:16 -04:00
|
|
|
state.unreadMessageCount,
|
2021-02-09 10:37:48 -05:00
|
|
|
flashTitle,
|
2021-04-23 05:01:16 -04:00
|
|
|
stopFlashingTitle,
|
2021-04-27 03:52:58 -04:00
|
|
|
markMessagesAsRead,
|
2021-02-09 10:37:48 -05:00
|
|
|
])
|
|
|
|
|
|
|
|
const value = {
|
2021-04-23 05:01:16 -04:00
|
|
|
status: state.status,
|
|
|
|
messages: state.messages,
|
|
|
|
initialMessagesLoaded: state.initialMessagesLoaded,
|
|
|
|
atEnd: state.atEnd,
|
|
|
|
unreadMessageCount: state.unreadMessageCount,
|
|
|
|
loadInitialMessages,
|
2021-02-09 10:37:48 -05:00
|
|
|
loadMoreMessages,
|
2021-06-07 05:51:27 -04:00
|
|
|
reset,
|
2021-04-23 05:01:16 -04:00
|
|
|
sendMessage,
|
2021-04-27 03:52:58 -04:00
|
|
|
markMessagesAsRead,
|
2021-06-07 05:51:27 -04:00
|
|
|
error: state.error,
|
2021-02-09 10:37:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatProvider.propTypes = {
|
2021-04-27 03:52:58 -04:00
|
|
|
children: PropTypes.any,
|
2021-02-09 10:37:48 -05:00
|
|
|
}
|
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
export function useChatContext(propTypes) {
|
|
|
|
const data = useContext(ChatContext)
|
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'ChatContext.Provider')
|
|
|
|
return data
|
2021-02-09 10:37:48 -05:00
|
|
|
}
|