mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(ResultScreen): add widget status and manual refresh functionality.
This commit is contained in:
@@ -52,6 +52,21 @@ type Step = 'search' | 'workshop' | 'result'
|
||||
|
||||
const POLL_INTERVAL_MS = 2000
|
||||
const POLL_TIMEOUT_MS = 120_000
|
||||
// ResultScreen poll: a bit slower than the workshop poll because each tick
|
||||
// re-fetches the full SVG, not a small JSON. 3s spacing is responsive
|
||||
// enough to feel near-instant when the widget lands.
|
||||
const RESULT_POLL_INTERVAL_MS = 3000
|
||||
// Give up after 3 minutes — long enough to cover even worst-case
|
||||
// first-time GitHub fetches; past that the user can hit "Check now".
|
||||
const RESULT_POLL_TIMEOUT_MS = 180_000
|
||||
|
||||
type WidgetStatus = 'ready' | 'building' | 'not_found' | 'rate_limited'
|
||||
|
||||
function readWidgetStatus(r: Response): WidgetStatus {
|
||||
const raw = r.headers.get('X-Widget-Status')
|
||||
if (raw === 'building' || raw === 'not_found' || raw === 'rate_limited') return raw
|
||||
return 'ready'
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
const [me, setMe] = useState<Me>({ login: null })
|
||||
@@ -71,9 +86,16 @@ export default function App() {
|
||||
// settings). For visitors it's /api/<u>?<query>, which round-trips their
|
||||
// Workshop edits without touching the owner's saved widget.
|
||||
const [embedUrl, setEmbedUrl] = useState<string>('')
|
||||
// 'ready' once the backend has actually rendered the user's widget; until
|
||||
// then the SVG returned by /api/<u> is the "Building @user's widget…"
|
||||
// placeholder. We poll on this so the page upgrades itself from
|
||||
// placeholder to real widget without the user having to refresh.
|
||||
const [widgetStatus, setWidgetStatus] = useState<'ready' | 'building' | 'not_found' | 'rate_limited'>('ready')
|
||||
const pollTimer = useRef<number | null>(null)
|
||||
const pollStart = useRef<number>(0)
|
||||
const aborted = useRef(false)
|
||||
const resultPollTimer = useRef<number | null>(null)
|
||||
const resultPollStart = useRef<number>(0)
|
||||
|
||||
const stopPolling = useCallback(() => {
|
||||
if (pollTimer.current != null) {
|
||||
@@ -82,9 +104,53 @@ export default function App() {
|
||||
}
|
||||
}, [])
|
||||
|
||||
const stopResultPolling = useCallback(() => {
|
||||
if (resultPollTimer.current != null) {
|
||||
window.clearTimeout(resultPollTimer.current)
|
||||
resultPollTimer.current = null
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Pull and re-render the embed SVG once. Used both by the auto-poll loop
|
||||
// (when the first /api/<u> returned a building placeholder) and by the
|
||||
// manual "Check now" button on ResultScreen.
|
||||
const refetchEmbed = useCallback(async () => {
|
||||
if (!embedUrl) return
|
||||
try {
|
||||
const r = await fetch(`${embedUrl}${embedUrl.includes('?') ? '&' : '?'}t=${Date.now()}`)
|
||||
if (!r.ok) return
|
||||
const status = readWidgetStatus(r)
|
||||
const text = await r.text()
|
||||
setGeneratedSvg(text)
|
||||
setWidgetStatus(status)
|
||||
} catch {
|
||||
/* transient — let the next poll tick try again */
|
||||
}
|
||||
}, [embedUrl])
|
||||
|
||||
useEffect(() => stopPolling, [stopPolling])
|
||||
useEffect(() => stopResultPolling, [stopResultPolling])
|
||||
useEffect(() => { fetchMe().then(setMe) }, [])
|
||||
|
||||
// Result-screen auto-refresh. Backend serves a "Building @user's widget…"
|
||||
// placeholder SVG (with header X-Widget-Status: building) until the
|
||||
// worker finishes the first build. Without this loop the user would
|
||||
// stare at the placeholder until they manually reloaded.
|
||||
useEffect(() => {
|
||||
if (step !== 'result' || widgetStatus !== 'building' || !embedUrl) return
|
||||
resultPollStart.current = Date.now()
|
||||
const tick = async () => {
|
||||
await refetchEmbed()
|
||||
if (Date.now() - resultPollStart.current > RESULT_POLL_TIMEOUT_MS) {
|
||||
stopResultPolling()
|
||||
return
|
||||
}
|
||||
resultPollTimer.current = window.setTimeout(tick, RESULT_POLL_INTERVAL_MS)
|
||||
}
|
||||
resultPollTimer.current = window.setTimeout(tick, RESULT_POLL_INTERVAL_MS)
|
||||
return stopResultPolling
|
||||
}, [step, widgetStatus, embedUrl, refetchEmbed, stopResultPolling])
|
||||
|
||||
const startFetch = useCallback((user: string) => {
|
||||
stopPolling()
|
||||
aborted.current = false
|
||||
@@ -199,6 +265,7 @@ export default function App() {
|
||||
setGenerating(true)
|
||||
setGeneratedSvg(null)
|
||||
setGenerateError(null)
|
||||
setWidgetStatus('ready')
|
||||
const isOwner = !!me.login && me.login.toLowerCase() === username.toLowerCase()
|
||||
try {
|
||||
if (isOwner) {
|
||||
@@ -249,6 +316,7 @@ export default function App() {
|
||||
}
|
||||
setGeneratedSvg(await svgRes.text())
|
||||
setEmbedUrl(cleanUrl)
|
||||
setWidgetStatus(readWidgetStatus(svgRes))
|
||||
} else {
|
||||
// Visitor path: encode Workshop edits into a query string and hit
|
||||
// the same URL we'll hand the user for their README. No owner
|
||||
@@ -268,6 +336,7 @@ export default function App() {
|
||||
}
|
||||
setGeneratedSvg(await svgRes.text())
|
||||
setEmbedUrl(url)
|
||||
setWidgetStatus(readWidgetStatus(svgRes))
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('generate call failed:', e)
|
||||
@@ -315,8 +384,10 @@ export default function App() {
|
||||
generatedSvg={generatedSvg}
|
||||
generateError={generateError}
|
||||
embedUrl={embedUrl}
|
||||
widgetStatus={widgetStatus}
|
||||
onBack={handleBack}
|
||||
onRegenerate={handleGenerate}
|
||||
onRefresh={refetchEmbed}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -18,11 +18,17 @@ type Props = {
|
||||
// We display whichever URL was used to produce the SVG above, so the
|
||||
// snippet they copy matches what they're previewing.
|
||||
embedUrl: string
|
||||
// 'building' means the SVG above is still the "Building @user's
|
||||
// widget…" placeholder; the parent is auto-polling for the real one.
|
||||
widgetStatus: 'ready' | 'building' | 'not_found' | 'rate_limited'
|
||||
onBack: () => void
|
||||
onRegenerate: () => void
|
||||
// Manual refetch — same fetch the auto-poll runs, exposed as a button
|
||||
// so the user has a way to nudge it if they want.
|
||||
onRefresh: () => void
|
||||
}
|
||||
|
||||
export function ResultScreen({ username, generating, generatedSvg, generateError, embedUrl, onBack, onRegenerate }: Props) {
|
||||
export function ResultScreen({ username, generating, generatedSvg, generateError, embedUrl, widgetStatus, onBack, onRegenerate, onRefresh }: Props) {
|
||||
const safeSvg = useMemo(() => (generatedSvg ? sanitizeSvg(generatedSvg) : ''), [generatedSvg])
|
||||
const [copied, setCopied] = useState(false)
|
||||
const fallbackUrl = `${window.location.origin}/api/${encodeURIComponent(username)}`
|
||||
@@ -95,6 +101,18 @@ export function ResultScreen({ username, generating, generatedSvg, generateError
|
||||
|
||||
{!generating && !generateError && safeSvg && (
|
||||
<>
|
||||
{widgetStatus === 'building' && (
|
||||
<div className="mt-6 flex items-center gap-2 text-xs text-gray-500">
|
||||
<div className="w-3 h-3 border-2 border-gray-300 border-t-gray-600 rounded-full animate-spin-slow" />
|
||||
<span>Still building on the server — this page will update automatically when ready.</span>
|
||||
<button
|
||||
onClick={onRefresh}
|
||||
className="ml-2 px-2 py-1 rounded-md text-gray-600 hover:text-gray-900 hover:bg-gray-100 transition-colors"
|
||||
>
|
||||
Check now
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<div className="w-full mt-6 rounded-2xl bg-white shadow-lg ring-1 ring-gray-200 p-6 flex justify-center">
|
||||
{/*
|
||||
Force the inline SVG to scale up to the card width. The
|
||||
|
||||
Reference in New Issue
Block a user