-
-
Notifications
You must be signed in to change notification settings - Fork 38
feat: add GitHub API rate limit indicator to monitor API usage #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| import './App.css' | ||
| import RateLimitIndicator from "./RateLimitIndicator"; | ||
|
|
||
| function App() { | ||
|
|
||
| return ( | ||
| <> | ||
| <h1>Hello, OrgExplorer!</h1> | ||
|
|
||
| <RateLimitIndicator /> | ||
|
|
||
| </> | ||
| ) | ||
| } | ||
|
|
||
| export default App | ||
| export default App |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| interface RateLimitData { | ||
| limit: number; | ||
| remaining: number; | ||
| reset: number; | ||
| } | ||
|
|
||
| export default function RateLimitIndicator() { | ||
| const [rateLimit, setRateLimit] = useState<RateLimitData | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| async function fetchRateLimit() { | ||
| try { | ||
| const response = await fetch("https://api.github.com/rate_limit"); | ||
| const data = await response.json(); | ||
|
|
||
| setRateLimit({ | ||
| limit: data.rate.limit, | ||
| remaining: data.rate.remaining, | ||
| reset: data.rate.reset, | ||
| }); | ||
| } catch (error) { | ||
| console.error("Error fetching rate limit:", error); | ||
| } | ||
| } | ||
|
|
||
| fetchRateLimit(); | ||
| }, []); | ||
|
|
||
| if (!rateLimit) return <p>Loading API status...</p>; | ||
|
Comment on lines
+15
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't leave the widget stuck in loading on failed responses. If GitHub returns 403/5xx or an unexpected payload, the 🛠️ Suggested error-state handling export default function RateLimitIndicator() {
const [rateLimit, setRateLimit] = useState<RateLimitData | null>(null);
+ const [status, setStatus] = useState<"loading" | "ready" | "error">("loading");
useEffect(() => {
async function fetchRateLimit() {
try {
const response = await fetch("https://api.github.com/rate_limit");
+ if (!response.ok) {
+ throw new Error(`GitHub rate limit request failed: ${response.status}`);
+ }
const data = await response.json();
setRateLimit({
limit: data.rate.limit,
remaining: data.rate.remaining,
reset: data.rate.reset,
});
+ setStatus("ready");
} catch (error) {
console.error("Error fetching rate limit:", error);
+ setStatus("error");
}
}
fetchRateLimit();
}, []);
- if (!rateLimit) return <p>Loading API status...</p>;
+ if (status === "loading") return <p>Loading API status...</p>;
+ if (status === "error" || !rateLimit) return <p>API status unavailable</p>;🤖 Prompt for AI Agents |
||
|
|
||
| const resetTime = new Date(rateLimit.reset * 1000).toLocaleTimeString(); | ||
|
|
||
| return ( | ||
| <div | ||
| style={{ | ||
| position: "fixed", | ||
| bottom: "20px", | ||
| right: "20px", | ||
| background: "#1f2937", | ||
| padding: "12px", | ||
| borderRadius: "8px", | ||
| color: "white", | ||
| fontSize: "14px", | ||
| }} | ||
| > | ||
| <strong>GitHub API Status</strong> | ||
| <p>Remaining: {rateLimit.remaining} / {rateLimit.limit}</p> | ||
| <p>Reset: {resetTime}</p> | ||
|
Comment on lines
+31
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Externalize the new UI copy.
As per coding guidelines, "Internationalization: User-visible strings should be externalized to resource files (i18n)". 🤖 Prompt for AI Agents |
||
| </div> | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refresh the quota after GitHub API activity.
This effect runs only once on mount, so
remainingandresetbecome stale as soon as the user makes more GitHub requests. That undercuts the goal of using this widget to monitor quota while exploring orgs. Please either wire it to the same GitHub request layer the app uses or poll/refetch on an interval.♻️ Minimal polling-based fix
useEffect(() => { + let cancelled = false; + async function fetchRateLimit() { try { const response = await fetch("https://api.github.com/rate_limit"); const data = await response.json(); - setRateLimit({ - limit: data.rate.limit, - remaining: data.rate.remaining, - reset: data.rate.reset, - }); + if (!cancelled) { + setRateLimit({ + limit: data.rate.limit, + remaining: data.rate.remaining, + reset: data.rate.reset, + }); + } } catch (error) { console.error("Error fetching rate limit:", error); } } fetchRateLimit(); + const intervalId = window.setInterval(fetchRateLimit, 30000); + + return () => { + cancelled = true; + window.clearInterval(intervalId); + }; }, []);📝 Committable suggestion
🤖 Prompt for AI Agents