11import Head from 'next/head' ;
2+ import dayjs from 'dayjs' ;
23import { GetServerSidePropsContext , GetServerSidePropsResult } from 'next' ;
34import { useEffect } from 'react' ;
45import { useInfiniteQuery , useQueryClient , QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
@@ -13,6 +14,27 @@ type Props = {
1314 currentStreak : number ;
1415} ;
1516
17+ const fetchContribution = async ( username : string , to : string ) : Promise < number [ ] > => {
18+ console . log ( `https://forked-gh-contributions.deno.dev/${ username } .json?to=${ to } ` ) ;
19+
20+ const res = await fetch ( `https://forked-gh-contributions.deno.dev/${ username } .json?to=${ to } ` ) ;
21+ const json = await res . json ( ) ;
22+
23+ const contributions = json . contributions
24+ . flat ( )
25+ . reverse ( )
26+ . map ( ( c : { contributionCount : number } ) => c . contributionCount ) ;
27+
28+ // slice(1) because the first element is today's contribution count
29+ if ( contributions . slice ( 1 ) . every ( ( c : number ) => c !== 0 ) ) {
30+ const oldestDate = dayjs ( json . contributions . flat ( ) [ 0 ] . date ) . subtract ( 1 , 'days' ) . format ( 'YYYY-MM-DD' ) ;
31+
32+ return [ ...contributions , ...( await fetchContribution ( username , oldestDate ) ) ] ;
33+ }
34+
35+ return contributions ;
36+ } ;
37+
1638export const getServerSideProps = async (
1739 context : GetServerSidePropsContext ,
1840) : Promise < GetServerSidePropsResult < Props > > => {
@@ -22,18 +44,10 @@ export const getServerSideProps = async (
2244 return { notFound : true } ;
2345 }
2446
25- const res = await fetch ( `https://github-contributions-api.deno.dev/${ username } .json` ) ;
26- const json = await res . json ( ) ;
27-
28- const contributions = json . contributions
29- . flat ( )
30- . reverse ( )
31- . map ( ( c : { contributionCount : number } ) => c . contributionCount ) ;
32-
47+ const contributions = await fetchContribution ( username , '' ) ;
3348 const [ todayContributionCount , yesterdayContributionCount ] = contributions ;
3449
35- const streak = todayContributionCount > 0 ? contributions . indexOf ( 0 ) : contributions . slice ( 1 ) . indexOf ( 0 ) ;
36- const currentStreak = streak < 0 ? '365+' : streak ;
50+ const currentStreak = todayContributionCount > 0 ? contributions . indexOf ( 0 ) : contributions . slice ( 1 ) . indexOf ( 0 ) ;
3751
3852 return { props : { username, todayContributionCount, yesterdayContributionCount, currentStreak } } ;
3953} ;
0 commit comments