Skip to content

Commit 8274253

Browse files
committed
Utilize OneGraph to join GitHub Sponsors & OpenCollective
1 parent 8ae19df commit 8274253

File tree

1 file changed

+41
-63
lines changed

1 file changed

+41
-63
lines changed

src/theme/BackerBanner/index.js

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,15 @@
1-
// TODO: This file needs a massive rewrite for GitHub Sponsors
21
import React, { useState, useEffect } from 'react';
32
import classnames from 'classnames';
43
import shuffle from 'lodash.shuffle';
54
import ExternalLink from '@theme/ExternalLink';
65

76
import styles from './banner.module.scss';
87

9-
const collectiveURL ='https://rest.opencollective.com/v2/gulpjs/orders/incoming?status=active,paid&limit=1000';
8+
const sponsorsURL = 'https://serve.onegraph.com/graphql?app_id=c8251aa1-22ab-4dca-9e57-e7c335ddcd7c';
109

11-
function uniqueBySlug(array) {
12-
const predicate = function (o) {
13-
return o.fromAccount.slug;
14-
}
15-
return array.reduce(function(acc, curr) {
16-
return acc.filter(function (a) {
17-
return predicate(a) === predicate(curr);
18-
}).length ? acc : acc.push(curr) && acc
19-
}, []);
20-
}
21-
22-
const specialFilter = [
23-
'addyosmani',
24-
];
25-
26-
const thirtyTwoDaysAgo = Date.now() - 2.765e+9;
27-
28-
function withinMonth(createdAt) {
29-
const datePaid = new Date(createdAt);
30-
return datePaid >= thirtyTwoDaysAgo;
31-
}
32-
33-
function recentNonCompanies(backer) {
34-
if (backer.fromAccount && specialFilter.includes(backer.fromAccount.slug)) {
35-
return false;
36-
}
37-
38-
if (backer.fromAccount && backer.fromAccount.type === 'ORGANIZATION') {
39-
return false;
40-
}
41-
42-
if (backer.tier && backer.tier.slug === 'company') {
43-
return false;
44-
}
45-
46-
if (backer.frequency === 'MONTHLY' && backer.status === 'ACTIVE') {
47-
return true;
48-
}
49-
50-
if (backer.frequency === 'ONETIME' && backer.status === 'PAID' && withinMonth(backer.createdAt)) {
10+
function between5And250(backer) {
11+
const amount = backer.tier.amountDonated;
12+
if (amount >= 500 && amount < 25000) {
5113
return true;
5214
}
5315

@@ -59,38 +21,54 @@ function selectRandom(backers) {
5921
}
6022

6123
async function getBackers() {
62-
const response = await fetch(collectiveURL);
63-
const orders = await response.json();
24+
const response = await fetch(sponsorsURL, {
25+
method: 'POST',
26+
headers: {
27+
'Content-Type': 'application/json',
28+
},
29+
body: JSON.stringify({
30+
doc_id: 'fe685787-b348-42a4-960c-2322add1e11b',
31+
}),
32+
});
6433

65-
const allBackers = orders.nodes.filter(recentNonCompanies);
34+
// TODO: Handle errors
35+
const { data, errors } = await response.json();
6636

67-
const uniqueBackers = uniqueBySlug(allBackers);
68-
const backersToDisplay = selectRandom(uniqueBackers)
37+
let ghBackers = data.gitHub.organization.sponsors.nodes;
38+
let ocBackers = data.openCollective.organization.sponsors.nodes;
39+
let allBackers = [].concat(ghBackers, ocBackers);
6940

70-
return backersToDisplay.map(function(backer) {
71-
const fromAccount = backer.fromAccount;
72-
const totalDonations = backer.totalDonations;
41+
const validBackers = allBackers.filter(between5And250);
7342

74-
const name = fromAccount.name;
75-
const slug = fromAccount.slug;
76-
const website = fromAccount.website;
77-
const twitterHandle = fromAccount.twitterHandle;
78-
const imageUrl = fromAccount.imageUrl;
43+
const backersToDisplay = selectRandom(validBackers)
44+
45+
return backersToDisplay.map(function (backer) {
46+
const {
47+
name,
48+
openCollectiveHandle,
49+
twitterHandle,
50+
githubHandle,
51+
avatarUrl
52+
} = backer.sponsor;
53+
// It is in US cents
54+
const monthlyAmount = (backer.tier.amountDonated / 100);
7955

8056
let href;
81-
if (website) {
82-
href = website;
57+
if (githubHandle) {
58+
href = `https://github.com/${githubHandle}`;
8359
} else if (twitterHandle) {
84-
href = 'https://twitter.com/' + twitterHandle
60+
href = `https://twitter.com/${twitterHandle}`;
8561
} else {
86-
href = 'https://opencollective.com/' + slug
62+
href = `https://opencollective.com/${openCollectiveHandle}`
8763
}
8864

65+
let usersName = name || githubHandle || twitterHandle || openCollectiveHandle || '';
66+
8967
return {
90-
key: slug,
91-
src: imageUrl,
92-
alt: name,
93-
title: `Thank you ${name} for $${totalDonations.value}!`,
68+
key: href,
69+
src: avatarUrl,
70+
alt: usersName,
71+
title: `Thank you ${usersName} for the $${monthlyAmount}/month!`,
9472
href: href,
9573
};
9674
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy