The screen shows a code on its setup and with its controls. Anyone with it can step, pause and jump through the slides, send the screen back to its setup, or change the setup and play it there. Server-sent events, held in memory (routes/present.js); X-Accel-Buffering: no, so nginx needs no block of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
10 KiB
TypeScript
250 lines
10 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
PRESENT REMOTE: /remote and /remote/:code
|
|
|
|
A phone driving a presenting screen (EventPresent.tsx) by the
|
|
code the screen shows (server/src/routes/present.js): what's on
|
|
it now, back, pause and next, any slide by name, back to its
|
|
setup, and the setup itself (EventPresent's Setup), whose Play
|
|
starts the screen with those choices. Anyone with the code can.
|
|
Outside the site's Layout, like the presentation.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { ArrowLeft, CaretLeft, CaretRight, Gear, Pause, Play, SlidersHorizontal } from '@phosphor-icons/react'
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
import { useNavigate, useParams } from 'react-router-dom'
|
|
|
|
import { Setup, addressFor, readSettings, schedulesOf, sectionsOf } from './EventPresent.tsx'
|
|
import { BASE, post } from '../lib/api.ts'
|
|
import { useEvent } from '../lib/useContent.ts'
|
|
import { BRAND, accentText } from '../lib/color.ts'
|
|
import { documentTitle } from '../lib/site.ts'
|
|
import type { RemoteCommand, ScreenState } from '../lib/useScreenRemote.ts'
|
|
|
|
type Seen = ScreenState & { connected: boolean }
|
|
|
|
export default function PresentRemote() {
|
|
const { code } = useParams()
|
|
|
|
useEffect(() => {
|
|
document.title = documentTitle('Remote')
|
|
}, [])
|
|
|
|
return code ? <Connected code={code.toUpperCase()} /> : <EnterCode />
|
|
}
|
|
|
|
function EnterCode() {
|
|
const navigate = useNavigate()
|
|
const [code, setCode] = useState('')
|
|
return (
|
|
<main className="flex min-h-screen items-center justify-center bg-surface px-4 text-body">
|
|
<form
|
|
onSubmit={(e) => {
|
|
e.preventDefault()
|
|
if (code.trim()) navigate(`/remote/${encodeURIComponent(code.trim().toUpperCase())}`)
|
|
}}
|
|
className="w-full max-w-sm space-y-4 bg-raised p-6 shadow-md"
|
|
>
|
|
<h1 className="text-2xl font-bold text-ink">Remote</h1>
|
|
<label className="block">
|
|
<span className="text-sm text-muted">The code on the screen (with its controls, or on its setup)</span>
|
|
<input
|
|
value={code}
|
|
onChange={(e) => setCode(e.target.value)}
|
|
autoFocus
|
|
autoCapitalize="characters"
|
|
autoComplete="off"
|
|
maxLength={6}
|
|
className="mt-1 w-full border border-muted/25 bg-surface px-3 py-2 font-mono text-2xl tracking-widest uppercase"
|
|
/>
|
|
</label>
|
|
<button type="submit" className="btn w-full bg-action px-5 py-3 text-white">
|
|
Connect
|
|
</button>
|
|
</form>
|
|
</main>
|
|
)
|
|
}
|
|
|
|
function Connected({ code }: { code: string }) {
|
|
const [seen, setSeen] = useState<Seen | null>(null)
|
|
const [lost, setLost] = useState(false)
|
|
const [tries, setTries] = useState(0)
|
|
const [problem, setProblem] = useState('')
|
|
const [editing, setEditing] = useState(false)
|
|
|
|
useEffect(() => {
|
|
setLost(false)
|
|
const source = new EventSource(`${BASE}/present/${encodeURIComponent(code)}`)
|
|
source.addEventListener('state', (e) => setSeen(JSON.parse((e as MessageEvent).data)))
|
|
// Closed is the server's no (no such code); otherwise it retries.
|
|
source.onerror = () => source.readyState === EventSource.CLOSED && setLost(true)
|
|
return () => source.close()
|
|
}, [code, tries])
|
|
|
|
const { data: event } = useEvent(seen?.event || undefined)
|
|
const sections = useMemo(() => (event ? sectionsOf(event.blocks ?? [], event.title) : []), [event])
|
|
const schedules = useMemo(() => schedulesOf(event?.blocks ?? []), [event])
|
|
|
|
const send = (command: RemoteCommand) => {
|
|
setProblem('')
|
|
post(`/present/${encodeURIComponent(code)}/command`, command).catch((err) => setProblem(err.message))
|
|
}
|
|
|
|
if (lost) {
|
|
return (
|
|
<main className="flex min-h-screen flex-col items-center justify-center gap-4 bg-surface px-4 text-body">
|
|
<p className="notice">No screen has the code {code} right now.</p>
|
|
<div className="flex gap-3">
|
|
<button type="button" onClick={() => setTries((n) => n + 1)} className="btn border border-muted/25 px-5 py-2 text-ink">
|
|
Try again
|
|
</button>
|
|
<a href="/remote" className="btn border border-muted/25 px-5 py-2 text-ink">
|
|
Another code
|
|
</a>
|
|
</div>
|
|
</main>
|
|
)
|
|
}
|
|
|
|
const accent = event?.present?.accent || event?.color || BRAND
|
|
|
|
if (editing && seen?.event && event) {
|
|
return (
|
|
<>
|
|
<div className="sticky top-0 z-10 flex items-center gap-3 bg-raised px-4 py-3 shadow-md">
|
|
<button type="button" onClick={() => setEditing(false)} className="btn inline-flex items-center gap-2 px-3 py-2 text-ink">
|
|
<ArrowLeft aria-hidden="true" weight="bold" className="h-4 w-4" />
|
|
Remote
|
|
</button>
|
|
<span className="text-sm text-muted">Play starts the screen with these choices.</span>
|
|
</div>
|
|
<Setup
|
|
// Fresh choices each time the screen's setup changes.
|
|
key={seen.query}
|
|
title={event.title}
|
|
back={`/events/${encodeURIComponent(event.id)}`}
|
|
accent={accent}
|
|
sections={sections}
|
|
schedules={schedules}
|
|
settings={readSettings(new URLSearchParams(seen.query))}
|
|
onPlay={(next) => {
|
|
send({ do: 'start', query: addressFor(next).toString() })
|
|
setEditing(false)
|
|
}}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
const control = 'btn flex h-16 flex-1 items-center justify-center bg-raised text-ink shadow-md disabled:opacity-50'
|
|
const playing = !!seen?.event && seen.playing
|
|
const off = !seen?.connected
|
|
return (
|
|
<main className="min-h-screen bg-surface text-body">
|
|
<div className="h-2" style={{ background: accent }} />
|
|
<div className="mx-auto max-w-lg space-y-6 px-4 py-6">
|
|
<header>
|
|
<p className="text-sm font-semibold text-muted">
|
|
Remote <span className="font-mono tracking-widest">{code}</span>
|
|
</p>
|
|
<h1 className="text-2xl font-bold" style={{ color: accentText(accent) }}>
|
|
{seen?.event ? seen.title : 'Connecting…'}
|
|
</h1>
|
|
</header>
|
|
|
|
{seen && off && <p className="notice">The screen isn't connected right now. This page carries on when it is.</p>}
|
|
|
|
{seen?.event && (
|
|
<p className="bg-raised p-4 shadow-sm" aria-live="polite">
|
|
{playing ? (
|
|
<>
|
|
<span className="block text-sm text-muted">
|
|
Slide {seen.index + 1} of {seen.slides.length}
|
|
{seen.paused ? ', paused' : ''}
|
|
</span>
|
|
<span className="font-semibold text-ink">{seen.slides[seen.index]}</span>
|
|
</>
|
|
) : (
|
|
<span className="text-muted">The screen is on its setup. Change the setup to pick what it plays.</span>
|
|
)}
|
|
</p>
|
|
)}
|
|
|
|
{seen && playing && (
|
|
<div className="flex gap-3">
|
|
<button type="button" disabled={off} onClick={() => send({ do: 'prev' })} className={control} aria-label="Previous slide">
|
|
<CaretLeft weight="bold" className="h-6 w-6" aria-hidden="true" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={off}
|
|
onClick={() => send({ do: seen.paused ? 'resume' : 'pause' })}
|
|
className={control}
|
|
aria-label={seen.paused ? 'Play' : 'Pause'}
|
|
>
|
|
{seen.paused ? (
|
|
<Play weight="fill" className="h-6 w-6" aria-hidden="true" />
|
|
) : (
|
|
<Pause weight="bold" className="h-6 w-6" aria-hidden="true" />
|
|
)}
|
|
</button>
|
|
<button type="button" disabled={off} onClick={() => send({ do: 'next' })} className={control} aria-label="Next slide">
|
|
<CaretRight weight="bold" className="h-6 w-6" aria-hidden="true" />
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{seen?.event && (
|
|
<div className="flex flex-wrap gap-3">
|
|
<button
|
|
type="button"
|
|
disabled={off || !event}
|
|
onClick={() => setEditing(true)}
|
|
className="btn inline-flex items-center gap-2 border border-muted/25 px-4 py-2 text-ink disabled:opacity-50"
|
|
>
|
|
<SlidersHorizontal aria-hidden="true" weight="bold" className="h-4 w-4" />
|
|
Change setup
|
|
</button>
|
|
{playing && (
|
|
<button
|
|
type="button"
|
|
disabled={off}
|
|
onClick={() => send({ do: 'setup' })}
|
|
className="btn inline-flex items-center gap-2 border border-muted/25 px-4 py-2 text-ink disabled:opacity-50"
|
|
>
|
|
<Gear aria-hidden="true" weight="bold" className="h-4 w-4" />
|
|
Screen to its setup
|
|
</button>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
<p role="status" className="text-sm text-muted">
|
|
{problem}
|
|
</p>
|
|
|
|
{seen && playing && seen.slides.length > 0 && (
|
|
<section>
|
|
<h2 className="mb-2 text-lg font-bold text-ink">Go to a slide</h2>
|
|
<ol className="divide-y divide-muted/15 bg-raised shadow-sm">
|
|
{seen.slides.map((name, i) => (
|
|
<li key={i}>
|
|
<button
|
|
type="button"
|
|
disabled={off}
|
|
onClick={() => send({ do: 'go', index: i })}
|
|
aria-current={i === seen.index ? 'true' : undefined}
|
|
className={`flex w-full gap-3 px-4 py-3 text-left hover:bg-tint ${i === seen.index ? 'bg-tint font-semibold text-ink' : ''}`}
|
|
>
|
|
<span className="w-6 shrink-0 text-right text-muted">{i + 1}</span>
|
|
<span>{name}</span>
|
|
</button>
|
|
</li>
|
|
))}
|
|
</ol>
|
|
</section>
|
|
)}
|
|
</div>
|
|
</main>
|
|
)
|
|
}
|