diff --git a/lib/github.ts b/lib/github.ts index 5788742..de4dd7c 100644 --- a/lib/github.ts +++ b/lib/github.ts @@ -34,6 +34,22 @@ type TotalFollowersResponse = { } } +type TotalStarsResponse = { + data: { + user: { + repositories: { + nodes: [ + { + stargazers: { + totalCount: number + } + } + ] + } + } + } +} + export async function getPinnedRepos() { const response = await fetch(GITHUB_GRAPHQL, { method: 'POST', @@ -107,4 +123,30 @@ export async function getTotalFollowers() { }).then(r => r.json()) as TotalFollowersResponse return response.data.user.followers.totalCount +} + +export async function getTotalStars(totalRepos: number) { + const response = await fetch(GITHUB_GRAPHQL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}` + }, + body: JSON.stringify({ + query: `{ + user(login: "r-freeman") { + repositories(first: ${totalRepos}) { + nodes { + stargazers { + totalCount + } + } + } + } + }` + }) + }).then(r => r.json()) as TotalStarsResponse + + return response.data.user.repositories.nodes + .reduce((acc, node) => acc + node.stargazers.totalCount, 0) } \ No newline at end of file diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx index 502e5bc..794743c 100644 --- a/pages/dashboard.tsx +++ b/pages/dashboard.tsx @@ -2,10 +2,16 @@ import Head from 'next/head' import {SimpleLayout} from '@/components/SimpleLayout' import {Card} from '@/components/Card' import {numberFormat} from '@/lib/numberFormat' -import {getTotalFollowers, getTotalRepos} from '@/lib/github' +import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github' import {GetStaticProps} from 'next' -export default function Dashboard({totalRepos, totalFollowers}: { totalRepos: number, totalFollowers: number }) { +type DashboardProps = { + totalRepos: number + totalFollowers: number + totalStars: number +} + +export default function Dashboard({totalRepos, totalFollowers, totalStars}: DashboardProps) { return ( <> @@ -48,6 +54,14 @@ export default function Dashboard({totalRepos, totalFollowers}: { totalRepos: nu {numberFormat(totalFollowers)} + +

+ GitHub Stars +

+ + {numberFormat(totalStars)} + +
@@ -55,13 +69,18 @@ export default function Dashboard({totalRepos, totalFollowers}: { totalRepos: nu } export const getStaticProps: GetStaticProps = async () => { - const totalRepos = await getTotalRepos() - const totalFollowers = await getTotalFollowers() + const [totalRepos, totalFollowers] = await Promise.all([ + getTotalRepos(), + getTotalFollowers() + ]) + + const totalStars = await getTotalStars(totalRepos) return { props: { totalRepos, - totalFollowers + totalFollowers, + totalStars } } }