import { useCallback, useState } from 'react' import { runJob } from './api' // Shared shape for "kick off a backend job, show progress, surface errors". export function useJob() { const [running, setRunning] = useState(false) const [progress, setProgress] = useState([]) const [error, setError] = useState('') const start = useCallback(async (path, body) => { setRunning(true) setProgress([]) setError('') try { return await runJob(path, body, setProgress) } catch (e) { setError(e.message) throw e } finally { setRunning(false) } }, []) return { running, progress, error, setError, start } } export function ProgressBox({ progress }) { if (!progress.length) return null return (
{progress.slice(0, -1).map((m, i) =>
{m}
)}
{progress[progress.length - 1]}
) } export function ErrorBox({ error, onRetry }) { if (!error) return null return (
{error} {onRetry && (
)}
) }