import { useEffect, useRef, useState } from 'react'; import { asset } from '../lib/asset.js'; import { calculate, formatForDeck, loadInputs } from '../lib/calc.js'; import { buildPersonalisedDeck, getExampleDeck, prefetchTemplate, triggerDownload } from '../lib/pptxReport.js'; const SLIDE_COUNT = 15; const slides = Array.from({ length: SLIDE_COUNT }, (_, i) => asset(`img/slides/slide-${String(i + 1).padStart(2, '0')}.jpg`)); export default function DeckViewer({ active, registrant }) { const [cur, setCur] = useState(0); const [busy, setBusy] = useState(false); const thumbsRef = useRef(null); const go = (n) => setCur(Math.max(0, Math.min(SLIDE_COUNT - 1, n))); async function downloadDeck() { if (busy) return; setBusy(true); try { const i = loadInputs(registrant); const r = calculate(i); const values = formatForDeck(i, r, registrant); const meta = { company: registrant?.company || 'Your Company', date: registrant?.date }; // Slide 3 shows their device count (from the form) straight away. Slide 12 keeps the // example figures until they've entered their own numbers in the calculator. const blob = i.touched && r.complete ? await buildPersonalisedDeck(values, meta) : values.subs ? await buildPersonalisedDeck(values, meta, { roi: false }) : await getExampleDeck(); triggerDownload(blob, 'BlackDice_Turn_Network_Protection_Strategy_Deck.pptx'); } catch (err) { console.error(err); } finally { setBusy(false); } } // Warm the template cache while the deck is being viewed useEffect(() => { if (active) prefetchTemplate().catch(() => {}); }, [active]); useEffect(() => { if (!active) return; const onKey = (e) => { if (['INPUT', 'SELECT', 'TEXTAREA'].includes(e.target.tagName)) return; if (e.key === 'ArrowRight' || e.key === 'ArrowDown') { e.preventDefault(); setCur((c) => Math.min(SLIDE_COUNT - 1, c + 1)); } if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') { e.preventDefault(); setCur((c) => Math.max(0, c - 1)); } }; document.addEventListener('keydown', onKey); return () => document.removeEventListener('keydown', onKey); }, [active]); useEffect(() => { const thumb = thumbsRef.current?.children[cur]; thumb?.scrollIntoView({ block: 'nearest', inline: 'center', behavior: 'smooth' }); }, [cur]); // Preload the next slide for snappy navigation useEffect(() => { if (cur + 1 < SLIDE_COUNT) new Image().src = slides[cur + 1]; }, [cur]); return (
Turn Network Protection into Competitive Advantage
15 slides · Use arrow keys or click to navigate · Click thumbnails to jump to any slide
{`Slide
{cur + 1} / {SLIDE_COUNT}
{slides.map((src, i) => ( ))}
); }