about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/composer/textarea/suggestions/item/index.js
blob: f55640bcf9393fc0520fdad5ac488210f979270b (plain) (blame)
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
//  Package imports.
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

//  Components.
import AccountContainer from 'flavours/glitch/containers/account_container';

//  Utils.
import { unicodeMapping } from 'flavours/glitch/util/emoji';
import { assignHandlers } from 'flavours/glitch/util/react_helpers';

//  Gets our asset host from the environment, if available.
const assetHost = ((process || {}).env || {}).CDN_HOST || '';

//  Handlers.
const handlers = {

  //  Handles a click on a suggestion.
  handleClick (e) {
    const {
      index,
      onClick,
    } = this.props;
    if (onClick) {
      e.preventDefault();
      e.stopPropagation();  //  Prevents following account links
      onClick(index);
    }
  },

  //  This prevents the focus from changing, which would mess with
  //  our suggestion code.
  handleMouseDown (e) {
    e.preventDefault();
  },
};

//  The component.
export default class ComposerTextareaSuggestionsItem extends React.Component {

  //  Constructor.
  constructor (props) {
    super(props);
    assignHandlers(this, handlers);
  }

  //  Rendering.
  render () {
    const {
      handleMouseDown,
      handleClick,
    } = this.handlers;
    const {
      selected,
      suggestion,
    } = this.props;
    const computedClass = classNames('composer--textarea--suggestions--item', { selected });

    //  The result.
    return (
      <div
        className={computedClass}
        onMouseDown={handleMouseDown}
        onClickCapture={handleClick}  //  Jumps in front of contents
        role='button'
        tabIndex='0'
      >
        { //  If the suggestion is an object, then we render an emoji.
          //  Otherwise, we render an account.
          typeof suggestion === 'object' ? function () {
            const url = function () {
              if (suggestion.custom) {
                return suggestion.imageUrl;
              } else {
                const mapping = unicodeMapping[suggestion.native] || unicodeMapping[suggestion.native.replace(/\uFE0F$/, '')];
                if (!mapping) {
                  return null;
                }
                return `${assetHost}/emoji/${mapping.filename}.svg`;
              }
            }();
            return url ? (
              <div className='emoji'>
                <img
                  alt={suggestion.native || suggestion.colons}
                  className='emojione'
                  src={url}
                />
                {suggestion.colons}
              </div>
            ) : null;
          }() : (
            <AccountContainer
              id={suggestion}
              small
            />
          )
        }
      </div>
    );
  }

}

//  Props.
ComposerTextareaSuggestionsItem.propTypes = {
  index: PropTypes.number,
  onClick: PropTypes.func,
  selected: PropTypes.bool,
  suggestion: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
};