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.

AbstractChatMessage.js 2.2KB

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