2023-10-17 04:41:40 -04:00
|
|
|
import React, { lazy, Suspense, useEffect, useState } from 'react'
|
2021-04-23 05:01:16 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
|
2020-10-12 06:25:59 -04:00
|
|
|
import MessageInput from './message-input'
|
|
|
|
import InfiniteScroll from './infinite-scroll'
|
2021-06-07 05:51:27 -04:00
|
|
|
import ChatFallbackError from './chat-fallback-error'
|
2020-10-12 06:25:59 -04:00
|
|
|
import Icon from '../../../shared/components/icon'
|
2021-02-23 05:17:41 -05:00
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
2021-06-25 04:14:07 -04:00
|
|
|
import { useUserContext } from '../../../shared/context/user-context'
|
2020-11-24 06:58:08 -05:00
|
|
|
import withErrorBoundary from '../../../infrastructure/error-boundary'
|
2021-06-07 05:51:27 -04:00
|
|
|
import { FetchError } from '../../../infrastructure/fetch-json'
|
2021-02-09 10:37:48 -05:00
|
|
|
import { useChatContext } from '../context/chat-context'
|
2021-10-06 04:33:24 -04:00
|
|
|
import LoadingSpinner from '../../../shared/components/loading-spinner'
|
2020-10-12 06:25:59 -04:00
|
|
|
|
2023-10-17 04:41:40 -04:00
|
|
|
const MessageList = lazy(() => import('./message-list'))
|
|
|
|
|
2021-06-21 06:02:38 -04:00
|
|
|
const ChatPane = React.memo(function ChatPane() {
|
2020-11-09 09:52:34 -05:00
|
|
|
const { t } = useTranslation()
|
2020-11-24 06:58:08 -05:00
|
|
|
|
2024-08-28 06:09:32 -04:00
|
|
|
const { chatIsOpen } = useLayoutContext()
|
2024-01-26 04:23:48 -05:00
|
|
|
const user = useUserContext()
|
2021-02-09 10:37:48 -05:00
|
|
|
|
|
|
|
const {
|
2021-04-23 05:01:16 -04:00
|
|
|
status,
|
|
|
|
messages,
|
|
|
|
initialMessagesLoaded,
|
2020-11-24 06:58:08 -05:00
|
|
|
atEnd,
|
2021-04-23 05:01:16 -04:00
|
|
|
loadInitialMessages,
|
2020-11-24 06:58:08 -05:00
|
|
|
loadMoreMessages,
|
2021-06-07 05:51:27 -04:00
|
|
|
reset,
|
2020-11-24 06:58:08 -05:00
|
|
|
sendMessage,
|
2021-04-27 03:52:58 -04:00
|
|
|
markMessagesAsRead,
|
2021-06-07 05:51:27 -04:00
|
|
|
error,
|
2021-02-09 10:37:48 -05:00
|
|
|
} = useChatContext()
|
2020-11-24 06:58:08 -05:00
|
|
|
|
2020-10-12 06:25:59 -04:00
|
|
|
useEffect(() => {
|
2021-01-11 06:57:38 -05:00
|
|
|
if (chatIsOpen && !initialMessagesLoaded) {
|
2021-04-23 05:01:16 -04:00
|
|
|
loadInitialMessages()
|
2021-01-11 06:57:38 -05:00
|
|
|
}
|
2021-04-23 05:01:16 -04:00
|
|
|
}, [chatIsOpen, loadInitialMessages, initialMessagesLoaded])
|
2020-10-12 06:25:59 -04:00
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
const shouldDisplayPlaceholder = status !== 'pending' && messages.length === 0
|
2020-10-12 06:25:59 -04:00
|
|
|
|
|
|
|
const messageContentCount = messages.reduce(
|
|
|
|
(acc, { contents }) => acc + contents.length,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
2023-10-17 04:41:40 -04:00
|
|
|
// Keep the chat pane in the DOM to avoid resetting the form input and re-rendering MathJax content.
|
|
|
|
const [chatOpenedOnce, setChatOpenedOnce] = useState(chatIsOpen)
|
|
|
|
useEffect(() => {
|
2024-08-28 06:09:32 -04:00
|
|
|
if (chatIsOpen) {
|
2023-10-17 04:41:40 -04:00
|
|
|
setChatOpenedOnce(true)
|
|
|
|
}
|
2024-08-28 06:09:32 -04:00
|
|
|
}, [chatIsOpen])
|
2023-10-17 04:41:40 -04:00
|
|
|
|
2021-06-07 05:51:27 -04:00
|
|
|
if (error) {
|
|
|
|
// let user try recover from fetch errors
|
|
|
|
if (error instanceof FetchError) {
|
|
|
|
return <ChatFallbackError reconnect={reset} />
|
|
|
|
}
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2024-08-29 10:39:47 -04:00
|
|
|
if (!user) {
|
2021-06-21 10:02:20 -04:00
|
|
|
return null
|
|
|
|
}
|
2023-10-17 04:41:40 -04:00
|
|
|
if (!chatOpenedOnce) {
|
|
|
|
return null
|
|
|
|
}
|
2021-06-21 10:02:20 -04:00
|
|
|
|
2020-10-12 06:25:59 -04:00
|
|
|
return (
|
|
|
|
<aside className="chat">
|
|
|
|
<InfiniteScroll
|
|
|
|
atEnd={atEnd}
|
|
|
|
className="messages"
|
|
|
|
fetchData={loadMoreMessages}
|
2021-04-23 05:01:16 -04:00
|
|
|
isLoading={status === 'pending'}
|
2020-10-12 06:25:59 -04:00
|
|
|
itemCount={messageContentCount}
|
|
|
|
>
|
|
|
|
<div>
|
2020-11-09 09:52:34 -05:00
|
|
|
<h2 className="sr-only">{t('chat')}</h2>
|
2023-10-17 04:41:40 -04:00
|
|
|
<Suspense fallback={<LoadingSpinner delay={500} />}>
|
|
|
|
{status === 'pending' && <LoadingSpinner delay={500} />}
|
|
|
|
{shouldDisplayPlaceholder && <Placeholder />}
|
|
|
|
<MessageList
|
|
|
|
messages={messages}
|
|
|
|
userId={user.id}
|
|
|
|
resetUnreadMessages={markMessagesAsRead}
|
|
|
|
/>
|
|
|
|
</Suspense>
|
2020-10-12 06:25:59 -04:00
|
|
|
</div>
|
|
|
|
</InfiniteScroll>
|
|
|
|
<MessageInput
|
2021-04-23 05:01:16 -04:00
|
|
|
resetUnreadMessages={markMessagesAsRead}
|
2020-10-12 06:25:59 -04:00
|
|
|
sendMessage={sendMessage}
|
|
|
|
/>
|
|
|
|
</aside>
|
|
|
|
)
|
2021-06-21 06:02:38 -04:00
|
|
|
})
|
2020-10-12 06:25:59 -04:00
|
|
|
|
|
|
|
function Placeholder() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="no-messages text-center small">{t('no_messages')}</div>
|
|
|
|
<div className="first-message text-center">
|
|
|
|
{t('send_first_message')}
|
|
|
|
<br />
|
|
|
|
<Icon type="arrow-down" />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-07 05:51:27 -04:00
|
|
|
export default withErrorBoundary(ChatPane, ChatFallbackError)
|