feat(api): implement precomputed widget data storage and retrieval.

This commit is contained in:
shay
2026-04-18 11:13:43 -04:00
parent f0c6edacf6
commit 79b9c31197
7 changed files with 207 additions and 94 deletions
+67 -23
View File
@@ -1,9 +1,10 @@
import { useState, useRef, useCallback } from 'react'
import { useState, useRef, useCallback, useEffect } from 'react'
import './index.css'
import { SearchScreen } from './screens/SearchScreen'
import { WorkshopScreen } from './screens/WorkshopScreen'
import { ResultScreen } from './screens/ResultScreen'
import type { WidgetData } from './lib/renderWidgets'
import { DEMO_WIDGET_DATA } from './lib/demoData'
export type Achievement = {
title: string
@@ -43,41 +44,81 @@ const DEFAULT_SETTINGS: WidgetSettings = {
type Step = 'search' | 'workshop' | 'result'
const POLL_INTERVAL_MS = 2000
const POLL_TIMEOUT_MS = 120_000
export default function App() {
const [step, setStep] = useState<Step>('search')
const [username, setUsername] = useState('')
const [settings, setSettings] = useState<WidgetSettings>({ ...DEFAULT_SETTINGS })
const [widgetData, setWidgetData] = useState<WidgetData | null>(null)
const [isDemo, setIsDemo] = useState(false)
const [fetchDone, setFetchDone] = useState(false)
const [fetchError, setFetchError] = useState<string | null>(null)
const fetchRef = useRef<AbortController | null>(null)
const pollTimer = useRef<number | null>(null)
const pollStart = useRef<number>(0)
const aborted = useRef(false)
const stopPolling = useCallback(() => {
if (pollTimer.current != null) {
window.clearTimeout(pollTimer.current)
pollTimer.current = null
}
}, [])
useEffect(() => stopPolling, [stopPolling])
const startFetch = useCallback((user: string) => {
if (fetchRef.current) fetchRef.current.abort()
const ctrl = new AbortController()
fetchRef.current = ctrl
stopPolling()
aborted.current = false
pollStart.current = Date.now()
setFetchDone(false)
setFetchError(null)
setWidgetData(null)
// Show demo data immediately so Workshop is interactive while the worker builds.
setWidgetData(DEMO_WIDGET_DATA)
setIsDemo(true)
fetch(`/api/${encodeURIComponent(user)}/data`, { signal: ctrl.signal })
.then(async r => {
if (r.status === 404) throw new Error('User not found')
if (!r.ok) throw new Error(`Fetch failed (${r.status})`)
return r.json()
})
.then(json => {
setWidgetData(json.data)
setFetchDone(true)
})
.catch(e => {
if (e.name !== 'AbortError') {
console.warn('Fetch error:', e)
setFetchError(e.message)
const poll = async () => {
if (aborted.current) return
try {
const r = await fetch(`/api/${encodeURIComponent(user)}/data`)
if (aborted.current) return
const body = await r.json().catch(() => ({} as { status?: string; data?: WidgetData }))
const status = body.status
if (r.status === 200 && status === 'ready' && body.data) {
setWidgetData(body.data)
setIsDemo(false)
setFetchDone(true)
return
}
})
}, [])
if (r.status === 404 || status === 'not_found') {
setFetchError('User not found')
setFetchDone(true)
return
}
if (r.status === 429 || status === 'rate_limited') {
setFetchError('Enrollment limit reached — try again tomorrow')
setFetchDone(true)
return
}
// 202 building (or any non-terminal) — poll again until timeout.
if (Date.now() - pollStart.current > POLL_TIMEOUT_MS) {
setFetchError('Still building — refresh to keep waiting')
setFetchDone(true)
return
}
pollTimer.current = window.setTimeout(poll, POLL_INTERVAL_MS)
} catch (e) {
if (aborted.current) return
console.warn('Data poll failed:', e)
setFetchError('Network error')
setFetchDone(true)
}
}
poll()
}, [stopPolling])
const handleSearch = (user: string) => {
setUsername(user)
@@ -92,7 +133,8 @@ export default function App() {
const handleBack = () => {
if (step === 'result') setStep('workshop')
else if (step === 'workshop') {
if (fetchRef.current) fetchRef.current.abort()
aborted.current = true
stopPolling()
setStep('search')
}
}
@@ -110,6 +152,8 @@ export default function App() {
onGenerate={handleGenerate}
onBack={handleBack}
fetchDone={fetchDone}
fetchError={fetchError}
isDemo={isDemo}
widgetData={widgetData}
/>
)}