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.

AbstractChatMessage.tsx 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { PureComponent } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { getLocalizedDateFormatter } from '../../base/i18n/dateUtil';
  4. import { MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL } from '../constants';
  5. import { IMessage } from '../reducer';
  6. /**
  7. * Formatter string to display the message timestamp.
  8. */
  9. const TIMESTAMP_FORMAT = 'H:mm';
  10. /**
  11. * The type of the React {@code Component} props of {@code AbstractChatMessage}.
  12. */
  13. export interface IProps extends WithTranslation {
  14. /**
  15. * Whether current participant is currently knocking in the lobby room.
  16. */
  17. knocking: boolean;
  18. /**
  19. * The representation of a chat message.
  20. */
  21. message: IMessage;
  22. /**
  23. * Whether or not the avatar image of the participant which sent the message
  24. * should be displayed.
  25. */
  26. showAvatar?: boolean;
  27. /**
  28. * Whether or not the name of the participant which sent the message should
  29. * be displayed.
  30. */
  31. showDisplayName: boolean;
  32. /**
  33. * Whether or not the time at which the message was sent should be
  34. * displayed.
  35. */
  36. showTimestamp: boolean;
  37. }
  38. /**
  39. * Abstract component to display a chat message.
  40. */
  41. export default class AbstractChatMessage<P extends IProps> extends PureComponent<P> {
  42. /**
  43. * Returns the timestamp to display for the message.
  44. *
  45. * @returns {string}
  46. */
  47. _getFormattedTimestamp() {
  48. return getLocalizedDateFormatter(new Date(this.props.message.timestamp))
  49. .format(TIMESTAMP_FORMAT);
  50. }
  51. /**
  52. * Generates the message text to be rendered in the component.
  53. *
  54. * @returns {string}
  55. */
  56. _getMessageText() {
  57. const { message } = this.props;
  58. return message.messageType === MESSAGE_TYPE_ERROR
  59. ? this.props.t('chat.error', {
  60. error: message.message
  61. })
  62. : message.message;
  63. }
  64. /**
  65. * Returns the message that is displayed as a notice for private messages.
  66. *
  67. * @returns {string}
  68. */
  69. _getPrivateNoticeMessage() {
  70. const { message, t } = this.props;
  71. return t('chat.privateNotice', {
  72. recipient: message.messageType === MESSAGE_TYPE_LOCAL ? message.recipient : t('chat.you')
  73. });
  74. }
  75. }