Spaces:
Runtime error
Runtime error
Khunndev
Sisyphus
Make the counting-to-results moment theatrical with a mono count-up and precision timer.
eef382a Download frontend/src/components/StepResults.tsx from npisb/pipecountapp-dev: direct link, hf CLI and curl.
- Browser
- Download file 4.42 kB
-
https://huggingface.co/spaces/npisb/pipecountapp-dev/resolve/main/frontend/src/components/StepResults.tsx
- Command line
-
hf download hf://spaces/npisb/pipecountapp-dev/frontend/src/components/StepResults.tsx
-
curl -L -o StepResults.tsx https://huggingface.co/spaces/npisb/pipecountapp-dev/resolve/main/frontend/src/components/StepResults.tsx
4.42 kB
| import { useEffect, useRef, useState } from 'react' | |
| import { t } from '../lib/i18n' | |
| import type { Detection, DiameterGroup, Lang, ToolMode } from '../lib/types' | |
| import { ResultCanvas } from './ResultCanvas' | |
| import { ResultsDock } from './ResultsDock' | |
| import { ExportSheet } from './ExportSheet' | |
| import { exportCanvasPng, exportCsv, exportJson } from '../lib/export' | |
| interface Props { | |
| lang: Lang | |
| image: HTMLImageElement | |
| detections: Detection[] | |
| groups: DiameterGroup[] | |
| counts: Record<number, number> | |
| showConfidenceLabels: boolean | |
| onUpdate: (dets: Detection[]) => void | |
| onExpectedChange: (groupId: number, value: number | undefined) => void | |
| onRecount: () => void | |
| onNewPhoto: () => void | |
| } | |
| export function StepResults({ | |
| lang, | |
| image, | |
| detections, | |
| groups, | |
| counts, | |
| showConfidenceLabels, | |
| onUpdate, | |
| onExpectedChange, | |
| onRecount, | |
| onNewPhoto, | |
| }: Props) { | |
| const [tool, setTool] = useState<ToolMode>('view') | |
| const [selectedGroupId, setSelectedGroupId] = useState(groups[0]?.id ?? 0) | |
| const [animDone, setAnimDone] = useState(false) | |
| const [exportOpen, setExportOpen] = useState(false) | |
| const countDisplayRef = useRef<HTMLSpanElement>(null) | |
| const canvasRef = useRef<HTMLCanvasElement | null>(null) | |
| const viewportRef = useRef<HTMLDivElement>(null) | |
| const [viewportSize, setViewportSize] = useState<{ w: number; h: number } | null>(null) | |
| useEffect(() => { | |
| const el = viewportRef.current | |
| if (!el || !animDone) { | |
| setViewportSize(null) | |
| return | |
| } | |
| const measure = () => { | |
| const w = el.clientWidth | |
| const h = el.clientHeight | |
| if (w > 0 && h > 0) setViewportSize({ w, h }) | |
| } | |
| measure() | |
| const ro = new ResizeObserver(measure) | |
| ro.observe(el) | |
| return () => ro.disconnect() | |
| }, [animDone]) | |
| return ( | |
| <div | |
| className={ | |
| animDone | |
| ? 'grid min-h-0 flex-1 grid-rows-[minmax(0,1fr)_auto] overflow-hidden bg-[var(--bg)]' | |
| : 'flex min-h-0 flex-1 flex-col overflow-hidden bg-[var(--bg)]' | |
| } | |
| > | |
| <div | |
| ref={viewportRef} | |
| className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden px-2 pt-2 pb-1" | |
| > | |
| <ResultCanvas | |
| image={image} | |
| detections={detections} | |
| groups={groups} | |
| tool={tool} | |
| selectedGroupId={selectedGroupId} | |
| animate | |
| showCircleLabels={animDone} | |
| circleLabelMode={showConfidenceLabels ? 'confidence' : 'count'} | |
| displayBounds={animDone ? viewportSize : null} | |
| onUpdate={onUpdate} | |
| onCanvasReady={(c) => { canvasRef.current = c }} | |
| onAnimationComplete={() => setAnimDone(true)} | |
| countDisplayRef={countDisplayRef} | |
| /> | |
| {!animDone && ( | |
| <div className="pointer-events-none absolute left-1/2 top-3 z-10 -translate-x-1/2"> | |
| <div | |
| className="grain overlay-enter flex flex-col items-center rounded-[var(--r-lg)] border border-[var(--hairline)] bg-[var(--surface)]/95 px-7 py-3 backdrop-blur-md" | |
| style={{ boxShadow: 'var(--shadow-lg)' }} | |
| > | |
| <span ref={countDisplayRef} className="ct-count"> | |
| 0 | |
| </span> | |
| <span | |
| className="ct-section-label" | |
| style={{ marginTop: '0.125rem', letterSpacing: 'var(--tracking-wider)' }} | |
| > | |
| {t(lang, 'total')} | |
| </span> | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| {animDone && ( | |
| <ResultsDock | |
| lang={lang} | |
| total={detections.length} | |
| tool={tool} | |
| groups={groups} | |
| counts={counts} | |
| selectedGroupId={selectedGroupId} | |
| onToolChange={setTool} | |
| onSelectGroup={setSelectedGroupId} | |
| onExpectedChange={onExpectedChange} | |
| onNewPhoto={onNewPhoto} | |
| onRecount={onRecount} | |
| onExport={() => setExportOpen(true)} | |
| /> | |
| )} | |
| <ExportSheet | |
| lang={lang} | |
| open={exportOpen} | |
| onClose={() => setExportOpen(false)} | |
| onExportPng={() => canvasRef.current && exportCanvasPng(canvasRef.current)} | |
| onExportJson={() => exportJson(detections, groups, counts)} | |
| onExportCsv={() => exportCsv(detections, groups)} | |
| canExportPng={!!canvasRef.current} | |
| canExportData={detections.length > 0} | |
| /> | |
| </div> | |
| ) | |
| } | |