|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useRouter } from 'next/navigation'; |
| 4 | +import { useEffect, useMemo, useRef, useState } from 'react'; |
| 5 | + |
| 6 | +import { ParseError, parseDeck } from '@/ir/parse'; |
| 7 | +import { planDeck } from '@/ir/plan'; |
| 8 | +import type { Deck } from '@/ir/schema'; |
| 9 | +import { DeckRenderer } from '@/render/DeckRenderer'; |
| 10 | +import { getDeck } from '@/storage/deck-store'; |
| 11 | + |
| 12 | +type Props = { deckId: string }; |
| 13 | + |
| 14 | +export function PresentMode({ deckId }: Props) { |
| 15 | + const router = useRouter(); |
| 16 | + const [deck, setDeck] = useState<Deck | null>(null); |
| 17 | + const [error, setError] = useState<string | null>(null); |
| 18 | + const [index, setIndex] = useState(0); |
| 19 | + const stageRef = useRef<HTMLDivElement>(null); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + let cancelled = false; |
| 23 | + getDeck(deckId).then((stored) => { |
| 24 | + if (cancelled) return; |
| 25 | + if (!stored) { |
| 26 | + router.replace('/'); |
| 27 | + return; |
| 28 | + } |
| 29 | + try { |
| 30 | + const parsed = parseDeck(stored.source, { theme: stored.theme, brand: stored.brand }); |
| 31 | + const planned = planDeck(parsed); |
| 32 | + setDeck(planned); |
| 33 | + } catch (e) { |
| 34 | + const message = e instanceof ParseError ? e.message : (e as Error).message; |
| 35 | + setError(message); |
| 36 | + } |
| 37 | + }); |
| 38 | + return () => { |
| 39 | + cancelled = true; |
| 40 | + }; |
| 41 | + }, [deckId, router]); |
| 42 | + |
| 43 | + const total = deck?.slides.length ?? 0; |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + const onKey = (e: KeyboardEvent) => { |
| 47 | + if (e.key === 'ArrowRight' || e.key === 'PageDown' || e.key === ' ') { |
| 48 | + e.preventDefault(); |
| 49 | + setIndex((i) => Math.min(total - 1, i + 1)); |
| 50 | + } else if (e.key === 'ArrowLeft' || e.key === 'PageUp') { |
| 51 | + e.preventDefault(); |
| 52 | + setIndex((i) => Math.max(0, i - 1)); |
| 53 | + } else if (e.key === 'Home') { |
| 54 | + setIndex(0); |
| 55 | + } else if (e.key === 'End') { |
| 56 | + setIndex(total - 1); |
| 57 | + } else if (e.key === 'Escape') { |
| 58 | + router.push(`/d/${deckId}/edit`); |
| 59 | + } else if (/^[0-9]$/.test(e.key)) { |
| 60 | + const n = Number(e.key); |
| 61 | + if (n >= 1 && n <= total) setIndex(n - 1); |
| 62 | + } |
| 63 | + }; |
| 64 | + window.addEventListener('keydown', onKey); |
| 65 | + return () => window.removeEventListener('keydown', onKey); |
| 66 | + }, [total, router, deckId]); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + const el = stageRef.current; |
| 70 | + if (!el) return; |
| 71 | + if (!document.fullscreenElement) { |
| 72 | + void el.requestFullscreen?.().catch(() => {}); |
| 73 | + } |
| 74 | + return () => { |
| 75 | + if (document.fullscreenElement) { |
| 76 | + void document.exitFullscreen?.().catch(() => {}); |
| 77 | + } |
| 78 | + }; |
| 79 | + }, []); |
| 80 | + |
| 81 | + const visibleDeck = useMemo<Deck | null>(() => { |
| 82 | + if (!deck) return null; |
| 83 | + const slide = deck.slides[index]; |
| 84 | + if (!slide) return deck; |
| 85 | + return { ...deck, slides: [slide] }; |
| 86 | + }, [deck, index]); |
| 87 | + |
| 88 | + if (error) { |
| 89 | + return ( |
| 90 | + <div className="present-error"> |
| 91 | + <strong>Could not present this deck</strong> |
| 92 | + <pre>{error}</pre> |
| 93 | + <button type="button" onClick={() => router.push(`/d/${deckId}/edit`)}> |
| 94 | + Back to editor |
| 95 | + </button> |
| 96 | + </div> |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + if (!deck || !visibleDeck) { |
| 101 | + return ( |
| 102 | + <div className="present-loading"> |
| 103 | + <span>Loading…</span> |
| 104 | + </div> |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + return ( |
| 109 | + <div className="present-stage" ref={stageRef}> |
| 110 | + <div className="present-frame"> |
| 111 | + <DeckRenderer deck={visibleDeck} /> |
| 112 | + </div> |
| 113 | + <div className="present-hud" aria-hidden="true"> |
| 114 | + <span className="present-hud__page"> |
| 115 | + {String(index + 1).padStart(2, '0')} / {String(total).padStart(2, '0')} |
| 116 | + </span> |
| 117 | + </div> |
| 118 | + <button |
| 119 | + type="button" |
| 120 | + className="present-nav present-nav--prev" |
| 121 | + aria-label="Previous slide" |
| 122 | + onClick={() => setIndex((i) => Math.max(0, i - 1))} |
| 123 | + disabled={index === 0} |
| 124 | + > |
| 125 | + ‹ |
| 126 | + </button> |
| 127 | + <button |
| 128 | + type="button" |
| 129 | + className="present-nav present-nav--next" |
| 130 | + aria-label="Next slide" |
| 131 | + onClick={() => setIndex((i) => Math.min(total - 1, i + 1))} |
| 132 | + disabled={index === total - 1} |
| 133 | + > |
| 134 | + › |
| 135 | + </button> |
| 136 | + </div> |
| 137 | + ); |
| 138 | +} |
0 commit comments