Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ChatMessage.tsx 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: 'calc(100% - 40px)' // 100% - avatar and margin
  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. maxWidth: '100%'
  54. },
  55. messageContent: {
  56. maxWidth: '100%',
  57. overflow: 'hidden',
  58. flex: 1
  59. },
  60. replyButtonContainer: {
  61. display: 'flex',
  62. alignItems: 'flex-start',
  63. height: '100%'
  64. },
  65. replyButton: {
  66. padding: '2px'
  67. },
  68. displayName: {
  69. ...withPixelLineHeight(theme.typography.labelBold),
  70. color: theme.palette.text02,
  71. whiteSpace: 'nowrap',
  72. textOverflow: 'ellipsis',
  73. overflow: 'hidden',
  74. marginBottom: theme.spacing(1)
  75. },
  76. userMessage: {
  77. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  78. color: theme.palette.text01,
  79. whiteSpace: 'pre-wrap',
  80. wordBreak: 'break-word'
  81. },
  82. privateMessageNotice: {
  83. ...withPixelLineHeight(theme.typography.labelRegular),
  84. color: theme.palette.text02,
  85. marginTop: theme.spacing(1)
  86. },
  87. timestamp: {
  88. ...withPixelLineHeight(theme.typography.labelRegular),
  89. color: theme.palette.text03,
  90. marginTop: theme.spacing(1)
  91. }
  92. };
  93. };
  94. /**
  95. * Renders a single chat message.
  96. */
  97. class ChatMessage extends AbstractChatMessage<IProps> {
  98. /**
  99. * Implements React's {@link Component#render()}.
  100. *
  101. * @inheritdoc
  102. * @returns {ReactElement}
  103. */
  104. render() {
  105. const { message, t, knocking, classes, type } = this.props;
  106. return (
  107. <div
  108. className = { classes.chatMessageWrapper }
  109. id = { this.props.message.messageId }
  110. tabIndex = { -1 }>
  111. <div
  112. className = { clsx('chatmessage', classes.chatMessage, type,
  113. message.privateMessage && 'privatemessage',
  114. message.lobbyChat && !knocking && 'lobbymessage') }>
  115. <div className = { classes.replyWrapper }>
  116. <div className = { clsx('messagecontent', classes.messageContent) }>
  117. { this.props.showDisplayName && this._renderDisplayName() }
  118. <div className = { clsx('usermessage', classes.userMessage) }>
  119. <span className = 'sr-only'>
  120. { this.props.message.displayName === this.props.message.recipient
  121. ? t('chat.messageAccessibleTitleMe')
  122. : t('chat.messageAccessibleTitle',
  123. { user: this.props.message.displayName }) }
  124. </span>
  125. <Message text = { this._getMessageText() } />
  126. </div>
  127. { (message.privateMessage || (message.lobbyChat && !knocking))
  128. && this._renderPrivateNotice() }
  129. </div>
  130. { (message.privateMessage || (message.lobbyChat && !knocking))
  131. && message.messageType !== MESSAGE_TYPE_LOCAL
  132. && (
  133. <div
  134. className = { classes.replyButtonContainer }>
  135. <PrivateMessageButton
  136. isLobbyMessage = { message.lobbyChat }
  137. participantID = { message.id } />
  138. </div>
  139. ) }
  140. </div>
  141. </div>
  142. { this.props.showTimestamp && this._renderTimestamp() }
  143. </div>
  144. );
  145. }
  146. /**
  147. * Renders the display name of the sender.
  148. *
  149. * @returns {React$Element<*>}
  150. */
  151. _renderDisplayName() {
  152. return (
  153. <div
  154. aria-hidden = { true }
  155. className = { clsx('display-name', this.props.classes.displayName) }>
  156. { this.props.message.displayName }
  157. </div>
  158. );
  159. }
  160. /**
  161. * Renders the message privacy notice.
  162. *
  163. * @returns {React$Element<*>}
  164. */
  165. _renderPrivateNotice() {
  166. return (
  167. <div className = { this.props.classes.privateMessageNotice }>
  168. { this._getPrivateNoticeMessage() }
  169. </div>
  170. );
  171. }
  172. /**
  173. * Renders the time at which the message was sent.
  174. *
  175. * @returns {React$Element<*>}
  176. */
  177. _renderTimestamp() {
  178. return (
  179. <div className = { clsx('timestamp', this.props.classes.timestamp) }>
  180. { this._getFormattedTimestamp() }
  181. </div>
  182. );
  183. }
  184. }
  185. /**
  186. * Maps part of the Redux store to the props of this component.
  187. *
  188. * @param {Object} state - The Redux state.
  189. * @returns {IProps}
  190. */
  191. function _mapStateToProps(state: IReduxState) {
  192. const { knocking } = state['features/lobby'];
  193. return {
  194. knocking
  195. };
  196. }
  197. export default translate(connect(_mapStateToProps)(withStyles(styles)(ChatMessage)));