1- // src/hooks/useGitHubStars.ts
2-
3- import { useState , useEffect } from 'react' ;
4- import {
5- getStarsFromCache ,
6- saveStarsToCache ,
7- getExpiredStarsFromCache
8- } from '../utils/githubCache' ;
9- import { Project } from '../components/projectCard' ;
1+ import { useState , useEffect } from "react" ;
2+ import {
3+ getStarsFromCache ,
4+ saveStarsToCache ,
5+ getExpiredStarsFromCache ,
6+ } from "../utils/githubCache" ;
7+ import { Project } from "../components/projectCard" ;
108
119interface RateLimit {
1210 remaining : number ;
@@ -25,56 +23,67 @@ export const useGitHubStars = (initialProjects: Project[]) => {
2523 const updatedProjects = await Promise . all (
2624 initialProjects . map ( async ( project ) => {
2725 let stars = 0 ;
28-
26+
2927 if ( project . githubLink ) {
3028 try {
3129 const url = new URL ( project . githubLink ) ;
32- const [ , owner , repo ] = url . pathname . split ( '/' ) ;
33-
30+ const [ , owner , repo ] = url . pathname . split ( "/" ) ;
31+
3432 if ( owner && repo ) {
3533 const repoKey = `${ owner } /${ repo } ` ;
36-
34+
3735 // Try to get data from cache if not forcing refresh
3836 if ( ! forceRefresh ) {
3937 const cachedStars = getStarsFromCache ( repoKey ) ;
4038 if ( cachedStars !== null ) {
4139 return { ...project , stars : cachedStars } ;
4240 }
4341 }
44-
45- const response = await fetch ( `https://api.github.com/repos/${ owner } /${ repo } ` ) ;
46-
47- const remaining = parseInt ( response . headers . get ( 'X-RateLimit-Remaining' ) || '0' ) ;
48- const resetTimestamp = parseInt ( response . headers . get ( 'X-RateLimit-Reset' ) || '0' ) * 1000 ;
42+
43+ const response = await fetch (
44+ `https://api.github.com/repos/${ owner } /${ repo } ` ,
45+ ) ;
46+
47+ const remaining = parseInt (
48+ response . headers . get ( "X-RateLimit-Remaining" ) || "0" ,
49+ ) ;
50+ const resetTimestamp =
51+ parseInt ( response . headers . get ( "X-RateLimit-Reset" ) || "0" ) *
52+ 1000 ;
4953 const resetTime = new Date ( resetTimestamp ) . toLocaleTimeString ( ) ;
50-
54+
5155 setRateLimit ( {
5256 remaining,
53- resetTime
57+ resetTime,
5458 } ) ;
55-
59+
5660 if ( response . ok ) {
5761 const data = await response . json ( ) ;
5862 stars = data . stargazers_count ;
59-
63+
6064 // Update cache for this repo
6165 saveStarsToCache ( repoKey , stars ) ;
6266 } else if ( response . status === 403 && remaining === 0 ) {
6367 // If we hit rate limit, try to use expired cache if available
6468 const expiredStars = getExpiredStarsFromCache ( repoKey ) ;
6569 if ( expiredStars !== null ) {
6670 stars = expiredStars ;
67- console . warn ( `Rate limit exceeded. Using expired cache for ${ repoKey } ` ) ;
71+ console . warn (
72+ `Rate limit exceeded. Using expired cache for ${ repoKey } ` ,
73+ ) ;
6874 }
6975 }
7076 }
7177 } catch ( error ) {
72- console . error ( `Failed to fetch stars for ${ project . projectName } :` , error ) ;
78+ console . error (
79+ `Failed to fetch stars for ${ project . projectName } :` ,
80+ error ,
81+ ) ;
7382 }
7483 }
75-
84+
7685 return { ...project , stars } ;
77- } )
86+ } ) ,
7887 ) ;
7988
8089 setProjects ( updatedProjects ) ;
@@ -85,10 +94,10 @@ export const useGitHubStars = (initialProjects: Project[]) => {
8594 fetchGitHubStars ( ) ;
8695 } , [ ] ) ;
8796
88- return {
89- projects,
90- isLoading,
91- rateLimit,
92- refreshStars : ( ) => fetchGitHubStars ( true )
97+ return {
98+ projects,
99+ isLoading,
100+ rateLimit,
101+ refreshStars : ( ) => fetchGitHubStars ( true ) ,
93102 } ;
94- } ;
103+ } ;
0 commit comments