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.

MessageRecipient.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import React from 'react';
  4. import { translate } from '../../../base/i18n/functions';
  5. import { IconCloseLarge } from '../../../base/icons/svg';
  6. import { connect } from '../../../base/redux/functions';
  7. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  8. import Button from '../../../base/ui/components/web/Button';
  9. import { BUTTON_TYPES } from '../../../base/ui/constants.any';
  10. import AbstractMessageRecipient, {
  11. IProps,
  12. _mapDispatchToProps,
  13. _mapStateToProps
  14. } from '../AbstractMessageRecipient';
  15. const styles = (theme: Theme) => {
  16. return {
  17. container: {
  18. margin: '0 16px 8px',
  19. padding: '6px',
  20. paddingLeft: '16px',
  21. display: 'flex',
  22. justifyContent: 'space-between',
  23. alignItems: 'center',
  24. backgroundColor: theme.palette.support05,
  25. borderRadius: theme.shape.borderRadius,
  26. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  27. color: theme.palette.text01
  28. },
  29. iconButton: {
  30. padding: '2px',
  31. '&:hover': {
  32. backgroundColor: theme.palette.action03
  33. }
  34. }
  35. };
  36. };
  37. /**
  38. * Class to implement the displaying of the recipient of the next message.
  39. */
  40. class MessageRecipient extends AbstractMessageRecipient<IProps> {
  41. /**
  42. * Initializes a new {@code MessageRecipient} instance.
  43. *
  44. * @param {IProps} props - The read-only properties with which the new instance
  45. * is to be initialized.
  46. */
  47. constructor(props: IProps) {
  48. super(props);
  49. // Bind event handler so it is only bound once for every instance.
  50. this._onKeyPress = this._onKeyPress.bind(this);
  51. }
  52. /**
  53. * KeyPress handler for accessibility.
  54. *
  55. * @param {Object} e - The key event to handle.
  56. *
  57. * @returns {void}
  58. */
  59. _onKeyPress(e: React.KeyboardEvent) {
  60. if (
  61. (this.props._onRemovePrivateMessageRecipient || this.props._onHideLobbyChatRecipient)
  62. && (e.key === ' ' || e.key === 'Enter')
  63. ) {
  64. e.preventDefault();
  65. if (this.props._isLobbyChatActive && this.props._onHideLobbyChatRecipient) {
  66. this.props._onHideLobbyChatRecipient();
  67. } else if (this.props._onRemovePrivateMessageRecipient) {
  68. this.props._onRemovePrivateMessageRecipient();
  69. }
  70. }
  71. }
  72. /**
  73. * Implements {@code PureComponent#render}.
  74. *
  75. * @inheritdoc
  76. */
  77. render() {
  78. const { _privateMessageRecipient, _isLobbyChatActive,
  79. _lobbyMessageRecipient, _visible } = this.props;
  80. if ((!_privateMessageRecipient && !_isLobbyChatActive) || !_visible) {
  81. return null;
  82. }
  83. const { classes, t } = this.props;
  84. return (
  85. <div
  86. className = { classes.container }
  87. id = 'chat-recipient'
  88. role = 'alert'>
  89. <span>
  90. { t(_isLobbyChatActive ? 'chat.lobbyChatMessageTo' : 'chat.messageTo', {
  91. recipient: _isLobbyChatActive ? _lobbyMessageRecipient : _privateMessageRecipient
  92. }) }
  93. </span>
  94. <Button
  95. accessibilityLabel = { t('dialog.close') }
  96. className = { classes.iconButton }
  97. icon = { IconCloseLarge }
  98. onClick = { _isLobbyChatActive
  99. ? this.props._onHideLobbyChatRecipient : this.props._onRemovePrivateMessageRecipient }
  100. onKeyPress = { this._onKeyPress }
  101. type = { BUTTON_TYPES.TERTIARY } />
  102. </div>
  103. );
  104. }
  105. }
  106. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(
  107. withStyles(styles)(MessageRecipient)));