2021-04-23 05:01:16 -04:00
|
|
|
import React, { useEffect } from 'react'
|
2021-02-23 05:17:41 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-04-23 05:01:16 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
|
2020-10-12 06:25:59 -04:00
|
|
|
import MessageList from './message-list'
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2021-02-23 05:17:41 -05:00
|
|
|
const { chatIsOpen } = useLayoutContext({ chatIsOpen: PropTypes.bool })
|
2021-06-25 04:14:07 -04:00
|
|
|
const user = useUserContext({
|
|
|
|
id: PropTypes.string.isRequired,
|
2021-05-27 05:30:24 -04:00
|
|
|
})
|
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
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:02:20 -04:00
|
|
|
if (!user) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
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>
|
2021-04-23 05:01:16 -04:00
|
|
|
{status === 'pending' && <LoadingSpinner />}
|
2020-10-12 06:25:59 -04:00
|
|
|
{shouldDisplayPlaceholder && <Placeholder />}
|
|
|
|
<MessageList
|
|
|
|
messages={messages}
|
2021-04-23 05:01:16 -04:00
|
|
|
userId={user.id}
|
|
|
|
resetUnreadMessages={markMessagesAsRead}
|
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)
|