您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Chat.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // Gifted chat requires a special object format and a reversed list
  44. // of messages.
  45. const messages
  46. = this.props._messages.map(this._transformMessage).reverse();
  47. const modalStyle = [
  48. styles.modalBackdrop
  49. ];
  50. if (this.props._solidBackground) {
  51. // We only use a transparent background, when we are in a video
  52. // meeting to give a user a glympse of what's happening. Otherwise
  53. // we use a non-transparent background.
  54. modalStyle.push(styles.solidModalBackdrop);
  55. }
  56. return (
  57. <Modal
  58. onRequestClose = { this.props._onToggleChat }
  59. visible = { this.props._isOpen }>
  60. <Header>
  61. <BackButton onPress = { this.props._onToggleChat } />
  62. <HeaderLabel labelKey = 'chat.title' />
  63. </Header>
  64. <SafeAreaView style = { modalStyle }>
  65. <GiftedChat
  66. messages = { messages }
  67. onSend = { this._onSend }
  68. renderMessage = { this._renderMessage } />
  69. </SafeAreaView>
  70. </Modal>
  71. );
  72. }
  73. _onSend: (Array<Object>) => void;
  74. /**
  75. * Callback to trigger a message send action.
  76. *
  77. * @param {string} message - The chat message to display.
  78. * @returns {void}
  79. */
  80. _onSend([ message ]) {
  81. this.props._onSendMessage(message.text);
  82. }
  83. _renderMessage: Object => React$Element<*>
  84. /**
  85. * Renders a single message.
  86. *
  87. * @param {Object} messageProps - The message props object to be rendered.
  88. * @returns {React$Element<*>}
  89. */
  90. _renderMessage(messageProps) {
  91. const { currentMessage } = messageProps;
  92. return (
  93. <ChatMessage message = { currentMessage } />
  94. );
  95. }
  96. _transformMessage: (Object, number) => Object;
  97. /**
  98. * Transforms a Jitsi message object to a format that gifted-chat can
  99. * handle.
  100. *
  101. * @param {Object} message - The chat message in our internal format.
  102. * @param {number} index - The index of the message in the array.
  103. * @returns {Object}
  104. */
  105. _transformMessage(message, index) {
  106. const system = message.messageType === 'error';
  107. return (
  108. {
  109. _id: index,
  110. createdAt: new Date(message.timestamp),
  111. messageType: message.messageType,
  112. system,
  113. text: system
  114. ? this.props.t('chat.error', {
  115. error: message.error,
  116. originalText: message.message
  117. })
  118. : message.message,
  119. user: {
  120. _id: message.id,
  121. name: message.displayName
  122. }
  123. }
  124. );
  125. }
  126. }
  127. /**
  128. * Maps part of the Redux state to the props of this component.
  129. *
  130. * @param {Object} state - The Redux state.
  131. * @returns {{
  132. * _solidBackground: boolean
  133. * }}
  134. */
  135. function _mapStateToProps(state) {
  136. const abstractReduxProps = _abstractMapStateToProps(state);
  137. return {
  138. ...abstractReduxProps,
  139. _solidBackground: state['features/base/conference'].audioOnly
  140. };
  141. }
  142. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));