Files
blackdice-roi-toolkit/src/components/DeckViewer.jsx
T
2026-10-13 17:04:40 +05:00

98 lines
4.2 KiB
React

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 (
<div className="deck-viewer-wrap">
<div className="deck-viewer-top">
<div>
<div className="deck-viewer-title">Turn Network Protection into Competitive Advantage</div>
<div className="deck-viewer-sub">15 slides · Use arrow keys or click to navigate · Click thumbnails to jump to any slide</div>
</div>
<button type="button" className="deck-dl-btn" onClick={downloadDeck} disabled={busy}>
<svg viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4" /><polyline points="7 10 12 15 17 10" /><line x1="12" y1="15" x2="12" y2="3" /></svg>
{busy ? 'Preparing…' : 'Download Deck (PPTX)'}
</button>
</div>
<div className="slide-viewer">
<div className="slide-display">
<button className="slide-nav-btn prev" onClick={() => go(cur - 1)} disabled={cur === 0} aria-label="Previous slide">&#8249;</button>
<img src={slides[cur]} alt={`Slide ${cur + 1}`} />
<button className="slide-nav-btn next" onClick={() => go(cur + 1)} disabled={cur === SLIDE_COUNT - 1} aria-label="Next slide">&#8250;</button>
</div>
<div className="slide-controls">
<div className="slide-counter">{cur + 1} / {SLIDE_COUNT}</div>
<div className="slide-thumbs" ref={thumbsRef}>
{slides.map((src, i) => (
<button key={src} className={`slide-thumb-btn${i === cur ? ' active' : ''}`} onClick={() => go(i)} title={`Slide ${i + 1}`}>
<img src={src} alt={`Slide ${i + 1}`} loading="lazy" />
</button>
))}
</div>
</div>
</div>
</div>
);
}