Spaces:
Sleeping
Sleeping
File size: 7,943 Bytes
df4a1a2 b1ae7de df4a1a2 39cf1e2 df4a1a2 39cf1e2 df4a1a2 563ed2b df4a1a2 563ed2b df4a1a2 563ed2b df4a1a2 39cf1e2 df4a1a2 563ed2b df4a1a2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | import { useCallback, useEffect, useRef, useState } from 'react'
import './index.css'
import UploadView from './components/UploadView'
import ProcessingView from './components/ProcessingView'
import ResultsView from './components/ResultsView'
import HealthDashboard from './components/HealthDashboard'
import LandingPage from './components/LandingPage'
import API from 'api'
function hasSeenLanding() {
try {
return window.sessionStorage.getItem('landingSeen') === '1'
} catch {
return false
}
}
function markLandingSeen() {
try {
window.sessionStorage.setItem('landingSeen', '1')
} catch {
// Ignore storage failures (private mode / blocked storage).
}
}
export default function App() {
const [showLanding, setShowLanding] = useState(() => !hasSeenLanding())
const [tool, setTool] = useState('extractor')
const [view, setView] = useState('upload')
const [jobs, setJobs] = useState([])
const [activeJob, setActiveJob] = useState(null)
const pollersRef = useRef(new Map())
const updateJob = useCallback((jobId, updates) => {
setJobs((currentJobs) => currentJobs.map((job) => (
job.id === jobId ? { ...job, ...updates } : job
)))
setActiveJob((currentJob) => (
currentJob?.id === jobId ? { ...currentJob, ...updates } : currentJob
))
}, [])
const stopPolling = useCallback((jobId) => {
const poller = pollersRef.current.get(jobId)
if (poller) {
window.clearInterval(poller)
pollersRef.current.delete(jobId)
}
}, [])
const pollJob = useCallback((jobId) => {
if (pollersRef.current.has(jobId)) {
return
}
const interval = window.setInterval(async () => {
try {
const statusResponse = await fetch(`${API}/api/status/${jobId}`)
const statusData = await statusResponse.json()
if (statusData.status === 'done') {
stopPolling(jobId)
const resultsResponse = await fetch(`${API}/api/results/${jobId}`)
const resultsData = await resultsResponse.json()
updateJob(jobId, {
status: 'done',
annotation: resultsData.annotation,
duration: resultsData.duration,
})
setActiveJob((currentJob) => {
if (currentJob?.id === jobId) {
setView('results')
return {
...currentJob,
status: 'done',
annotation: resultsData.annotation,
duration: resultsData.duration,
}
}
return currentJob
})
} else if (statusData.status === 'error') {
stopPolling(jobId)
updateJob(jobId, {
status: 'error',
error: statusData.error,
})
}
} catch (error) {
console.error('Polling failed', error)
}
}, 1000)
pollersRef.current.set(jobId, interval)
}, [stopPolling, updateJob])
useEffect(() => () => {
for (const poller of pollersRef.current.values()) {
window.clearInterval(poller)
}
pollersRef.current.clear()
}, [])
const handleUpload = useCallback(async (files) => {
const newJobs = []
for (const file of files) {
const formData = new FormData()
formData.append('file', file)
try {
const response = await fetch(`${API}/api/upload-and-process`, {
method: 'POST',
body: formData,
})
const data = await response.json()
if (!response.ok || !data?.job_id) {
console.error('Upload failed', data)
continue
}
newJobs.push({
id: data.job_id,
filename: data.filename,
status: data.status || 'queued',
annotation: null,
duration: null,
imageUrl: `${API}/api/image/${data.job_id}`,
size_bytes: file.size,
})
} catch (error) {
console.error('Upload failed', error)
}
}
if (!newJobs.length) {
return
}
setJobs((currentJobs) => [...newJobs, ...currentJobs])
setActiveJob(newJobs[0])
setView('processing')
newJobs.forEach((job) => pollJob(job.id))
}, [pollJob])
const handleRetryJob = useCallback(async (job) => {
if (!job?.id) return
try {
const response = await fetch(`${API}/api/process/${job.id}`, {
method: 'POST',
})
if (!response.ok) {
throw new Error(`Retry failed with status ${response.status}`)
}
updateJob(job.id, { status: 'queued', error: null })
pollJob(job.id)
} catch (error) {
console.error('Retry failed', error)
}
}, [pollJob, updateJob])
const handleOpenJob = useCallback((job) => {
setTool('extractor')
setActiveJob(job)
if (job.status === 'done') {
setView('results')
return
}
setView('processing')
pollJob(job.id)
}, [pollJob])
const handleGoHome = useCallback(() => {
setView('upload')
setActiveJob(null)
}, [])
const handleDismissLanding = useCallback(() => {
markLandingSeen()
setShowLanding(false)
}, [])
const showStudio = tool === 'extractor' && view === 'results' && activeJob
const processedCount = jobs.filter((job) => job.status === 'done').length
return (
<>
{showLanding && <LandingPage onDismiss={handleDismissLanding} />}
<div className={`app-shell${showStudio ? ' is-studio' : ''}`}>
{!showStudio && (
<header className="app-header">
<button
type="button"
className="app-brand"
onClick={() => {
setTool('extractor')
handleGoHome()
}}
>
<div className="app-brand-mark">
<svg
className="fedora-boomerang"
viewBox="0 0 24 24"
width="24"
height="24"
fill="currentColor"
>
<path d="M2,16C2,16 5,14 12,14C19,14 22,16 22,16V17H2V16M12,5C8,5 6,8 6,10H18C18,8 16,5 12,5Z" />
</svg>
</div>
<span className="app-brand-copy">
<strong>Agent P-DF</strong>
<small>AI Table Extraction Studio</small>
</span>
</button>
<nav className="app-header-nav">
<button
type="button"
className={`btn btn-sm${tool === 'extractor' ? ' btn-primary' : ''}`}
onClick={() => setTool('extractor')}
>
Extractor
</button>
<button
type="button"
className={`btn btn-sm${tool === 'health' ? ' btn-primary' : ''}`}
onClick={() => setTool('health')}
>
System Health
</button>
{tool === 'extractor' && view !== 'upload' && (
<button type="button" className="btn btn-sm" onClick={handleGoHome}>
New Upload
</button>
)}
{processedCount > 0 && (
<button type="button" className="btn btn-sm btn-ghost">
{processedCount} processed
</button>
)}
</nav>
</header>
)}
{tool === 'health' ? (
<HealthDashboard />
) : (
<>
{view === 'upload' && (
<UploadView
onUpload={handleUpload}
jobs={jobs}
onOpenJob={handleOpenJob}
onRetryJob={handleRetryJob}
/>
)}
{view === 'processing' && activeJob && (
<ProcessingView job={activeJob} onBack={handleGoHome} />
)}
{view === 'results' && activeJob && (
<ResultsView
job={activeJob}
onBack={handleGoHome}
onJobUpdate={updateJob}
/>
)}
</>
)}
</div>
</>
)
}
|