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'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
2021-02-23 05:17:41 -05:00
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
2021-04-23 05:01:16 -04:00
|
|
|
import { useApplicationContext } from '../../../shared/context/application-context'
|
2020-11-24 06:58:08 -05:00
|
|
|
import withErrorBoundary from '../../../infrastructure/error-boundary'
|
2021-02-09 10:37:48 -05:00
|
|
|
import { useChatContext } from '../context/chat-context'
|
2020-10-12 06:25:59 -04:00
|
|
|
|
2021-02-09 10:37:48 -05:00
|
|
|
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-04-23 05:01:16 -04:00
|
|
|
const { user } = useApplicationContext()
|
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,
|
|
|
|
sendMessage,
|
2021-04-23 05:01:16 -04:00
|
|
|
markMessagesAsRead
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function LoadingSpinner() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
|
|
<div className="loading">
|
|
|
|
<Icon type="fw" modifier="refresh" spin />
|
2020-10-27 06:52:40 -04:00
|
|
|
{` ${t('loading')}…`}
|
2020-10-12 06:25:59 -04:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
export default withErrorBoundary(ChatPane)
|