# All files / components/MessageBox index.js

95.24% Statements 20/21
93.33% Branches 14/15
100% Functions 5/5
95% Lines 19/20

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192

    1x

    1x

    1x

    3x

    3x

    1x

    1x
    2x
    1x
    1x
    1x

    4x

    1x

    6x

    6x

    6x

    1x

    1x

    1x

     import React from 'react';
    import PropTypes from 'prop-types';
    import styled, { css } from 'styled-components';

    // Constants Types
    import { messagesTypes, messagesStatus, personTypes } from '../../utils/constants';

    // Other components used by this component
    import SystemMessage from '../SystemMessage';
    import TimeMarkMessage from '../TimeMarkMessage';
    import Avatar from '../Avatar';

    // Helpers components
    import NormalMessageBox from './NormalMessageBox';

    const StyledMessageBoxContainer = styled.div`
      position: relative;
      flex-direction: column;
      display: block;
      overflow: hidden;
      min-width: 200px;
      &:hover div {
        opacity: 1;
        visibility: visible;
      }
    `;
    StyledMessageBoxContainer.displayName = 'StyledMessageBoxContainer';

    const StyledMessageBoxAvatarContainer = styled.div`
      display: inline-block;
      position: absolute;
      bottom: 0;
      ${(props) => props.position === 'left'
          && css`
            float: left;
            left: 0;
          `};

      ${(props) => props.position === 'right'
          && css`
            float: right;
            right: 0;
          `};
    `;
    StyledMessageBoxAvatarContainer.displayName = 'StyledMessageBoxAvatarContainer';

    const renderAutomaticMessage = (props, message) => {
      if (message.type === messagesTypes.SYSTEM) {
        return (<SystemMessage {...props} />);
      } Eif (message.type === messagesTypes.TIME) {
        return (<TimeMarkMessage {...props} />);
      }
      return <div />;
    };

    const renderNormalMessage = (props) => <NormalMessageBox {...props} />;

    const MessageBox = (props) => {
      const {
        className, onMessageClick, message,
      } = props;
      const {
        type, person, position,
      } = message;

      return (
        <StyledMessageBoxContainer
          className={className}
          onClick={onMessageClick}
          onKeyPress={onMessageClick}
        >
          {
            person.avatar && type !== messagesTypes.SYSTEM
            && (
              <StyledMessageBoxAvatarContainer position={position}>
                <Avatar
                  src={person.avatar}
                  withStatus={false}
                  size="xsmall"
                />
              </StyledMessageBoxAvatarContainer>
            )
          }

          {
            type === messagesTypes.SYSTEM || type === messagesTypes.TIME
              ? renderAutomaticMessage(props, message)
              : renderNormalMessage(props)
          }
        </StyledMessageBoxContainer>
      );
    };

    MessageBox.propTypes = {
      /**
       * message: Actual message object.
       */
      message: PropTypes.shape({
        /**
         * Id: Message unique identifier
         */
        id: PropTypes.number,
        /**
         * Position: What side the message is displayed inside the chatbox
         */
        position: PropTypes.oneOf(['left', 'right']),
        /**
         * Type of Message: Type of the message. Values are defined as constants.
         * The current supported types are: SYSTEM, TEXT, TYPING, TIME, ACTIONABLE, FILE, PHOTO.
         */
        type: PropTypes.oneOf(Object.values(messagesTypes)),
        /**
        * forwarded: Boolean that indicates if message was forwareded. If this is false (default) onForwardClick
        * handler has no effect at all
        */
        forwarded: PropTypes.bool,
        /**
         * Title: Message title. Is used to display as a first line. It is also possible to make it clickable
         */
        title: PropTypes.string,
        /**
         * Text: Actual message content text.
         */
        text: PropTypes.string,
        /**
         * data: Object with extra data used to display information about the message
         */
        data: PropTypes.object,
        /**
         * status: Message status. Values are defined as constants.
         * Current supported values are: WAITING, SENT, RECEIVED, READ
         */
        status: PropTypes.oneOf(Object.values(messagesStatus)),
        /**
         * date: Message creation date.
         */
        date: PropTypes.instanceOf(Date),
        /**
         * dateFormat: Formated date string to show. This is the default to use.
         * If this is not provided by default to show the 'HH:mm' is used.
         */
        dateFormat: PropTypes.string,
        /**
         * person: Object representing the person that created or submited this message.
         */
        person: PropTypes.shape({
          id: PropTypes.number,
          name: PropTypes.string,
          internal_name: PropTypes.string,
          active: PropTypes.bool,
          email: PropTypes.string,
          avatar: PropTypes.string,
          role: PropTypes.string,
          status_name: PropTypes.string,
          availability_status_name: PropTypes.string,
          type: PropTypes.oneOf(Object.values(personTypes)),
        }),
        /**
         * labels: Object with keys are the ids of labels
         * Used to pass string (probably internazionalization) to components
         */
        labels: PropTypes.object,
      }),
      /**
       * onMessageClick: Handler function that is called on message click action
       */
      onMessageClick: PropTypes.func,
      /**
       * onFailedClick: Handler function to be called when user click on forwared message
       */
      // eslint-disable-next-line react/no-unused-prop-types
      onFailedClick: PropTypes.func,
      /**
       * className: Extra className to provide to the component in order to style it when used in different contexts.
       */
      className: PropTypes.string,
    };

    MessageBox.defaultProps = {
      position: 'left',
      type: 'text',
      text: '',
      onMessageClick: null,
      person: null,
      className: '',
    };

    MessageBox.displayName = 'MessageBox';

    export default MessageBox;