2021-06-23 05:37:08 -04:00
|
|
|
import {
|
2021-02-09 10:37:48 -05:00
|
|
|
createContext,
|
|
|
|
useCallback,
|
|
|
|
useContext,
|
2021-04-23 05:01:16 -04:00
|
|
|
useEffect,
|
|
|
|
useReducer,
|
2021-04-27 03:52:58 -04:00
|
|
|
useMemo,
|
2022-10-31 11:04:49 -04:00
|
|
|
useRef,
|
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-06-25 04:14:07 -04:00
|
|
|
import { useUserContext } from '../../../shared/context/user-context'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-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
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'RECEIVE_MESSAGE':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
messages: appendMessage(state.messages, action.message),
|
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,
|
|
|
|
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 }) {
|
2022-10-31 11:04:49 -04:00
|
|
|
const clientId = useRef(uuid())
|
2021-06-25 04:14:07 -04:00
|
|
|
const user = useUserContext({
|
|
|
|
id: PropTypes.string.isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
})
|
2021-06-25 04:13:17 -04:00
|
|
|
const { _id: projectId } = useProjectContext({
|
|
|
|
_id: 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',
|
2022-05-16 10:25:49 -04:00
|
|
|
error,
|
2021-06-07 05:51:27 -04:00
|
|
|
})
|
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, {
|
2022-10-31 11:04:49 -04:00
|
|
|
body: { content, client_id: clientId.current },
|
2021-06-07 05:51:27 -04:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({
|
|
|
|
type: 'ERROR',
|
2022-05-16 10:25:49 -04:00
|
|
|
error,
|
2021-06-07 05:51:27 -04:00
|
|
|
})
|
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) {
|
2021-12-14 05:51:33 -05:00
|
|
|
// If the message is from the current client id, then we are receiving the sent message back from the socket.
|
|
|
|
// Ignore it to prevent double message.
|
2022-10-31 11:04:49 -04:00
|
|
|
if (message.clientId === clientId.current) return
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
dispatch({ type: 'RECEIVE_MESSAGE', message })
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('new-chat-message', receivedMessage)
|
|
|
|
return () => {
|
|
|
|
if (!socket) return
|
|
|
|
|
|
|
|
socket.removeListener('new-chat-message', receivedMessage)
|
|
|
|
}
|
2021-12-14 05:51:33 -05:00
|
|
|
}, [socket])
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
])
|
|
|
|
|
2021-06-21 06:02:38 -04:00
|
|
|
const value = useMemo(
|
|
|
|
() => ({
|
|
|
|
status: state.status,
|
|
|
|
messages: state.messages,
|
|
|
|
initialMessagesLoaded: state.initialMessagesLoaded,
|
|
|
|
atEnd: state.atEnd,
|
|
|
|
unreadMessageCount: state.unreadMessageCount,
|
|
|
|
loadInitialMessages,
|
|
|
|
loadMoreMessages,
|
|
|
|
reset,
|
|
|
|
sendMessage,
|
|
|
|
markMessagesAsRead,
|
|
|
|
error: state.error,
|
|
|
|
}),
|
|
|
|
[
|
|
|
|
loadInitialMessages,
|
|
|
|
loadMoreMessages,
|
|
|
|
markMessagesAsRead,
|
|
|
|
reset,
|
|
|
|
sendMessage,
|
|
|
|
state.atEnd,
|
|
|
|
state.error,
|
|
|
|
state.initialMessagesLoaded,
|
|
|
|
state.messages,
|
|
|
|
state.status,
|
|
|
|
state.unreadMessageCount,
|
|
|
|
]
|
|
|
|
)
|
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
|
|
|
}
|