about summary refs log tree commit diff
path: root/app/javascript/glitch/components/status/header.js
blob: bdb868e4d998fd8b0beacb2f46f0bb7daf620406 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*

`<StatusHeader>`
================

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 { defineMessages, injectIntl } from 'react-intl';

//  Mastodon imports  //
import Avatar from '../../../mastodon/components/avatar';
import AvatarOverlay from '../../../mastodon/components/avatar_overlay';
import DisplayName from '../../../mastodon/components/display_name';
import IconButton from '../../../mastodon/components/icon_button';
import VisibilityIcon from './visibility_icon';

                            /* * * * */

/*

Inital setup:
-------------

The `messages` constant is used to define any messages that we need
from inside props. In our case, these are the `collapse` and
`uncollapse` messages used with our collapse/uncollapse buttons.

*/

const messages = defineMessages({
  collapse: { id: 'status.collapse', defaultMessage: 'Collapse' },
  uncollapse: { id: 'status.uncollapse', defaultMessage: 'Uncollapse' },
  public: { id: 'privacy.public.short', defaultMessage: 'Public' },
  unlisted: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
  private: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
  direct: { id: 'privacy.direct.short', defaultMessage: 'Direct' },
});

                            /* * * * */

/*

The `<StatusHeader>` component:
-------------------------------

The `<StatusHeader>` component wraps together the header information
(avatar, display name) and upper buttons and icons (collapsing, media
icons) into a single `<header>` element.

###  Props

 -  __`account`, `friend` (`ImmutablePropTypes.map`) :__
    These give the accounts associated with the status. `account` is
    the author of the post; `friend` will have their avatar appear
    in the overlay if provided.

 -  __`mediaIcon` (`PropTypes.string`) :__
    If a mediaIcon should be placed in the header, this string
    specifies it.

 -  __`collapsible`, `collapsed` (`PropTypes.bool`) :__
    These props tell whether a post can be, and is, collapsed.

 -  __`parseClick` (`PropTypes.func`) :__
    This function will be called when the user clicks inside the header
    information.

 -  __`setExpansion` (`PropTypes.func`) :__
    This function is used to set the expansion state of the post.

 -  __`intl` (`PropTypes.object`) :__
    This is our internationalization object, provided by
    `injectIntl()`.

*/

@injectIntl
export default class StatusHeader extends React.PureComponent {

  static propTypes = {
    status: ImmutablePropTypes.map.isRequired,
    friend: ImmutablePropTypes.map,
    mediaIcon: PropTypes.string,
    collapsible: PropTypes.bool,
    collapsed: PropTypes.bool,
    parseClick: PropTypes.func.isRequired,
    setExpansion: PropTypes.func.isRequired,
    intl: PropTypes.object.isRequired,
  };

/*

###  Implementation

####  `handleCollapsedClick()`.

`handleCollapsedClick()` is just a simple callback for our collapsing
button. It calls `setExpansion` to set the collapsed state of the
status.

*/

  handleCollapsedClick = (e) => {
    const { collapsed, setExpansion } = this.props;
    if (e.button === 0) {
      setExpansion(collapsed ? null : false);
      e.preventDefault();
    }
  }

/*

####  `handleAccountClick()`.

`handleAccountClick()` handles any clicks on the header info. It calls
`parseClick()` with our `account` as the anticipatory `destination`.

*/

  handleAccountClick = (e) => {
    const { status, parseClick } = this.props;
    parseClick(e, `/accounts/${+status.getIn(['account', 'id'])}`);
  }

/*

####  `render()`.

`render()` actually puts our element on the screen. `<StatusHeader>`
has a very straightforward rendering process.

*/

  render () {
    const {
      status,
      friend,
      mediaIcon,
      collapsible,
      collapsed,
      intl,
    } = this.props;

    const account = status.get('account');

    return (
      <header className='status__info'>
        {

/*

We have to include the status icons before the header content because
it is rendered as a float.

*/

        }
        <div className='status__info__icons'>
          {mediaIcon ? (
            <i
              className={`fa fa-fw fa-${mediaIcon}`}
              aria-hidden='true'
            />
          ) : null}
          {(
            <VisibilityIcon visibility={status.get('visibility')} />
          )}
          {collapsible ? (
            <IconButton
              className='status__collapse-button'
              animate flip
              active={collapsed}
              title={
                collapsed ?
                intl.formatMessage(messages.uncollapse) :
                intl.formatMessage(messages.collapse)
              }
              icon='angle-double-up'
              onClick={this.handleCollapsedClick}
            />
          ) : null}
        </div>
        {

/*

This begins our header content. It is all wrapped inside of a link
which gets handled by `handleAccountClick`. We use an `<AvatarOverlay>`
if we have a `friend` and a normal `<Avatar>` if we don't.

*/

        }
        <a
          href={account.get('url')}
          target='_blank'
          className='status__display-name'
          onClick={this.handleAccountClick}
        >
          <div className='status__avatar'>{
            friend ? (
              <AvatarOverlay account={account} friend={friend} />
            ) : (
              <Avatar account={account} size={48} />
            )
          }</div>
          <DisplayName account={account} />
        </a>

      </header>
    );
  }

}