about summary refs log tree commit diff
path: root/app/javascript/glitch/components/status/prepend.js
blob: d9b04b5ec6ddfa2625606f45db55aecd094545f5 (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
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
/*

`<StatusPrepend>`
=================

Originally a part of `<Status>`, but extracted into a separate
component for better documentation and maintainance by
@kibi@glitch.social as a part of glitch-soc/mastodon.

*/

                            /* * * * */

/*

Imports:
--------

*/

//  Package imports  //
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import escapeTextContentForBrowser from 'escape-html';
import { defineMessages, injectIntl } from 'react-intl';
import { FormattedMessage } from 'react-intl';

//  Mastodon imports  //
import emojify from '../../../mastodon/emoji';


const messages = defineMessages({
  deleteNotification: { id: 'status.dismiss_notification', defaultMessage: 'Dismiss notification' },
});

                            /* * * * */

/*

The `<StatusPrepend>` component:
--------------------------------

The `<StatusPrepend>` component holds a status's prepend, ie the text
that says “X reblogged this,” etc. It is represented by an `<aside>`
element.

###  Props

 -  __`type` (`PropTypes.string`) :__
    The type of prepend. One of `'reblogged_by'`, `'reblog'`,
    `'favourite'`.

 -  __`account` (`ImmutablePropTypes.map`) :__
    The account associated with the prepend.

 -  __`parseClick` (`PropTypes.func.isRequired`) :__
    Our click parsing function.

*/

@injectIntl
export default class StatusPrepend extends React.PureComponent {

  static propTypes = {
    type: PropTypes.string.isRequired,
    account: ImmutablePropTypes.map.isRequired,
    parseClick: PropTypes.func.isRequired,
    notificationId: PropTypes.number,
    onDeleteNotification: PropTypes.func,
    intl: PropTypes.object.isRequired,
  };

/*

###  Implementation

####  `handleClick()`.

This is just a small wrapper for `parseClick()` that gets fired when
an account link is clicked.

*/

  handleClick = (e) => {
    const { account, parseClick } = this.props;
    parseClick(e, `/accounts/${+account.get('id')}`);
  }

  handleNotificationDeleteClick = () => {
    this.props.onDeleteNotification(this.props.notificationId);
  }

/*

####  `<Message>`.

`<Message>` is a quick functional React component which renders the
actual prepend message based on our provided `type`. First we create a
`link` for the account's name, and then use `<FormattedMessage>` to
generate the message.

*/

  Message = () => {
    const { type, account } = this.props;
    let link = (
      <a
        onClick={this.handleClick}
        href={account.get('url')}
        className='status__display-name'
      >
        <b
          dangerouslySetInnerHTML={{
            __html : emojify(escapeTextContentForBrowser(
              account.get('display_name') || account.get('username')
            )),
          }}
        />
      </a>
    );
    switch (type) {
    case 'reblogged_by':
      return (
        <FormattedMessage
          id='status.reblogged_by'
          defaultMessage='{name} boosted'
          values={{ name : link }}
        />
      );
    case 'favourite':
      return (
        <FormattedMessage
          id='notification.favourite'
          defaultMessage='{name} favourited your status'
          values={{ name : link }}
        />
      );
    case 'reblog':
      return (
        <FormattedMessage
          id='notification.reblog'
          defaultMessage='{name} boosted your status'
          values={{ name : link }}
        />
      );
    }
    return null;
  }

/*

####  `render()`.

Our `render()` is incredibly simple; we just render the icon and then
the `<Message>` inside of an <aside>.

*/

  render () {
    const { Message } = this;
    const { type, intl } = this.props;

    const dismissTitle = intl.formatMessage(messages.deleteNotification);
    const dismiss = this.props.notificationId ? (
      <button
        aria-label={dismissTitle}
        title={dismissTitle}
        onClick={this.handleNotificationDeleteClick}
        className='status__prepend-dismiss-button'
      >
        <i className='fa fa-eraser' />
      </button>
    ) : null;

    return !type ? null : (
      <aside className={type === 'reblogged_by' ? 'status__prepend' : 'notification__message'}>
        <div className={type === 'reblogged_by' ? 'status__prepend-icon-wrapper' : 'notification__favourite-icon-wrapper'}>
          <i
            className={`fa fa-fw fa-${
              type === 'favourite' ? 'star star-icon' : 'retweet'
            } status__prepend-icon`}
          />
        </div>
        <Message />
        {dismiss}
      </aside>
    );
  }

}