import { useEffect, useRef, useState } from 'preact/hooks'; import { api } from '../api.ts'; import { printStatus } from '../bookstate.ts'; import { useDialog } from '../dialog.ts'; import { useQueue } from '../live.ts'; import { removeWithUndo } from '../removal.ts'; import { sheetCount } from '../sheets.ts'; import { showError, showToast } from '../toast-store.ts'; import { QueueRow } from '../components/BookRow.tsx'; interface MadePdf { ids: number[]; url: string } export function CardsToPrint({ onFix }: { onFix: (id: number) => void }) { const queue = useQueue(); const status = printStatus(queue); const [open, setOpen] = useState(false); const [made, setMade] = useState(null); const pdfUrl = useRef(null); useEffect(() => () => { if (pdfUrl.current) URL.revokeObjectURL(pdfUrl.current); }, []); // Nothing left to print while the panel is open (for example the last card was removed): close it. useEffect(() => { if (open && status.ready === 0 && !made) setOpen(false); }, [open, status.ready, made]); function forget() { if (pdfUrl.current) URL.revokeObjectURL(pdfUrl.current); pdfUrl.current = null; setMade(null); } return (

Cards to print {queue.length}

{status.reason ? {status.reason} : null}
{queue.length === 0 ? (

No cards waiting. Scan a book to start.

) : (
    {queue.map(b => onFix(b.id)} onRemove={() => removeWithUndo(b)} />)}
)}
); } /** Where the first card goes, then the PDF, then "did it print?". Anchored under the print button. */ function PrintPanel({ ready, made, setMade, pdfUrl, forget, onClose }: { ready: number; made: MadePdf | null; setMade: (m: MadePdf | null) => void; pdfUrl: { current: string | null }; forget: () => void; onClose: () => void; }) { const ref = useRef(null); useDialog(ref, onClose, false); const [start, setStart] = useState(1); const [busy, setBusy] = useState(false); const sheets = sheetCount(ready, start); // Keep keyboard focus inside when the panel changes from the question to the confirmation. const first = useRef(true); useEffect(() => { if (first.current) { first.current = false; return; } ref.current?.querySelector('.btn-primary')?.focus(); }, [made === null]); async function makePdf() { setBusy(true); // Open the tab inside the click so the popup blocker allows it, then point it at the PDF. const tab = window.open('', '_blank'); try { const { blob, ids } = await api.fetchPdf(start); if (pdfUrl.current) URL.revokeObjectURL(pdfUrl.current); // the previous PDF is no longer needed const url = URL.createObjectURL(new Blob([blob], { type: 'application/pdf' })); pdfUrl.current = url; if (tab) tab.location.href = url; setMade({ ids, url }); } catch (e) { tab?.close(); showError(`Could not make the PDF: ${(e as Error).message}`); } setBusy(false); } async function confirm() { if (!made) return; try { await api.confirmPrint(made.ids); showToast(`Cleared ${made.ids.length} ${made.ids.length === 1 ? 'card' : 'cards'} from the list`); forget(); onClose(); } catch (e) { showError(`Could not clear the cards: ${(e as Error).message}`); } } return ( ); }