chore(ResultScreen): update props to reflect SVG generation process changes

This commit is contained in:
shay
2026-04-18 12:17:25 -04:00
parent 7b2301cec0
commit 8ac657b874
4 changed files with 68 additions and 56 deletions
+32 -9
View File
@@ -55,6 +55,11 @@ export default function App() {
const [isDemo, setIsDemo] = useState(false)
const [fetchDone, setFetchDone] = useState(false)
const [fetchError, setFetchError] = useState<string | null>(null)
// Final SVG produced by POST /api/<u>/generate. Rendered on the Result
// screen so the user sees the actual backend output, not demo data.
const [generatedSvg, setGeneratedSvg] = useState<string | null>(null)
const [generateError, setGenerateError] = useState<string | null>(null)
const [generating, setGenerating] = useState(false)
const pollTimer = useRef<number | null>(null)
const pollStart = useRef<number>(0)
const aborted = useRef(false)
@@ -128,14 +133,33 @@ export default function App() {
const handleGenerate = async () => {
setStep('result')
// Fire-and-forget: the backend renders the composite SVG from the
// fetcher's cached payload + current settings and persists it to
// widgets.db for README embeds. The Result screen itself still
// renders client-side from widgetData for immediate feedback.
setGenerating(true)
setGeneratedSvg(null)
setGenerateError(null)
try {
await fetch(`/api/${encodeURIComponent(username)}/generate`, { method: 'POST' })
const r = await fetch(`/api/${encodeURIComponent(username)}/generate`, { method: 'POST' })
if (r.status === 404) {
const body = await r.json().catch(() => ({} as { status?: string }))
setGenerateError(body.status === 'not_found' ? 'User not found' : 'Not enrolled')
return
}
if (!r.ok) {
setGenerateError(`Generate failed (HTTP ${r.status})`)
return
}
// Fetch the backend-rendered composite SVG. Cache-bust so regenerate
// after settings change shows the new version, not a stale browser copy.
const svgRes = await fetch(`/api/${encodeURIComponent(username)}?t=${Date.now()}`)
if (!svgRes.ok) {
setGenerateError(`SVG fetch failed (HTTP ${svgRes.status})`)
return
}
setGeneratedSvg(await svgRes.text())
} catch (e) {
console.warn('generate call failed:', e)
setGenerateError('Network error')
} finally {
setGenerating(false)
}
}
@@ -169,10 +193,9 @@ export default function App() {
{step === 'result' && (
<ResultScreen
username={username}
settings={settings}
fetchDone={fetchDone}
fetchError={fetchError}
widgetData={widgetData}
generating={generating}
generatedSvg={generatedSvg}
generateError={generateError}
onBack={handleBack}
/>
)}
+25 -38
View File
@@ -1,7 +1,5 @@
import { useMemo } from 'react'
import DOMPurify from 'dompurify'
import type { WidgetSettings } from '../App'
import { renderAllWidgets, type WidgetData } from '../lib/renderWidgets'
import { useMemo } from 'react'
function sanitizeSvg(raw: string): string {
return DOMPurify.sanitize(raw, {
@@ -13,68 +11,57 @@ function sanitizeSvg(raw: string): string {
type Props = {
username: string
settings: WidgetSettings
fetchDone: boolean
fetchError: string | null
widgetData: WidgetData | null
generating: boolean
generatedSvg: string | null
generateError: string | null
onBack: () => void
}
export function ResultScreen({ username, settings, fetchDone, fetchError, widgetData, onBack }: Props) {
const svgContent = useMemo(() => {
if (!widgetData) return ''
const raw = renderAllWidgets({
data: widgetData,
theme: settings.theme,
widgets: settings.widgets,
widgetOrder: ['grade', 'impact', 'collaborators', 'focus', 'languages', 'achievements'],
achievements: settings.achievements.filter(a => a.title.trim()),
widgetSettings: settings.widgetSettings,
username,
})
return sanitizeSvg(raw)
}, [widgetData, settings, username])
const waiting = !fetchDone
export function ResultScreen({ username, generating, generatedSvg, generateError, onBack }: Props) {
const safeSvg = useMemo(() => (generatedSvg ? sanitizeSvg(generatedSvg) : ''), [generatedSvg])
const embedUrl = `${window.location.origin}/api/${encodeURIComponent(username)}`
const embedSnippet = `![${username}](${embedUrl})`
return (
<div className="min-h-screen flex flex-col items-center animate-fade-in-up">
{/* Back button */}
<div className="w-full max-w-2xl px-6 pt-6">
<button
onClick={onBack}
className="flex items-center gap-1.5 text-xs text-gray-400 hover:text-gray-600 transition-colors"
>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<path d="M19 12H5M12 19l-7-7 7-7"/>
<path d="M19 12H5M12 19l-7-7 7-7" />
</svg>
Back
</button>
</div>
<div className="flex-1 flex flex-col items-center justify-center px-6 pb-12">
<div className="flex-1 flex flex-col items-center justify-center px-6 pb-12 w-full max-w-2xl">
<h2 className="font-serif text-2xl text-gray-800 tracking-tight mb-1">
Your Widget
</h2>
{waiting ? (
{generating && (
<div className="mt-8 flex flex-col items-center gap-3">
<div className="w-5 h-5 border-2 border-gray-300 border-t-gray-600 rounded-full animate-spin-slow" />
<p className="text-xs text-gray-400">Generating with real data...</p>
<p className="text-xs text-gray-400">Generating with real data…</p>
</div>
) : !svgContent ? (
)}
{!generating && generateError && (
<div className="mt-8 flex flex-col items-center gap-3">
<p className="text-[10px] text-amber-500">
{fetchError ?? 'Could not fetch live data'}
</p>
<p className="text-xs text-red-500">{generateError}</p>
</div>
) : (
)}
{!generating && !generateError && safeSvg && (
<>
<p className="text-xs text-gray-400 mb-6">
Generated with real data
</p>
{/* SVG sanitized via DOMPurify.sanitize() before rendering */}
<div dangerouslySetInnerHTML={{ __html: svgContent }} />
<p className="text-xs text-gray-400 mb-6">Rendered from live data</p>
<div className="w-full flex justify-center" dangerouslySetInnerHTML={{ __html: safeSvg }} />
<div className="w-full mt-8">
<p className="text-xs text-gray-500 mb-2">Embed in your README:</p>
<pre className="bg-gray-100 text-xs p-3 rounded overflow-x-auto">{embedSnippet}</pre>
</div>
</>
)}
</div>