about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/drawer/results/index.js
blob: f2a79eb592b735e00fbf49d8b6251d1c5dba8e43 (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
//  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 AccountContainer from 'flavours/glitch/containers/account_container';
import StatusContainer from 'flavours/glitch/containers/status_container';

//  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>
            <FormattedMessage
              {...messages.total}
              values={{ count }}
            />
          </header>
          {accounts && accounts.size ? (
            <section>
              {accounts.map(
                accountId => (
                  <AccountContainer
                    id={accountId}
                    key={accountId}
                  />
                )
              )}
            </section>
          ) : null}
          {statuses && statuses.size ? (
            <section>
              {statuses.map(
                statusId => (
                  <StatusContainer
                    id={statusId}
                    key={statusId}
                  />
                )
              )}
            </section>
          ) : null}
          {hashtags && hashtags.size ? (
            <section>
              {hashtags.map(
                hashtag => (
                  <Link
                    className='hashtag'
                    key={hashtag}
                    to={`/timelines/tag/${hashtag}`}
                  >#{hashtag}</Link>
                )
              )}
            </section>
          ) : null}
        </div>
      )}
    </Motion>
  );
}

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