2021-01-11 06:57:38 -05:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2021-02-23 05:17:41 -05:00
|
|
|
import PropTypes from 'prop-types'
|
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'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-02-23 05:17:41 -05:00
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-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-02-09 10:37:48 -05:00
|
|
|
|
|
|
|
const {
|
|
|
|
userId,
|
2020-11-24 06:58:08 -05:00
|
|
|
atEnd,
|
|
|
|
loading,
|
|
|
|
loadMoreMessages,
|
|
|
|
messages,
|
|
|
|
sendMessage,
|
2021-02-09 10:37:48 -05:00
|
|
|
resetUnreadMessageCount
|
|
|
|
} = useChatContext()
|
2020-11-24 06:58:08 -05:00
|
|
|
|
2021-01-11 06:57:38 -05:00
|
|
|
const [initialMessagesLoaded, setInitialMessagesLoaded] = useState(false)
|
|
|
|
|
2020-10-12 06:25:59 -04:00
|
|
|
useEffect(() => {
|
2021-01-11 06:57:38 -05:00
|
|
|
if (chatIsOpen && !initialMessagesLoaded) {
|
|
|
|
loadMoreMessages()
|
|
|
|
setInitialMessagesLoaded(true)
|
|
|
|
}
|
|
|
|
}, [initialMessagesLoaded, loadMoreMessages, chatIsOpen])
|
2020-10-12 06:25:59 -04:00
|
|
|
|
|
|
|
const shouldDisplayPlaceholder = !loading && messages.length === 0
|
|
|
|
|
|
|
|
const messageContentCount = messages.reduce(
|
|
|
|
(acc, { contents }) => acc + contents.length,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<aside className="chat">
|
|
|
|
<InfiniteScroll
|
|
|
|
atEnd={atEnd}
|
|
|
|
className="messages"
|
|
|
|
fetchData={loadMoreMessages}
|
|
|
|
isLoading={loading}
|
|
|
|
itemCount={messageContentCount}
|
|
|
|
>
|
|
|
|
<div>
|
2020-11-09 09:52:34 -05:00
|
|
|
<h2 className="sr-only">{t('chat')}</h2>
|
2020-10-12 06:25:59 -04:00
|
|
|
{loading && <LoadingSpinner />}
|
|
|
|
{shouldDisplayPlaceholder && <Placeholder />}
|
|
|
|
<MessageList
|
|
|
|
messages={messages}
|
|
|
|
userId={userId}
|
2021-02-09 10:37:48 -05:00
|
|
|
resetUnreadMessages={resetUnreadMessageCount}
|
2020-10-12 06:25:59 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</InfiniteScroll>
|
|
|
|
<MessageInput
|
2021-02-09 10:37:48 -05:00
|
|
|
resetUnreadMessages={resetUnreadMessageCount}
|
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)
|