-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathuseGitHubStats.ts
More file actions
211 lines (179 loc) · 5.91 KB
/
useGitHubStats.ts
File metadata and controls
211 lines (179 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"use client";
import { useEffect, useState } from "react";
interface GitHubStats {
stars: number;
forks: number;
issues: number;
contributors: number;
}
interface UseGitHubStatsReturn {
stats: GitHubStats;
loading: boolean;
error: string | null;
lastUpdated: Date | null;
}
// Production-ready GitHub Stats Hook with Smart Rate Limiting
export const useGitHubStats = (): UseGitHubStatsReturn => {
const [stats, setStats] = useState<GitHubStats>({
stars: 0,
forks: 0,
issues: 0,
contributors: 0,
});
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
// Production configuration
const CACHE_KEY = "github-stats-javaistic";
const CACHE_DURATION = 2 * 60 * 60 * 1000; // 2 hours - conservative for production
const STALE_WHILE_REVALIDATE = 24 * 60 * 60 * 1000; // 24 hours - serve stale data if needed
// Conservative fallback data based on current repo stats
const PRODUCTION_FALLBACK: GitHubStats = {
stars: 40,
forks: 19,
issues: 4,
contributors: 15,
};
// Enhanced cache management with versioning
const getCachedData = () => {
try {
const cached = localStorage.getItem(CACHE_KEY);
if (!cached) return null;
const parsed = JSON.parse(cached);
const now = Date.now();
// Always return cached data if it exists and is less than 24 hours old
if (parsed.timestamp && now - parsed.timestamp < STALE_WHILE_REVALIDATE) {
// Mark if data is fresh or stale
return {
...parsed,
isFresh: now - parsed.timestamp < CACHE_DURATION,
};
}
return null;
} catch (err) {
console.warn("Cache read error:", err);
return null;
}
};
const setCachedData = (
data: GitHubStats,
metadata: { rateLimited?: boolean; source?: string } = {},
) => {
try {
const cacheData = {
data,
timestamp: Date.now(),
version: "1.0",
metadata: {
rateLimited: metadata.rateLimited || false,
source: metadata.source || "github-api",
userAgent: "javaistic-website",
},
};
localStorage.setItem(CACHE_KEY, JSON.stringify(cacheData));
setLastUpdated(new Date());
} catch (err) {
console.warn("Cache write error:", err);
}
};
const fetchWithRateLimitHandling = async () => {
try {
setLoading(true);
setError(null);
// Check cache first - always prefer cached data in production
const cachedData = getCachedData();
if (cachedData?.data) {
setStats(cachedData.data);
// If cache is fresh, don't make API call
if (cachedData.isFresh) {
setLoading(false);
return;
}
}
// Make conservative API call - single endpoint only
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 8000); // 8 second timeout
const response = await fetch(
"https://api.github.com/repos/javaistic/javaistic",
{
headers: {
Accept: "application/vnd.github.v3+json",
"X-GitHub-Api-Version": "2022-11-28",
},
signal: controller.signal,
},
);
clearTimeout(timeoutId);
// Check rate limit headers proactively
const remaining = response.headers.get("X-RateLimit-Remaining");
if (remaining && parseInt(remaining) < 5) {
console.warn(
`GitHub API rate limit low: ${remaining} requests remaining`,
);
}
if (response.status === 403 || response.status === 429) {
console.warn("GitHub API rate limited, using cached/fallback data");
if (cachedData?.data) {
setStats(cachedData.data);
setCachedData(cachedData.data, {
rateLimited: true,
source: "cache-fallback",
});
} else {
setStats(PRODUCTION_FALLBACK);
setCachedData(PRODUCTION_FALLBACK, {
rateLimited: true,
source: "production-fallback",
});
}
setError(null); // Don't show error for rate limiting in production
return;
}
if (!response.ok) {
throw new Error(`API Error: ${response.status}`);
}
const repoData = await response.json();
// Conservative contributor count estimation (avoid second API call)
const estimatedContributors = Math.max(
Math.floor((repoData.stargazers_count || 0) / 3), // Rough heuristic
PRODUCTION_FALLBACK.contributors,
);
const freshStats: GitHubStats = {
stars: repoData.stargazers_count || PRODUCTION_FALLBACK.stars,
forks: repoData.forks_count || PRODUCTION_FALLBACK.forks,
issues: repoData.open_issues_count || PRODUCTION_FALLBACK.issues,
contributors: estimatedContributors,
};
setStats(freshStats);
setCachedData(freshStats, { source: "github-api-success" });
} catch (err) {
console.warn("GitHub stats fetch error:", err);
// Graceful degradation - use cached data or fallback
const cachedData = getCachedData();
if (cachedData?.data) {
setStats(cachedData.data);
setError(null); // Don't show error if we have cached data
} else {
setStats(PRODUCTION_FALLBACK);
setCachedData(PRODUCTION_FALLBACK, { source: "error-fallback" });
setError(null); // Silent fallback in production
}
} finally {
setLoading(false);
}
};
useEffect(() => {
// Initial load
fetchWithRateLimitHandling();
// Conservative refresh interval - 4 hours in production
const refreshInterval = 4 * 60 * 60 * 1000;
const interval = setInterval(fetchWithRateLimitHandling, refreshInterval);
return () => clearInterval(interval);
}, []);
return {
stats,
loading,
error,
lastUpdated,
};
};