chat-pane.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, { useEffect } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { useTranslation } from 'react-i18next'
  4. import MessageList from './message-list'
  5. import MessageInput from './message-input'
  6. import InfiniteScroll from './infinite-scroll'
  7. import Icon from '../../../shared/components/icon'
  8. import { useLayoutContext } from '../../../shared/context/layout-context'
  9. import { useApplicationContext } from '../../../shared/context/application-context'
  10. import withErrorBoundary from '../../../infrastructure/error-boundary'
  11. import { useChatContext } from '../context/chat-context'
  12. function ChatPane() {
  13. const { t } = useTranslation()
  14. const { chatIsOpen } = useLayoutContext({ chatIsOpen: PropTypes.bool })
  15. const { user } = useApplicationContext({
  16. user: PropTypes.shape({ id: PropTypes.string.isRequired }.isRequired),
  17. })
  18. const {
  19. status,
  20. messages,
  21. initialMessagesLoaded,
  22. atEnd,
  23. loadInitialMessages,
  24. loadMoreMessages,
  25. sendMessage,
  26. markMessagesAsRead,
  27. } = useChatContext()
  28. useEffect(() => {
  29. if (chatIsOpen && !initialMessagesLoaded) {
  30. loadInitialMessages()
  31. }
  32. }, [chatIsOpen, loadInitialMessages, initialMessagesLoaded])
  33. const shouldDisplayPlaceholder = status !== 'pending' && messages.length === 0
  34. const messageContentCount = messages.reduce(
  35. (acc, { contents }) => acc + contents.length,
  36. 0
  37. )
  38. return (
  39. <aside className="chat">
  40. <InfiniteScroll
  41. atEnd={atEnd}
  42. className="messages"
  43. fetchData={loadMoreMessages}
  44. isLoading={status === 'pending'}
  45. itemCount={messageContentCount}
  46. >
  47. <div>
  48. <h2 className="sr-only">{t('chat')}</h2>
  49. {status === 'pending' && <LoadingSpinner />}
  50. {shouldDisplayPlaceholder && <Placeholder />}
  51. <MessageList
  52. messages={messages}
  53. userId={user.id}
  54. resetUnreadMessages={markMessagesAsRead}
  55. />
  56. </div>
  57. </InfiniteScroll>
  58. <MessageInput
  59. resetUnreadMessages={markMessagesAsRead}
  60. sendMessage={sendMessage}
  61. />
  62. </aside>
  63. )
  64. }
  65. function LoadingSpinner() {
  66. const { t } = useTranslation()
  67. return (
  68. <div className="loading">
  69. <Icon type="fw" modifier="refresh" spin />
  70. {` ${t('loading')}…`}
  71. </div>
  72. )
  73. }
  74. function Placeholder() {
  75. const { t } = useTranslation()
  76. return (
  77. <>
  78. <div className="no-messages text-center small">{t('no_messages')}</div>
  79. <div className="first-message text-center">
  80. {t('send_first_message')}
  81. <br />
  82. <Icon type="arrow-down" />
  83. </div>
  84. </>
  85. )
  86. }
  87. export default withErrorBoundary(ChatPane)