import { useEffect, useRef, useState } from 'react' import { calculate, formatForDeck, loadInputs } from './calc.js' import { buildPersonalisedDeck, getExampleDeck, prefetchTemplate, triggerDownload } from './pptxReport.js' import type { Registrant } from './RoiLanding' const SLIDE_COUNT = 15 const slides = Array.from({ length: SLIDE_COUNT }, (_, i) => `/roi/slides/slide-${String(i + 1).padStart(2, '0')}.jpg`) export default function RoiDeckViewer({ active, registrant }: { active: boolean; registrant: Registrant }) { const [cur, setCur] = useState(0) const [busy, setBusy] = useState(false) const thumbsRef = useRef(null) const go = (n: number) => setCur(Math.max(0, Math.min(SLIDE_COUNT - 1, n))) async function downloadDeck() { if (busy) return setBusy(true) try { const i: any = loadInputs(registrant) const r = calculate(i) const values = formatForDeck(i, r, registrant) const meta = { company: registrant?.company || 'Your Company' } // 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: KeyboardEvent) => { const tag = (e.target as HTMLElement)?.tagName if (['INPUT', 'SELECT', 'TEXTAREA'].includes(tag)) 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] as HTMLElement | undefined 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) => ( ))}
) }