1
- // TODO: This file needs a massive rewrite for GitHub Sponsors
2
1
import React , { useState , useEffect } from 'react' ;
3
2
import classnames from 'classnames' ;
4
3
import shuffle from 'lodash.shuffle' ;
5
4
import ExternalLink from '@theme/ExternalLink' ;
6
5
7
6
import styles from './banner.module.scss' ;
8
7
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 ' ;
10
9
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 ) {
51
13
return true ;
52
14
}
53
15
@@ -59,38 +21,54 @@ function selectRandom(backers) {
59
21
}
60
22
61
23
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
+ } ) ;
64
33
65
- const allBackers = orders . nodes . filter ( recentNonCompanies ) ;
34
+ // TODO: Handle errors
35
+ const { data, errors } = await response . json ( ) ;
66
36
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 ) ;
69
40
70
- return backersToDisplay . map ( function ( backer ) {
71
- const fromAccount = backer . fromAccount ;
72
- const totalDonations = backer . totalDonations ;
41
+ const validBackers = allBackers . filter ( between5And250 ) ;
73
42
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 ) ;
79
55
80
56
let href ;
81
- if ( website ) {
82
- href = website ;
57
+ if ( githubHandle ) {
58
+ href = `https://github.com/ ${ githubHandle } ` ;
83
59
} else if ( twitterHandle ) {
84
- href = ' https://twitter.com/' + twitterHandle
60
+ href = ` https://twitter.com/${ twitterHandle } ` ;
85
61
} else {
86
- href = ' https://opencollective.com/' + slug
62
+ href = ` https://opencollective.com/${ openCollectiveHandle } `
87
63
}
88
64
65
+ let usersName = name || githubHandle || twitterHandle || openCollectiveHandle || '' ;
66
+
89
67
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 !` ,
94
72
href : href ,
95
73
} ;
96
74
} ) ;
0 commit comments