-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewer.tsx
More file actions
474 lines (435 loc) · 14.9 KB
/
Viewer.tsx
File metadata and controls
474 lines (435 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
'use client';
import Link from 'next/link';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { SlideFrame } from './SlideFrame';
import { StackdeckMark } from './StackdeckMark';
import { Present } from './Present';
import { generateDeckPdf, type ProgressEvent } from '@/lib/generate-pdf';
import './SlideFrame.css';
import './Viewer.css';
const CONTACT_EMAIL = 'ankur@octifytechnologies.com';
const SENDER_NAME = 'Ankur';
type SlideRef = { file: string; title?: string };
type Props = {
slug: string;
title: string;
client?: string;
slides: SlideRef[];
};
export function Viewer({ slug, title, client, slides }: Props) {
const [index, setIndex] = useState(0);
const [presenting, setPresenting] = useState(false);
const [pdfStatus, setPdfStatus] = useState<ProgressEvent | null>(null);
// Personalization, read from `?to=<company>` so links sent out can be
// tailored without changing app code or storing per-recipient state.
const [recipient, setRecipient] = useState<string | null>(null);
const total = slides.length;
const current = slides[index];
// Read `?to=` once on mount.
useEffect(() => {
const to = new URLSearchParams(window.location.search).get('to');
if (to && to.length <= 80) setRecipient(to);
}, []);
// URL hash <-> slide index. On mount we honor an incoming `#3`; on every
// subsequent navigation we write back via replaceState. The prevIndexRef
// skips the first commit so we never clobber the incoming hash.
const hashPrevIndex = useRef<number | null>(null);
useEffect(() => {
const m = window.location.hash.match(/^#(\d{1,3})$/);
if (m) {
const n = Math.max(1, Math.min(parseInt(m[1], 10), total)) - 1;
setIndex(n);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (hashPrevIndex.current === null) {
hashPrevIndex.current = index;
return;
}
if (hashPrevIndex.current === index) return;
hashPrevIndex.current = index;
const target = `#${index + 1}`;
if (window.location.hash !== target) {
window.history.replaceState(
null,
'',
`${window.location.pathname}${window.location.search}${target}`,
);
}
}, [index]);
const mailtoHref = useMemo(() => {
const subjectBits = ['Re:', title];
if (client) subjectBits.push(`(${client})`);
const subject = subjectBits.join(' ');
const greeting = `Hi ${SENDER_NAME},`;
const body = recipient
? `${greeting}\n\nThanks for sending the ${title} deck. A few thoughts from ${recipient}:\n\n`
: `${greeting}\n\nThanks for sending the ${title} deck. A few thoughts:\n\n`;
return `mailto:${CONTACT_EMAIL}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
}, [title, client, recipient]);
const pdfFilename = useMemo(() => {
const safe = title.replace(/[\\/:*?"<>|]+/g, '').trim();
return `${safe}.pdf`;
}, [title]);
const downloadPdf = useCallback(async () => {
if (pdfStatus && pdfStatus.phase !== 'done') return;
try {
await generateDeckPdf({
slug,
filename: pdfFilename,
slides,
onProgress: setPdfStatus,
});
} catch (err) {
console.error('PDF generation failed', err);
setPdfStatus(null);
alert('Could not generate the PDF. Please try again.');
return;
}
setTimeout(() => setPdfStatus(null), 1200);
}, [slug, slides, pdfStatus, pdfFilename]);
const pdfBusy = pdfStatus !== null && pdfStatus.phase !== 'done';
const pdfLabel = (() => {
if (!pdfStatus) return 'Download PDF';
switch (pdfStatus.phase) {
case 'preparing':
return 'Preparing…';
case 'rendering':
return `Rendering ${pdfStatus.current} / ${pdfStatus.total}`;
case 'composing':
return 'Composing PDF…';
case 'done':
return 'Saved';
}
})();
const slideUrl = useCallback((file: string) => `/c/${slug}/slides/${file}`, [slug]);
const next = useCallback(() => setIndex((i) => Math.min(i + 1, total - 1)), [total]);
const prev = useCallback(() => setIndex((i) => Math.max(i - 1, 0)), []);
const enterPresent = useCallback(() => setPresenting(true), []);
const exitPresent = useCallback(() => setPresenting(false), []);
// Keep the active sidebar thumb in view as the user navigates. block:
// 'nearest' = no-op when already visible, minimal scroll otherwise. The
// scroll-margin set in CSS leaves room for the sticky CONTENTS header.
useEffect(() => {
const active = document.querySelector('.vstrip [aria-current="true"]') as HTMLElement | null;
active?.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}, [index]);
useEffect(() => {
if (presenting) return; // Present component owns its own keys
function onKey(e: KeyboardEvent) {
if (e.target instanceof HTMLInputElement) return;
if (e.key === 'ArrowRight' || e.key === 'PageDown' || e.key === ' ') {
e.preventDefault();
next();
} else if (e.key === 'ArrowLeft' || e.key === 'PageUp') {
e.preventDefault();
prev();
} else if (e.key === 'Home') {
setIndex(0);
} else if (e.key === 'End') {
setIndex(total - 1);
} else if (e.key === 'f' || e.key === 'F') {
enterPresent();
} else if (e.key >= '1' && e.key <= '9') {
const n = Number(e.key) - 1;
if (n < total) setIndex(n);
}
}
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [next, prev, total, enterPresent, presenting]);
const progress = (index + 1) / total;
return (
<div className="viewer">
<header className="vbar">
<div className="vbar-left">
<Link href="/" className="vbar-brand" aria-label="stackdeck">
<StackdeckMark size={20} />
</Link>
<span className="vbar-sep" aria-hidden />
<Link href="/" className="vbar-crumb vbar-crumb-link">
<ChevronLeft />
<span>All decks</span>
</Link>
<span className="vbar-sep" aria-hidden />
<div className="vbar-crumb-trail" title={title}>
{client ? <span className="vbar-crumb-client">{client}</span> : null}
{client ? <ChevronRight muted /> : null}
<span className="vbar-crumb-current">{title}</span>
{recipient ? (
<span className="vbar-recipient" title={`Personalized for ${recipient}`}>
<span className="vbar-recipient-dot" aria-hidden />
<span className="vbar-recipient-text">for {recipient}</span>
</span>
) : null}
</div>
</div>
<div className="vbar-right">
<button
type="button"
onClick={downloadPdf}
disabled={pdfBusy}
className={`vbar-download ${pdfStatus ? 'vbar-download-active' : ''}`}
title={pdfLabel}
aria-label={pdfLabel}
aria-busy={pdfBusy}
>
{pdfBusy ? <Spinner /> : <DownloadIcon />}
<span className="vbar-download-text">{pdfLabel}</span>
</button>
<button type="button" onClick={enterPresent} className="vbar-cta" title="Present (F)">
<PlayIcon />
<span>Present</span>
<kbd className="vbar-cta-key">F</kbd>
</button>
</div>
</header>
<div className="vprogress" aria-hidden>
<div className="vprogress-fill" style={{ transform: `scaleX(${progress})` }} />
</div>
<main className="vstage">
<button
type="button"
className="vstage-frame"
onClick={() => {
if (window.matchMedia('(max-width: 900px)').matches) enterPresent();
}}
aria-label="Open slide fullscreen"
>
<SlideFrame
src={slideUrl(current.file)}
title={current.title ?? `Slide ${index + 1}`}
showLoader
/>
<span className="vstage-tap-hint">
<ExpandIcon />
<span>Tap to enlarge</span>
</span>
</button>
<div className="vstage-caption">
<div className="vstage-pager" role="group" aria-label="Slide navigation">
<button
type="button"
className="vstage-pager-btn"
onClick={prev}
disabled={index === 0}
aria-label="Previous slide"
title="Previous (←)"
>
<svg width="14" height="14" viewBox="0 0 12 12" fill="none">
<path
d="M8 2L4 6l4 4"
stroke="currentColor"
strokeWidth="1.7"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
<span className="vstage-pager-counter">
<span className="vstage-pager-counter-current">
{String(index + 1).padStart(2, '0')}
</span>
<span className="vstage-pager-counter-sep">/</span>
<span className="vstage-pager-counter-total">{String(total).padStart(2, '0')}</span>
</span>
<button
type="button"
className="vstage-pager-btn"
onClick={next}
disabled={index === total - 1}
aria-label="Next slide"
title="Next (→)"
>
<svg width="14" height="14" viewBox="0 0 12 12" fill="none">
<path
d="M4 2l4 4-4 4"
stroke="currentColor"
strokeWidth="1.7"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
</div>
{current.title ? <span className="vstage-caption-title">{current.title}</span> : null}
{index === total - 1 ? (
<a href={mailtoHref} className="vstage-cta">
<span>Like what you see?</span>
<span className="vstage-cta-action">Get in touch</span>
<svg width="12" height="10" viewBox="0 0 14 10" fill="none">
<path
d="M1 5h12m0 0L9 1m4 4L9 9"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</a>
) : null}
</div>
{/* Persistent corner CTA, visible on every slide so a prospect who
bails partway through always has a path back. Hidden on the last
slide where the inline caption CTA already covers it. */}
{index !== total - 1 ? (
<a
href={mailtoHref}
className="vstage-corner-cta"
title={`Email ${SENDER_NAME} at ${CONTACT_EMAIL}`}
>
<svg width="12" height="12" viewBox="0 0 14 14" fill="none" aria-hidden>
<path
d="M2 4l5 4 5-4M2 4v6h10V4M2 4h10"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
<span>Talk to {SENDER_NAME}</span>
</a>
) : null}
</main>
<aside className="vstrip" aria-label="Slides">
<div className="vstrip-head">
<span className="vstrip-head-label">Contents</span>
<span className="vstrip-head-count">
{String(index + 1).padStart(2, '0')}/{String(total).padStart(2, '0')}
</span>
</div>
<div className="vstrip-list">
{slides.map((s, i) => (
<button
key={s.file}
type="button"
className={`thumb ${i === index ? 'thumb-active' : ''}`}
onClick={() => setIndex(i)}
aria-label={`Go to slide ${i + 1}${s.title ? `: ${s.title}` : ''}`}
aria-current={i === index}
>
<div className="thumb-frame">
<SlideFrame src={slideUrl(s.file)} title={s.title} lazy interactive={false} />
<span className="thumb-num" aria-hidden>
{String(i + 1).padStart(2, '0')}
</span>
{i === index ? (
<span className="thumb-badge" aria-hidden>
<PlayingDot />
</span>
) : null}
</div>
<span className="thumb-title">{s.title || `Slide ${i + 1}`}</span>
</button>
))}
</div>
</aside>
{presenting ? (
<Present
slug={slug}
title={title}
slides={slides}
initialIndex={index}
onIndexChange={setIndex}
onExit={exitPresent}
/>
) : null}
</div>
);
}
function ChevronLeft() {
return (
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden>
<path
d="M8 2L4 6l4 4"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function ChevronRight({ muted }: { muted?: boolean }) {
return (
<svg
width="10"
height="10"
viewBox="0 0 12 12"
fill="none"
aria-hidden
style={{ opacity: muted ? 0.45 : 1 }}
>
<path
d="M4 2l4 4-4 4"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function PlayIcon() {
return (
<svg width="11" height="11" viewBox="0 0 12 12" fill="none" aria-hidden>
<path d="M3 2v8l7-4-7-4z" fill="currentColor" />
</svg>
);
}
function DownloadIcon() {
return (
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden>
<path
d="M7 1.5v7M4 6l3 3 3-3M2 11.5h10"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function PlayingDot() {
return (
<span className="playing-dot">
<span />
<span />
<span />
</span>
);
}
function ExpandIcon() {
return (
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden>
<path
d="M2 5.5V2h3.5M12 8.5V12H8.5M2 8.5V12h3.5M12 5.5V2H8.5"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function Spinner() {
return (
<svg
width="14"
height="14"
viewBox="0 0 14 14"
fill="none"
aria-hidden
className="vbar-spinner"
>
<circle cx="7" cy="7" r="5.5" stroke="currentColor" strokeWidth="1.4" opacity="0.22" />
<path
d="M12.5 7a5.5 5.5 0 0 0-5.5-5.5"
stroke="currentColor"
strokeWidth="1.4"
strokeLinecap="round"
/>
</svg>
);
}