forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupport.jsx
More file actions
282 lines (256 loc) · 7.52 KB
/
Support.jsx
File metadata and controls
282 lines (256 loc) · 7.52 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Import External Dependencies
import { Component } from 'react';
import PropTypes from 'prop-types';
import VisibilitySensor from 'react-visibility-sensor';
// Import Data
import Backers from './_supporters.json';
import Additional from './AdditionalSupporters';
import SmallIcon from '../../assets/icon-square-small-slack.png';
// Load Styling
import './Support.scss';
const SUPPORTERS = [...Backers];
// Merge or add additional backers/sponsors
for (const additional of Additional) {
const existing = SUPPORTERS.find(
(supporter) => supporter.slug && supporter.slug === additional.slug
);
if (existing) {
existing.totalDonations += additional.totalDonations;
} else {
SUPPORTERS.push(additional);
}
}
// Resort the list
SUPPORTERS.sort((a, b) => b.totalDonations - a.totalDonations);
// Define ranks
const totalRanks = {
backer: {
maximum: 200,
random: 100,
},
latest: {
maxAge: 14 * 24 * 60 * 60 * 1000,
limit: 10,
},
bronze: {
minimum: 200,
maximum: 2000,
},
silver: {
minimum: 2000,
maximum: 10000,
},
gold: {
minimum: 10000,
maximum: 50000,
},
platinum: {
minimum: 50000,
},
};
const monthlyRanks = {
backer: {
maximum: 10,
random: 100,
},
latest: {
maxAge: 14 * 24 * 60 * 60 * 1000,
limit: 10,
},
bronze: {
minimum: 10,
maximum: 100,
},
silver: {
minimum: 100,
maximum: 500,
},
gold: {
minimum: 500,
maximum: 2500,
},
platinum: {
minimum: 2500,
},
};
function formatMoney(number) {
let str = Math.round(number) + '';
if (str.length > 3) {
str = str.slice(0, -3) + ',' + str.slice(-3);
}
return str;
}
export default class Support extends Component {
static propTypes = {
rank: PropTypes.string,
type: PropTypes.string,
};
state = {
inView: false,
};
handleInView = (inView) => {
if (!inView || this.state.inView) {
return;
}
this.setState({ inView });
};
render() {
let { rank, type } = this.props;
const { inView } = this.state;
let supporters = SUPPORTERS;
let minimum, maximum, maxAge, limit, random;
const ranks = type === 'monthly' ? monthlyRanks : totalRanks;
const getAmount =
type === 'monthly'
? (item) => item.monthlyDonations
: (item) => item.totalDonations;
if (rank && ranks[rank]) {
minimum = ranks[rank].minimum;
maximum = ranks[rank].maximum;
maxAge = ranks[rank].maxAge;
limit = ranks[rank].limit;
random = ranks[rank].random;
}
if (typeof minimum === 'number') {
supporters = supporters.filter(
(item) => getAmount(item) >= minimum * 100
);
}
if (typeof maximum === 'number') {
supporters = supporters.filter((item) => getAmount(item) < maximum * 100);
}
if (typeof maxAge === 'number') {
const now = Date.now();
supporters = supporters.filter(
(item) =>
item.firstDonation &&
now - new Date(item.firstDonation).getTime() < maxAge
);
}
if (typeof limit === 'number') {
supporters = supporters.slice(0, limit);
}
if (typeof random === 'number') {
if (supporters.length >= random) {
// Pick n random items
for (let i = 0; i < random; i++) {
const other = Math.floor(Math.random() * (supporters.length - i));
const temp = supporters[other];
supporters[other] = supporters[i];
supporters[i] = temp;
}
supporters = supporters.slice(0, random);
}
}
// resort to keep order
supporters.sort((a, b) => getAmount(b) - getAmount(a));
return (
<>
<h2>
{rank === 'backer'
? 'Backers'
: rank === 'latest'
? 'Latest Sponsors'
: `${rank[0].toUpperCase()}${rank.slice(1)} ${
type === 'monthly' ? 'Monthly ' : ''
}Sponsors`}
</h2>
<VisibilitySensor
delayedCall
partialVisibility
intervalDelay={300}
onChange={this.handleInView}
>
<div className="support">
<div className="support__description">
{rank === 'backer' ? (
<p>
The following <b>Backers</b> are individuals who have
contributed various amounts of money in order to help support
webpack. Every little bit helps, and we appreciate even the
smallest contributions. This list shows {random} randomly
chosen backers:
</p>
) : rank === 'latest' ? (
<p>
The following persons/organizations made their first donation
in the last {Math.round(maxAge / (1000 * 60 * 60 * 24))} days
(limited to the top {limit}).
</p>
) : (
<p>
<b className="support__rank">
{type === 'monthly' ? rank + ' monthly' : rank} sponsors
</b>
{type === 'monthly' ? (
<span>
are those who are currently pledging{' '}
{minimum ? `$${formatMoney(minimum)}` : 'up'}{' '}
{maximum ? `to $${formatMoney(maximum)}` : 'or more'}{' '}
monthly to webpack.
</span>
) : (
<span>
are those who have pledged{' '}
{minimum ? `$${formatMoney(minimum)}` : 'up'}{' '}
{maximum ? `to $${formatMoney(maximum)}` : 'or more'} to
webpack.
</span>
)}
</p>
)}
</div>
{supporters.map((supporter, index) => (
<a
key={supporter.slug || index}
className="support__item"
title={`$${formatMoney(supporter.totalDonations / 100)} by ${
supporter.name || supporter.slug
} ($${formatMoney(supporter.monthlyDonations / 100)} monthly)`}
target="_blank"
rel="noopener noreferrer nofollow"
href={
supporter.website ||
`https://opencollective.com/${supporter.slug}`
}
>
{
<img
className={`support__${rank}-avatar support__image`}
src={
inView && supporter.avatar ? supporter.avatar : SmallIcon
}
alt={
supporter.name || supporter.slug
? `${supporter.name || supporter.slug}'s avatar`
: 'avatar'
}
onError={this._handleImgError}
/>
}
</a>
))}
<div className="support__bottom">
<a
className="support__button"
href="https://opencollective.com/webpack#support"
>
Become a {rank === 'backer' ? 'backer' : 'sponsor'}
</a>
</div>
</div>
</VisibilitySensor>
</>
);
}
/**
* Handle images that aren't found
*
* @param {object} e - React synthetic event
*/
_handleImgError(e) {
const imgNode = e.target;
if (imgNode.getAttribute('src') === SmallIcon) return;
imgNode.setAttribute('src', SmallIcon);
}
}