This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
useNodeJsContributorsApi.tsx
164 lines (142 loc) · 4.7 KB
/
useNodeJsContributorsApi.tsx
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
/* eslint-disable camelcase */
import { useEffect, useState } from 'react';
import type {
Contributor,
ContributorApiResponse,
} from '../types/nodejsContributors';
const LIMIT_CONTRIBUTORS = 5;
const CONTRIBUTORS_API_URI = `https://api.github.com/repos/nodejs/node/contributors?per_page=${LIMIT_CONTRIBUTORS}`;
/**
* Parses "Link" response header from node/contributors API
* Returns page values for "next" and "last" API URLs
* @param linkHeader
*/
export function linkParser(linkHeader: string): {
[key: string]: {
url: string;
page: number;
};
} {
const regex = new RegExp(
`<([^?]+\\?per_page=${LIMIT_CONTRIBUTORS}&[a-z]+=([\\d]+))>;[\\s]*rel="([a-z]+)"`,
'g'
);
let array: RegExpExecArray | null = null;
const object = {};
do {
array = regex.exec(linkHeader);
if (array) {
object[array[3]] = {
url: array[1],
page: parseInt(array[2], 10),
};
}
} while (array !== null);
return object;
}
/**
* Retrieves max amount of contributors for Node.js main repo.
* Returns array with random contributor index and max contributors found.
*/
export async function getMaxContributors(): Promise<[number, number]> {
const response = await fetch(CONTRIBUTORS_API_URI);
const linksHeaderValue = response.headers.get('Link');
if (linksHeaderValue) {
const links = linkParser(linksHeaderValue);
const randomPage =
Math.floor(Math.random() * Math.floor(links?.last?.page ?? 1)) + 1;
return [randomPage, links?.last?.page ?? 1];
}
throw new Error('Failed to get amount if max contributors');
}
/**
* Retrieves a contributor's object by it's index in API
* @param randomPage
*/
export async function getContributor(randomPage: number): Promise<Contributor> {
const response = await fetch(`${CONTRIBUTORS_API_URI}&page=${randomPage}`);
const jsonResponse = (await response.json()) as ContributorApiResponse[];
const contributorData: Contributor[] = jsonResponse.map(
({ avatar_url, login, contributions, html_url }) => ({
avatarUri: avatar_url,
login,
contributionsCount: contributions,
profileUri: html_url,
commitsListUri: `https://github.com/nodejs/node/commits?author=${login}`,
})
);
const contributor = contributorData.shift() as Contributor;
if (window.localStorage) {
window.localStorage.setItem(
'contributors',
JSON.stringify(contributorData)
);
}
return contributor;
}
/**
* Calls relative APIs and returns random contributor for Node.js main repo.
* Trying to store cached data in localStorage in order to do less consequent requests
*/
export async function fetchRandomContributor(): Promise<Contributor> {
let maxContributors: number | null = null;
let fetchDate: number | null = null;
let needToRefetch = false;
let contributors: Contributor[] = [];
const ONE_MONTH_MS = 2592000000;
if (window.localStorage) {
const maxContributorsStored =
window.localStorage.getItem('max_contributors');
const fetchDateStored = window.localStorage.getItem('fetch_date');
contributors = JSON.parse(
window.localStorage.getItem('contributors') || '[]'
);
maxContributors = maxContributorsStored
? parseInt(maxContributorsStored, 10)
: null;
fetchDate = fetchDateStored ? parseInt(fetchDateStored, 10) : null;
}
if (fetchDate && Date.now() - fetchDate >= ONE_MONTH_MS) needToRefetch = true;
return new Promise((resolve, reject) => {
if (contributors && contributors.length > 0 && !needToRefetch) {
const contributor = contributors.shift();
if (window.localStorage) {
localStorage.setItem('contributors', JSON.stringify(contributors));
}
resolve(contributor as Contributor);
} else if (maxContributors && !needToRefetch) {
getContributor(
Math.floor(Math.random() * Math.floor(maxContributors)) + 1
)
.then(contributor => resolve(contributor))
.catch(error => reject(error));
} else {
getMaxContributors()
.then(([randomPage]) => getContributor(randomPage))
.then(contributor => {
if (window.localStorage) {
window.localStorage.setItem('fetch_date', String(Date.now()));
}
resolve(contributor);
})
.catch(error => reject(error));
}
});
}
export function useNodeJsContributorsApi(
isVisible: boolean
): Contributor | null | undefined {
const [contributor, setContributor] = useState<Contributor | null>(null);
useEffect(() => {
if (isVisible) {
fetchRandomContributor()
.then(contributorList => {
setContributor(contributorList);
})
.catch(() => {
setContributor(null);
});
}
}, [isVisible]);
return contributor;
}