Skip to content

Commit

Permalink
not use API route
Browse files Browse the repository at this point in the history
  • Loading branch information
nuotsu committed Aug 11, 2024
1 parent 8119c99 commit 1e6e7b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 85 deletions.
37 changes: 0 additions & 37 deletions src/app/(frontend)/api/stargazers/route.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/lib/fetch.ts

This file was deleted.

54 changes: 39 additions & 15 deletions src/ui/Reputation.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Octokit } from 'octokit'
import { stegaClean } from '@sanity/client/stega'
import { cn } from '@/lib/utils'
import Img from './Img'
import { fetchAPI } from '@/lib/fetch'

const octokit = new Octokit({
auth: process.env.NEXT_PUBLIC_GITHUB_TOKEN!,
})

export default async function Reputation({
reputation,
Expand All @@ -11,20 +15,7 @@ export default async function Reputation({
} & React.HTMLAttributes<HTMLDivElement>) {
if (!reputation) return null

const { count, avatars } = await fetchAPI<{
error?: string
count?: number
avatars?: { avatar_url: string; login: string }[]
}>('/stargazers', {
params: {
repo: stegaClean(reputation.repo),
limit: reputation.limit,
},
next: {
revalidate: 3600,
tags: ['stargazers'],
},
})
const { count, avatars } = await getStargazers(reputation)

const imgClassname = cn(
'aspect-square h-8 w-auto rounded-full border-2 border-canvas object-cover -mr-2 last:mr-0',
Expand Down Expand Up @@ -69,3 +60,36 @@ export default async function Reputation({
</div>
)
}

async function getStargazers(reputation?: Sanity.Reputation) {
if (!reputation) return {}

const [owner, repo] = stegaClean(reputation.repo)?.split('/') ?? []
const limit = Number(stegaClean(reputation.limit)) || 5

try {
const { data: { stargazers_count: count = 0 } = {} } =
await octokit.rest.repos.get({ owner, repo })

const { data: avatars } = await octokit.request(
'GET /repos/{owner}/{repo}/stargazers',
{
owner,
repo,
per_page: limit * 2,
page: Math.ceil(count / (limit * 2)),
},
)

return {
count,
avatars: avatars.reverse().slice(0, limit) as {
avatar_url: string
login: string
}[],
}
} catch (e) {
console.error(e)
return {}
}
}

0 comments on commit 1e6e7b7

Please sign in to comment.