File tree 3 files changed +45
-3
lines changed
3 files changed +45
-3
lines changed Original file line number Diff line number Diff line change
1
+ import { cache } from "react" ;
2
+
3
+ interface GitHubResponse {
4
+ stargazers_count : number ;
5
+ }
6
+
7
+ export const getStarCount = cache ( async ( ) => {
8
+ try {
9
+ const response = await fetch (
10
+ "https://api.github.com/repos/ahmedkhaleel2004/gitdiagram" ,
11
+ {
12
+ headers : {
13
+ Accept : "application/vnd.github.v3+json" ,
14
+ } ,
15
+ next : {
16
+ revalidate : 300 , // Cache for 5 minutes
17
+ } ,
18
+ } ,
19
+ ) ;
20
+
21
+ if ( ! response . ok ) {
22
+ throw new Error ( "Failed to fetch star count" ) ;
23
+ }
24
+
25
+ const data = ( await response . json ( ) ) as GitHubResponse ;
26
+ return data . stargazers_count ;
27
+ } catch ( error ) {
28
+ console . error ( "Error fetching star count:" , error ) ;
29
+ return null ;
30
+ }
31
+ } ) ;
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import Hero from "~/components/hero";
4
4
export default function HomePage ( ) {
5
5
return (
6
6
< main className = "flex-grow p-8" >
7
- < div className = "mx-auto my-4 max-w-4xl lg:my-12 " >
7
+ < div className = "mx-auto my-4 max-w-4xl lg:my-8 " >
8
8
< Hero />
9
9
< div className = "mt-12" > </ div >
10
10
< p className = "mx-auto mt-8 max-w-2xl text-center text-lg" >
Original file line number Diff line number Diff line change 1
1
import React from "react" ;
2
2
import Link from "next/link" ;
3
3
import { FaGithub } from "react-icons/fa" ;
4
+ import { getStarCount } from "~/app/_actions/github" ;
5
+
6
+ export async function Header ( ) {
7
+ const starCount = await getStarCount ( ) ;
8
+
9
+ const formatStarCount = ( count : number | null ) => {
10
+ if ( ! count ) return "0" ;
11
+ if ( count >= 1000 ) {
12
+ return `${ ( count / 1000 ) . toFixed ( 1 ) } k` ;
13
+ }
14
+ return count . toString ( ) ;
15
+ } ;
4
16
5
- export function Header ( ) {
6
17
return (
7
18
< header className = "border-b-[3px] border-black" >
8
19
< div className = "mx-auto flex h-16 max-w-4xl items-center justify-between px-8" >
@@ -32,7 +43,7 @@ export function Header() {
32
43
</ Link >
33
44
< span className = "flex items-center gap-1 text-sm font-medium text-black" >
34
45
< span className = "text-amber-400" > ★</ span >
35
- 1.3k
46
+ { formatStarCount ( starCount ) }
36
47
</ span >
37
48
</ nav >
38
49
</ div >
You can’t perform that action at this time.
0 commit comments