You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import clsx from 'clsx';
  4. import React from 'react';
  5. import { connect } from 'react-redux';
  6. import { IReduxState } from '../../../app/types';
  7. import { translate } from '../../../base/i18n/functions';
  8. import Message from '../../../base/react/components/web/Message';
  9. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  10. import { MESSAGE_TYPE_LOCAL } from '../../constants';
  11. import AbstractChatMessage, { IProps as AbstractProps } from '../AbstractChatMessage';
  12. import PrivateMessageButton from './PrivateMessageButton';
  13. interface IProps extends AbstractProps {
  14. classes: any;
  15. type: string;
  16. }
  17. const styles = (theme: Theme) => {
  18. return {
  19. chatMessageWrapper: {
  20. maxWidth: '100%'
  21. },
  22. chatMessage: {
  23. display: 'inline-flex',
  24. padding: '12px',
  25. backgroundColor: theme.palette.ui02,
  26. borderRadius: '4px 12px 12px 12px',
  27. boxSizing: 'border-box' as const,
  28. maxWidth: '100%',
  29. marginTop: '4px',
  30. '&.privatemessage': {
  31. backgroundColor: theme.palette.support05
  32. },
  33. '&.local': {
  34. backgroundColor: theme.palette.ui04,
  35. borderRadius: '12px 4px 12px 12px',
  36. '&.privatemessage': {
  37. backgroundColor: theme.palette.support05
  38. }
  39. },
  40. '&.error': {
  41. backgroundColor: 'rgb(215, 121, 118)',
  42. borderRadius: 0,
  43. fontWeight: 100
  44. },
  45. '&.lobbymessage': {
  46. backgroundColor: theme.palette.support05
  47. }
  48. },
  49. replyWrapper: {
  50. display: 'flex',
  51. flexDirection: 'row' as const,
  52. alignItems: 'center'
  53. },
  54. messageContent: {
  55. maxWidth: '100%',
  56. overflow: 'hidden',
  57. flex: 1
  58. },
  59. replyButtonContainer: {
  60. display: 'flex',
  61. alignItems: 'flex-start',
  62. height: '100%'
  63. },
  64. replyButton: {
  65. padding: '2px'
  66. },
  67. displayName: {
  68. ...withPixelLineHeight(theme.typography.labelBold),
  69. color: theme.palette.text02,
  70. whiteSpace: 'nowrap',
  71. textOverflow: 'ellipsis',
  72. overflow: 'hidden',
  73. marginBottom: theme.spacing(1)
  74. },
  75. userMessage: {
  76. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  77. color: theme.palette.text01,
  78. whiteSpace: 'pre-wrap',
  79. wordBreak: 'break-word'
  80. },
  81. privateMessageNotice: {
  82. ...withPixelLineHeight(theme.typography.labelRegular),
  83. color: theme.palette.text02,
  84. marginTop: theme.spacing(1)
  85. },
  86. timestamp: {
  87. ...withPixelLineHeight(theme.typography.labelRegular),
  88. color: theme.palette.text03,
  89. marginTop: theme.spacing(1)
  90. }
  91. };
  92. };
  93. /**
  94. * Renders a single chat message.
  95. */
  96. class ChatMessage extends AbstractChatMessage<IProps> {
  97. /**
  98. * Implements React's {@link Component#render()}.
  99. *
  100. * @inheritdoc
  101. * @returns {ReactElement}
  102. */
  103. render() {
  104. const { message, t, knocking, classes, type } = this.props;
  105. return (
  106. <div
  107. className = { classes.chatMessageWrapper }
  108. id = { this.props.message.messageId }
  109. tabIndex = { -1 }>
  110. <div
  111. className = { clsx('chatmessage', classes.chatMessage, type,
  112. message.privateMessage && 'privatemessage',
  113. message.lobbyChat && !knocking && 'lobbymessage') }>
  114. <div className = { classes.replyWrapper }>
  115. <div className = { clsx('messagecontent', classes.messageContent) }>
  116. { this.props.showDisplayName && this._renderDisplayName() }
  117. <div className = { clsx('usermessage', classes.userMessage) }>
  118. <span className = 'sr-only'>
  119. { this.props.message.displayName === this.props.message.recipient
  120. ? t('chat.messageAccessibleTitleMe')
  121. : t('chat.messageAccessibleTitle',
  122. { user: this.props.message.displayName }) }
  123. </span>
  124. <Message text = { this._getMessageText() } />
  125. </div>
  126. { (message.privateMessage || (message.lobbyChat && !knocking))
  127. && this._renderPrivateNotice() }
  128. </div>
  129. { (message.privateMessage || (message.lobbyChat && !knocking))
  130. && message.messageType !== MESSAGE_TYPE_LOCAL
  131. && (
  132. <div
  133. className = { classes.replyButtonContainer }>
  134. <PrivateMessageButton
  135. isLobbyMessage = { message.lobbyChat }
  136. participantID = { message.id } />
  137. </div>
  138. ) }
  139. </div>
  140. </div>
  141. { this.props.showTimestamp && this._renderTimestamp() }
  142. </div>
  143. );
  144. }
  145. /**
  146. * Renders the display name of the sender.
  147. *
  148. * @returns {React$Element<*>}
  149. */
  150. _renderDisplayName() {
  151. return (
  152. <div
  153. aria-hidden = { true }
  154. className = { clsx('display-name', this.props.classes.displayName) }>
  155. { this.props.message.displayName }
  156. </div>
  157. );
  158. }
  159. /**
  160. * Renders the message privacy notice.
  161. *
  162. * @returns {React$Element<*>}
  163. */
  164. _renderPrivateNotice() {
  165. return (
  166. <div className = { this.props.classes.privateMessageNotice }>
  167. { this._getPrivateNoticeMessage() }
  168. </div>
  169. );
  170. }
  171. /**
  172. * Renders the time at which the message was sent.
  173. *
  174. * @returns {React$Element<*>}
  175. */
  176. _renderTimestamp() {
  177. return (
  178. <div className = { clsx('timestamp', this.props.classes.timestamp) }>
  179. { this._getFormattedTimestamp() }
  180. </div>
  181. );
  182. }
  183. }
  184. /**
  185. * Maps part of the Redux store to the props of this component.
  186. *
  187. * @param {Object} state - The Redux state.
  188. * @returns {IProps}
  189. */
  190. function _mapStateToProps(state: IReduxState) {
  191. const { knocking } = state['features/lobby'];
  192. return {
  193. knocking
  194. };
  195. }
  196. export default translate(connect(_mapStateToProps)(withStyles(styles)(ChatMessage)));