import { useEffect, useState } from "react";
function ScrapeProgress({ jobId }: { jobId: string }) {
const [progress, setProgress] = useState({ completed: 0, total: 0 });
const [status, setStatus] = useState("queued");
useEffect(() => {
const source = new EventSource(
`https://api.reader.dev/v1/jobs/${jobId}/stream`,
// NOTE: EventSource in the browser can't set custom headers directly.
// Proxy the stream through your own backend that adds the x-api-key,
// or use the `event-source-polyfill` package.
);
source.addEventListener("progress", (ev) => {
const data = JSON.parse(ev.data);
setProgress({ completed: data.completed, total: data.total });
setStatus(data.status);
});
source.addEventListener("done", (ev) => {
const data = JSON.parse(ev.data);
setStatus(data.status);
source.close();
});
return () => source.close();
}, [jobId]);
const pct = progress.total > 0 ? (progress.completed / progress.total) * 100 : 0;
return (
<div>
<div style={{ width: "100%", background: "#eee", height: 12 }}>
<div style={{ width: `${pct}%`, background: "#10b981", height: 12 }} />
</div>
<p>
{progress.completed} / {progress.total} ({status})
</p>
</div>
);
}