Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Chat.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @flow
  2. import React from 'react';
  3. import { SafeAreaView } from 'react-native';
  4. import { GiftedChat } from 'react-native-gifted-chat';
  5. import { connect } from 'react-redux';
  6. import { translate } from '../../../base/i18n';
  7. import { BackButton, Header, HeaderLabel, Modal } from '../../../base/react';
  8. import AbstractChat, {
  9. _mapDispatchToProps,
  10. _mapStateToProps as _abstractMapStateToProps,
  11. type Props as AbstractProps
  12. } from '../AbstractChat';
  13. import ChatMessage from './ChatMessage';
  14. import styles from './styles';
  15. type Props = AbstractProps & {
  16. /**
  17. * True if the chat window should have a solid BG render.
  18. */
  19. _solidBackground: boolean
  20. }
  21. /**
  22. * Implements a React native component that renders the chat window (modal) of
  23. * the mobile client.
  24. */
  25. class Chat extends AbstractChat<Props> {
  26. /**
  27. * Initializes a new instance.
  28. *
  29. * @inheritdoc
  30. */
  31. constructor(props: Props) {
  32. super(props);
  33. this._onSend = this._onSend.bind(this);
  34. this._renderMessage = this._renderMessage.bind(this);
  35. this._transformMessage = this._transformMessage.bind(this);
  36. }
  37. /**
  38. * Implements React's {@link Component#render()}.
  39. *
  40. * @inheritdoc
  41. */
  42. render() {
  43. const messages = this.props._messages.map(this._transformMessage);
  44. const modalStyle = [
  45. styles.modalBackdrop
  46. ];
  47. if (this.props._solidBackground) {
  48. // We only use a transparent background, when we are in a video
  49. // meeting to give a user a glympse of what's happening. Otherwise
  50. // we use a non-transparent background.
  51. modalStyle.push(styles.solidModalBackdrop);
  52. }
  53. return (
  54. <Modal
  55. onRequestClose = { this.props._onToggleChat }
  56. visible = { this.props._isOpen }>
  57. <Header>
  58. <BackButton onPress = { this.props._onToggleChat } />
  59. <HeaderLabel labelKey = 'chat.title' />
  60. </Header>
  61. <SafeAreaView style = { modalStyle }>
  62. <GiftedChat
  63. messages = { messages }
  64. onSend = { this._onSend }
  65. renderMessage = { this._renderMessage } />
  66. </SafeAreaView>
  67. </Modal>
  68. );
  69. }
  70. _onSend: (Array<Object>) => void;
  71. /**
  72. * Callback to trigger a message send action.
  73. *
  74. * @param {string} message - The chat message to display.
  75. * @returns {void}
  76. */
  77. _onSend([ message ]) {
  78. this.props._onSendMessage(message.text);
  79. }
  80. _renderMessage: Object => React$Element<*>
  81. /**
  82. * Renders a single message.
  83. *
  84. * @param {Object} messageProps - The message props object to be rendered.
  85. * @returns {React$Element<*>}
  86. */
  87. _renderMessage(messageProps) {
  88. const { currentMessage } = messageProps;
  89. return (
  90. <ChatMessage message = { currentMessage } />
  91. );
  92. }
  93. _transformMessage: (Object, number) => Object;
  94. /**
  95. * Transforms a Jitsi message object to a format that gifted-chat can
  96. * handle.
  97. *
  98. * @param {Object} message - The chat message in our internal format.
  99. * @param {number} index - The index of the message in the array.
  100. * @returns {Object}
  101. */
  102. _transformMessage(message, index) {
  103. const system = message.messageType === 'error';
  104. return (
  105. {
  106. _id: index,
  107. createdAt: new Date(message.timestamp),
  108. messageType: message.messageType,
  109. system,
  110. text: system
  111. ? this.props.t('chat.error', {
  112. error: message.error,
  113. originalText: message.message
  114. })
  115. : message.message,
  116. user: {
  117. _id: message.id,
  118. name: message.displayName
  119. }
  120. }
  121. );
  122. }
  123. }
  124. /**
  125. * Maps part of the Redux state to the props of this component.
  126. *
  127. * @param {Object} state - The Redux state.
  128. * @returns {{
  129. * _solidBackground: boolean
  130. * }}
  131. */
  132. function _mapStateToProps(state) {
  133. const abstractReduxProps = _abstractMapStateToProps(state);
  134. return {
  135. ...abstractReduxProps,
  136. // Gifted chat requires the messages to be reverse ordered.
  137. _messages: [
  138. ...abstractReduxProps._messages
  139. ].reverse(),
  140. _solidBackground: state['features/base/conference'].audioOnly
  141. };
  142. }
  143. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));