mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:07:40 +01:00
fix(projects): GitHubStatus times out after 15s instead of skeleton-forever
useFetch grew an optional timeoutMs that wires an AbortController to the underlying fetch and surfaces an "AbortError" as a normal error string so the caller's existing error branch handles it. GitHubStatus passes 15_000ms — well under the server's 30s github-status.sh ceiling and short enough that a slow upstream resolves to FetchErrorState + Retry instead of an indefinite spinner. Verified live on /projects: at ~13s the skeleton transitions to "Couldn't load GitHub status / Retry" with no other panels affected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2 files changed
+21
-5
No files matched your search
@@ -22,7 +22,7 @@ const STATUS_ICONS: Record<string, { icon: typeof CheckCircle; className: string
|
||||
};
|
||||
|
||||
export function GitHubStatus() {
|
||||
const { data, loading, error, refetch } = useFetch<{ repos: RepoStatus[]; error?: string }>("/api/github", { intervalMs: 2 * 60_000 });
|
||||
const { data, loading, error, refetch } = useFetch<{ repos: RepoStatus[]; error?: string }>("/api/github", { intervalMs: 2 * 60_000, timeoutMs: 15_000 });
|
||||
const repos = data?.repos ?? [];
|
||||
|
||||
return (
|
||||
|
||||
+20
-4
@@ -3,7 +3,10 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { getJson } from "@/lib/api/fetch";
|
||||
|
||||
export function useFetch<T>(url: string | null, { intervalMs }: { intervalMs?: number } = {}) {
|
||||
export function useFetch<T>(
|
||||
url: string | null,
|
||||
{ intervalMs, timeoutMs }: { intervalMs?: number; timeoutMs?: number } = {},
|
||||
) {
|
||||
const [data, setData] = useState<T | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -16,21 +19,34 @@ export function useFetch<T>(url: string | null, { intervalMs }: { intervalMs?: n
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
getJson<T>(url)
|
||||
const controller = timeoutMs ? new AbortController() : null;
|
||||
const timeoutId = timeoutMs && controller
|
||||
? setTimeout(() => controller.abort(), timeoutMs)
|
||||
: null;
|
||||
|
||||
getJson<T>(url, controller ? { signal: controller.signal } : undefined)
|
||||
.then((json) => {
|
||||
if (!cancelled) setData(json);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (!cancelled) setError(err instanceof Error ? err.message : "Failed to load");
|
||||
if (cancelled) return;
|
||||
if (err instanceof DOMException && err.name === "AbortError") {
|
||||
setError(`Timed out after ${Math.round((timeoutMs ?? 0) / 1000)}s`);
|
||||
} else {
|
||||
setError(err instanceof Error ? err.message : "Failed to load");
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
if (!cancelled) setLoading(false);
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
controller?.abort();
|
||||
};
|
||||
}, [url, revision]);
|
||||
}, [url, revision, timeoutMs]);
|
||||
|
||||
// Silent background refresh — setRevision is stable so no ref needed.
|
||||
useEffect(() => {
|
||||
|
||||
Reference in new issue
Block a user