about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/drawer/results/index.js
blob: 4574c0e1e8966434f88d8c1b40635acfef0a4464 (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
//  Package imports.
import PropTypes from 'prop-types';
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import {
  FormattedMessage,
  defineMessages,
} from 'react-intl';
import spring from 'react-motion/lib/spring';
import { Link } from 'react-router-dom';

//  Components.
import Icon from 'flavours/glitch/components/icon';
import AccountContainer from 'flavours/glitch/containers/account_container';
import StatusContainer from 'flavours/glitch/containers/status_container';
import Hashtag from 'flavours/glitch/components/hashtag';

//  Utils.
import Motion from 'flavours/glitch/util/optional_motion';

//  Messages.
const messages = defineMessages({
  total: {
    defaultMessage: '{count, number} {count, plural, one {result} other {results}}',
    id: 'search_results.total',
  },
});

//  The component.
export default function DrawerResults ({
  results,
  visible,
}) {
  const accounts = results ? results.get('accounts') : null;
  const statuses = results ? results.get('statuses') : null;
  const hashtags = results ? results.get('hashtags') : null;

  //  This gets the total number of items.
  const count = [accounts, statuses, hashtags].reduce(function (size, item) {
    if (item && item.size) {
      return size + item.size;
    }
    return size;
  }, 0);

  //  The result.
  return (
    <Motion
      defaultStyle={{ x: -100 }}
      style={{
        x: spring(visible ? 0 : -100, {
          stiffness: 210,
          damping: 20,
        }),
      }}
    >
      {({ x }) => (
        <div
          className='drawer--results'
          style={{
            transform: `translateX(${x}%)`,
            visibility: x === -100 ? 'hidden' : 'visible',
          }}
        >
          <header>
            <Icon icon='search' fixedWidth />
            <FormattedMessage
              {...messages.total}
              values={{ count }}
            />
          </header>
          {accounts && accounts.size ? (
            <section>
              <h5><Icon icon='users' fixedWidth /><FormattedMessage id='search_results.accounts' defaultMessage='People' /></h5>

              {accounts.map(
                accountId => (
                  <AccountContainer
                    id={accountId}
                    key={accountId}
                  />
                )
              )}
            </section>
          ) : null}
          {statuses && statuses.size ? (
            <section>
              <h5><Icon icon='quote-right' fixedWidth /><FormattedMessage id='search_results.statuses' defaultMessage='Toots' /></h5>

              {statuses.map(
                statusId => (
                  <StatusContainer
                    id={statusId}
                    key={statusId}
                  />
                )
              )}
            </section>
          ) : null}
          {hashtags && hashtags.size ? (
            <section>
              <h5><Icon icon='hashtag' fixedWidth /><FormattedMessage id='search_results.hashtags' defaultMessage='Hashtags' /></h5>

              {hashtags.map(hashtag => <Hashtag key={hashtag.get('name')} hashtag={hashtag} />)}
            </section>
          ) : null}
        </div>
      )}
    </Motion>
  );
}

//  Props.
DrawerResults.propTypes = {
  results: ImmutablePropTypes.map,
  visible: PropTypes.bool,
};