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,
|
2024-01-26 04:23:48 -05:00
|
|
|
FC,
|
2021-02-09 10:37:48 -05:00
|
|
|
} from 'react'
|
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'
|
2023-10-27 05:06:25 -04:00
|
|
|
import { useIdeContext } from '@/shared/context/ide-context'
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
const PAGE_SIZE = 50
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export type Message = {
|
|
|
|
id: string
|
|
|
|
timestamp: number
|
|
|
|
contents: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
status: 'idle' | 'pending' | 'error'
|
|
|
|
messages: Message[]
|
|
|
|
initialMessagesLoaded: boolean
|
|
|
|
lastTimestamp: number | null
|
|
|
|
atEnd: boolean
|
|
|
|
unreadMessageCount: number
|
|
|
|
error?: Error | null
|
|
|
|
uniqueMessageIds: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
type Action =
|
|
|
|
| {
|
|
|
|
type: 'INITIAL_FETCH_MESSAGES'
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'FETCH_MESSAGES'
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'FETCH_MESSAGES_SUCCESS'
|
|
|
|
messages: Message[]
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'SEND_MESSAGE'
|
|
|
|
user: any
|
|
|
|
content: any
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'RECEIVE_MESSAGE'
|
|
|
|
message: any
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'MARK_MESSAGES_AS_READ'
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'CLEAR'
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'ERROR'
|
|
|
|
error: any
|
|
|
|
}
|
|
|
|
|
|
|
|
function chatReducer(state: State, action: Action): State {
|
2021-04-23 05:01:16 -04:00
|
|
|
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',
|
2023-04-19 03:55:25 -04:00
|
|
|
...prependMessages(
|
|
|
|
state.messages,
|
|
|
|
action.messages,
|
|
|
|
state.uniqueMessageIds
|
|
|
|
),
|
2021-04-23 05:01:16 -04:00
|
|
|
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,
|
2023-04-19 03:55:25 -04:00
|
|
|
...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,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
},
|
|
|
|
state.uniqueMessageIds
|
|
|
|
),
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'RECEIVE_MESSAGE':
|
|
|
|
return {
|
|
|
|
...state,
|
2023-04-19 03:55:25 -04:00
|
|
|
...appendMessage(
|
|
|
|
state.messages,
|
|
|
|
action.message,
|
|
|
|
state.uniqueMessageIds
|
|
|
|
),
|
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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const initialState: State = {
|
2021-04-23 05:01:16 -04:00
|
|
|
status: 'idle',
|
|
|
|
messages: [],
|
|
|
|
initialMessagesLoaded: false,
|
|
|
|
lastTimestamp: null,
|
|
|
|
atEnd: false,
|
|
|
|
unreadMessageCount: 0,
|
2021-04-27 03:52:58 -04:00
|
|
|
error: null,
|
2023-04-19 03:55:25 -04:00
|
|
|
uniqueMessageIds: [],
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export const ChatContext = createContext<
|
|
|
|
| {
|
|
|
|
status: 'idle' | 'pending' | 'error'
|
|
|
|
messages: Message[]
|
|
|
|
initialMessagesLoaded: boolean
|
|
|
|
atEnd: boolean
|
|
|
|
unreadMessageCount: number
|
|
|
|
loadInitialMessages: () => void
|
|
|
|
loadMoreMessages: () => void
|
|
|
|
sendMessage: (message: any) => void
|
|
|
|
markMessagesAsRead: () => void
|
|
|
|
reset: () => void
|
|
|
|
error?: Error | null
|
|
|
|
}
|
|
|
|
| undefined
|
|
|
|
>(undefined)
|
2021-02-23 05:17:41 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export const ChatProvider: FC = ({ children }) => {
|
2024-02-01 04:41:12 -05:00
|
|
|
const clientId = useRef<string>()
|
|
|
|
if (clientId.current === undefined) {
|
|
|
|
clientId.current = uuid()
|
|
|
|
}
|
2024-01-26 04:23:48 -05:00
|
|
|
const user = useUserContext()
|
|
|
|
const { _id: projectId } = useProjectContext()
|
2021-02-23 05:17:41 -05:00
|
|
|
|
2023-10-18 05:14:13 -04:00
|
|
|
const { chatIsOpen } = useLayoutContext()
|
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
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const query: Record<string, string> = {
|
|
|
|
limit: String(PAGE_SIZE),
|
|
|
|
}
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
if (state.lastTimestamp) {
|
2024-01-26 04:23:48 -05:00
|
|
|
query.before = String(state.lastTimestamp)
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
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
|
2023-10-27 05:06:25 -04:00
|
|
|
const { socket } = useIdeContext()
|
2021-04-23 05:01:16 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (!socket) return
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function receivedMessage(message: any) {
|
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>
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export function useChatContext() {
|
|
|
|
const context = useContext(ChatContext)
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('useChatContext is only available inside ChatProvider')
|
|
|
|
}
|
|
|
|
return context
|
2021-02-09 10:37:48 -05:00
|
|
|
}
|