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
309 lines (279 loc) · 8.44 KB
/
Support.jsx
File metadata and controls
309 lines (279 loc) · 8.44 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Import External Dependencies
import PropTypes from "prop-types";
import { Component } from "react";
// Import Data
import SmallIcon from "../../assets/icon-square-small-slack.png";
import Tooltip from "../Tooltip/Tooltip.jsx";
import Additional from "./AdditionalSupporters.mjs";
/* eslint import/no-unresolved: ["error", { ignore: ["_supporters\.json$"] }] */
import Backers from "./_supporters.json";
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: {
minimum: 1,
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: {
minimum: 1,
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) {
return Math.round(number).toLocaleString("en-US");
}
const AVATAR_CLASSES = {
backer:
"inline-block w-[31px] h-[31px] rounded-full border border-white shadow-[0_0_0_1px_rgb(112,202,10)] overflow-hidden align-middle",
latest: "max-h-16 max-w-48 align-middle",
bronze: "max-h-8 max-w-24 align-middle",
silver: "max-h-16 max-w-48 align-middle",
gold: "max-h-24 max-w-72 align-middle",
platinum: "max-h-32 max-w-full min-[400px]:max-w-96 align-middle",
};
export default class Support extends Component {
static propTypes = {
rank: PropTypes.string,
type: PropTypes.string,
};
state = {
inView: false,
};
containerRef = null;
observer = null;
componentDidMount() {
this.observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting && !this.state.inView) {
this.setState({ inView: true });
this.observer.disconnect();
}
},
{ threshold: 0.1 },
);
if (this.containerRef) {
this.observer.observe(this.containerRef);
}
}
componentWillUnmount() {
if (this.observer) {
this.observer.disconnect();
}
}
render() {
const { rank, type } = this.props;
const { inView } = this.state;
let supporters = SUPPORTERS;
let minimum;
let maximum;
let maxAge;
let limit;
let 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" && 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>
<div
ref={(el) => (this.containerRef = el)}
className="flex flex-wrap justify-center px-2 pb-4"
>
<div className="w-full mb-4">
{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="capitalize mr-1">
{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 contributed{" "}
{minimum ? `$${formatMoney(minimum)}` : "up"}{" "}
{maximum ? `to $${formatMoney(maximum)}` : "or more"} to
webpack.
</span>
)}
</p>
)}
</div>
{supporters.map((supporter, index) => (
<Tooltip
key={supporter.slug || index}
content={`$${formatMoney(supporter.totalDonations / 100)} by ${
supporter.name || supporter.slug
} ($${formatMoney(supporter.monthlyDonations / 100)} monthly)`}
>
<a
className="relative mx-0.5 mb-0.5 dark:bg-white"
target="_blank"
rel="noopener noreferrer nofollow"
href={
supporter.website ||
`https://opencollective.com/${supporter.slug}`
}
>
{
<img
className={AVATAR_CLASSES[rank]}
src={
inView && supporter.avatar ? supporter.avatar : SmallIcon
}
alt={
supporter.alt ||
(supporter.name || supporter.slug
? `${supporter.name || supporter.slug}'s avatar`
: "avatar")
}
onError={this._handleImgError}
/>
}
</a>
</Tooltip>
))}
<div className="w-full mt-4">
<a
className="inline-block py-[0.4em] px-[1em] uppercase text-[#175d96] border border-[#175d96] rounded-4xl transition-all duration-[250ms] hover:bg-[#175d96] hover:text-white dark:text-[#4fa8ff] dark:border-[#4fa8ff] dark:hover:bg-[#4fa8ff] dark:hover:text-[#04131f]"
href="https://opencollective.com/webpack#support"
>
Become a {rank === "backer" ? "backer" : "sponsor"}
</a>
</div>
</div>
</>
);
}
/**
* Handle images that aren't found
*
* @param {object} event - React synthetic event
*/
_handleImgError(event) {
const imgNode = event.target;
if (imgNode.getAttribute("src") === SmallIcon) return;
imgNode.setAttribute("src", SmallIcon);
}
}