Added total github stars to dashboard

This commit is contained in:
r-freeman 2023-01-28 15:01:32 +00:00
parent 6cc0e2bf39
commit 0e6c2b9a33
2 changed files with 66 additions and 5 deletions

View File

@ -34,6 +34,22 @@ type TotalFollowersResponse = {
} }
} }
type TotalStarsResponse = {
data: {
user: {
repositories: {
nodes: [
{
stargazers: {
totalCount: number
}
}
]
}
}
}
}
export async function getPinnedRepos() { export async function getPinnedRepos() {
const response = await fetch(GITHUB_GRAPHQL, { const response = await fetch(GITHUB_GRAPHQL, {
method: 'POST', method: 'POST',
@ -108,3 +124,29 @@ export async function getTotalFollowers() {
return response.data.user.followers.totalCount 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)
}

View File

@ -2,10 +2,16 @@ import Head from 'next/head'
import {SimpleLayout} from '@/components/SimpleLayout' import {SimpleLayout} from '@/components/SimpleLayout'
import {Card} from '@/components/Card' import {Card} from '@/components/Card'
import {numberFormat} from '@/lib/numberFormat' import {numberFormat} from '@/lib/numberFormat'
import {getTotalFollowers, getTotalRepos} from '@/lib/github' import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github'
import {GetStaticProps} from 'next' 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 ( return (
<> <>
<Head> <Head>
@ -48,6 +54,14 @@ export default function Dashboard({totalRepos, totalFollowers}: { totalRepos: nu
{numberFormat(totalFollowers)} {numberFormat(totalFollowers)}
</Card.Description> </Card.Description>
</Card> </Card>
<Card as="li">
<h2 className="text-base font-semibold transition group-hover:text-indigo-500 text-zinc-800 dark:text-zinc-400">
<Card.Link href="https://github.com/r-freeman">GitHub Stars</Card.Link>
</h2>
<Card.Description className="text-zinc-800 dark:text-zinc-100 font-semibold text-5xl">
{numberFormat(totalStars)}
</Card.Description>
</Card>
</ul> </ul>
</SimpleLayout> </SimpleLayout>
</> </>
@ -55,13 +69,18 @@ export default function Dashboard({totalRepos, totalFollowers}: { totalRepos: nu
} }
export const getStaticProps: GetStaticProps = async () => { export const getStaticProps: GetStaticProps = async () => {
const totalRepos = await getTotalRepos() const [totalRepos, totalFollowers] = await Promise.all([
const totalFollowers = await getTotalFollowers() getTotalRepos(),
getTotalFollowers()
])
const totalStars = await getTotalStars(totalRepos)
return { return {
props: { props: {
totalRepos, totalRepos,
totalFollowers totalFollowers,
totalStars
} }
} }
} }