Support script and URL sources, automatic Sessionize polling, staff-linked service sessions, chronological agenda blocks, and inline Placeholders. Fix demo mode to use the active event date and document deployed behavior with regression coverage.
608 lines
22 KiB
TypeScript
608 lines
22 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import {
|
||
activateEvent,
|
||
deactivateEvent,
|
||
fetchPretixOptions,
|
||
getAllEvents,
|
||
runPretixSync,
|
||
runSessionizeSync,
|
||
upsertEvent,
|
||
} from '../lib/api';
|
||
import type { Event, PretixOptions, PretixSyncResult, SyncResult } from '../lib/types';
|
||
import { useToast } from '../context/ToastContext';
|
||
import { Empty, Modal, Spinner, Toggle } from '../components/ui';
|
||
import { normalizeSessionizeSource } from '../lib/sessionizeSource';
|
||
|
||
export function Events() {
|
||
const { push } = useToast();
|
||
const [events, setEvents] = useState<Event[]>([]);
|
||
const [loaded, setLoaded] = useState(false);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [modal, setModal] = useState<{ event: Event | null } | null>(null);
|
||
const [syncingId, setSyncingId] = useState<string | null>(null);
|
||
const [syncResult, setSyncResult] = useState<SyncResult | null>(null);
|
||
const [pretixSyncingId, setPretixSyncingId] = useState<string | null>(null);
|
||
const [pretixResult, setPretixResult] = useState<PretixSyncResult | null>(null);
|
||
|
||
async function load() {
|
||
try {
|
||
setEvents(await getAllEvents());
|
||
setLoaded(true);
|
||
} catch (e) {
|
||
setError(e instanceof Error ? e.message : 'Failed to load');
|
||
}
|
||
}
|
||
|
||
useEffect(() => {
|
||
load();
|
||
}, []);
|
||
|
||
async function toggleActive(ev: Event, active: boolean) {
|
||
if (active && !confirm(`Set "${ev.name}" as the active event? All other events will be deactivated.`)) return;
|
||
try {
|
||
if (active) await activateEvent(ev.id);
|
||
else await deactivateEvent(ev.id);
|
||
await load();
|
||
push('success', active ? `${ev.name} is now active` : `${ev.name} deactivated`);
|
||
} catch (e) {
|
||
push('error', e instanceof Error ? e.message : 'Failed to update event');
|
||
}
|
||
}
|
||
|
||
async function runSync(ev: Event) {
|
||
if (!ev.sessionize_endpoint) {
|
||
push('error', `Set a Sessionize endpoint for ${ev.name} first (Edit)`);
|
||
return;
|
||
}
|
||
setSyncingId(ev.id);
|
||
try {
|
||
const result = await runSessionizeSync(ev.id);
|
||
setSyncResult(result);
|
||
await load();
|
||
push('success', `Synced ${ev.name}`);
|
||
} catch (e) {
|
||
push('error', e instanceof Error ? e.message : 'Sync failed');
|
||
} finally {
|
||
setSyncingId(null);
|
||
}
|
||
}
|
||
|
||
async function runPretix(ev: Event) {
|
||
if (!ev.pretix_organizer || !ev.pretix_event_slug || !ev.pretix_api_key) {
|
||
push('error', `Set the Pretix API key + organizer + event slug for ${ev.name} first (Edit)`);
|
||
return;
|
||
}
|
||
setPretixSyncingId(ev.id);
|
||
try {
|
||
const result = await runPretixSync(ev.id);
|
||
setPretixResult(result);
|
||
await load();
|
||
push(
|
||
'success',
|
||
result.status === 'success'
|
||
? `Pretix synced ${ev.name} (${result.attendees ?? 0} attendees)`
|
||
: `Pretix ${result.status} for ${ev.name}`,
|
||
);
|
||
} catch (e) {
|
||
push('error', e instanceof Error ? e.message : 'Pretix sync failed');
|
||
} finally {
|
||
setPretixSyncingId(null);
|
||
}
|
||
}
|
||
|
||
if (error) return <Empty icon="⚠️" text={error} />;
|
||
if (!loaded)
|
||
return (
|
||
<div className="loading-screen">
|
||
<Spinner />
|
||
</div>
|
||
);
|
||
|
||
return (
|
||
<div>
|
||
<div className="page-head">
|
||
<div>
|
||
<h1>Events</h1>
|
||
<div className="sub">
|
||
{events.length} event{events.length === 1 ? '' : 's'} · only one can be active at a time.
|
||
</div>
|
||
</div>
|
||
<button className="btn primary" onClick={() => setModal({ event: null })}>
|
||
+ New event
|
||
</button>
|
||
</div>
|
||
|
||
<div className="card">
|
||
{events.length === 0 ? (
|
||
<Empty icon="📅" text="No events yet. Create one to get started." />
|
||
) : (
|
||
<div className="table-wrap">
|
||
<table className="data">
|
||
<thead>
|
||
<tr>
|
||
<th>Event</th>
|
||
<th>Dates</th>
|
||
<th>Location</th>
|
||
<th>Stats</th>
|
||
<th>Status</th>
|
||
<th></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{events.map((ev) => (
|
||
<tr key={ev.id}>
|
||
<td>
|
||
<div style={{ fontWeight: 700 }}>{ev.name}</div>
|
||
{ev.is_active && <span className="pill green">active</span>}
|
||
</td>
|
||
<td className="muted">
|
||
{fmtDate(ev.start_date)} – {fmtDate(ev.end_date)}
|
||
</td>
|
||
<td className="muted">{ev.location || '—'}</td>
|
||
<td className="muted small">
|
||
{ev.num_registrations} reg · {ev.num_sessions} sessions · {ev.num_speakers} speakers
|
||
{ev.avg_feedback != null && ` · ⭐ ${ev.avg_feedback.toFixed(1)}`}
|
||
</td>
|
||
<td>
|
||
<Toggle
|
||
checked={ev.is_active}
|
||
onChange={(v) => toggleActive(ev, v)}
|
||
label={ev.is_active ? 'Active' : 'Inactive'}
|
||
/>
|
||
</td>
|
||
<td>
|
||
<div style={{ display: 'flex', gap: 8, justifyContent: 'flex-end' }}>
|
||
<button
|
||
className="btn small"
|
||
disabled={syncingId === ev.id}
|
||
onClick={() => runSync(ev)}
|
||
title={
|
||
ev.sessionize_endpoint
|
||
? 'Sync with Sessionize'
|
||
: 'Set a Sessionize endpoint first (Edit)'
|
||
}
|
||
>
|
||
{syncingId === ev.id ? 'Syncing…' : 'Sync'}
|
||
</button>
|
||
<button
|
||
className="btn small"
|
||
disabled={
|
||
pretixSyncingId === ev.id ||
|
||
!ev.pretix_organizer ||
|
||
!ev.pretix_event_slug ||
|
||
!ev.pretix_api_key
|
||
}
|
||
onClick={() => runPretix(ev)}
|
||
title={
|
||
ev.pretix_organizer && ev.pretix_event_slug && ev.pretix_api_key
|
||
? 'Sync with Pretix'
|
||
: 'Set the Pretix API key + organizer + event slug first (Edit)'
|
||
}
|
||
>
|
||
{pretixSyncingId === ev.id ? 'Syncing…' : 'Sync Pretix'}
|
||
</button>
|
||
<button className="btn small" onClick={() => setModal({ event: ev })}>
|
||
Edit
|
||
</button>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{modal && (
|
||
<EventModal
|
||
event={modal.event}
|
||
onClose={() => setModal(null)}
|
||
onSaved={async (msg) => {
|
||
setModal(null);
|
||
await load();
|
||
push('success', msg);
|
||
}}
|
||
/>
|
||
)}
|
||
|
||
{syncResult && <SyncResultModal result={syncResult} onClose={() => setSyncResult(null)} />}
|
||
{pretixResult && <PretixResultModal result={pretixResult} onClose={() => setPretixResult(null)} />}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function PretixResultModal({ result, onClose }: { result: PretixSyncResult; onClose: () => void }) {
|
||
const s = result.summary;
|
||
return (
|
||
<Modal
|
||
open
|
||
title={`Pretix sync — ${result.event_name ?? result.event_id}`}
|
||
onClose={onClose}
|
||
footer={
|
||
<button className="btn primary" onClick={onClose}>
|
||
Close
|
||
</button>
|
||
}
|
||
>
|
||
{result.status === 'error' || result.status === 'skipped' ? (
|
||
<div className="pill red" style={{ display: 'inline-block', padding: '8px 12px' }}>
|
||
{result.error ?? result.reason ?? 'Pretix sync failed'}
|
||
</div>
|
||
) : (
|
||
<>
|
||
<div className="stat-grid">
|
||
<div className="stat">
|
||
<div className="val">{result.orders ?? 0}</div>
|
||
<div className="lbl">Orders</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{result.attendees ?? 0}</div>
|
||
<div className="lbl">Attendees</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s?.created ?? 0}</div>
|
||
<div className="lbl">Created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s?.updated ?? 0}</div>
|
||
<div className="lbl">Updated</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{result.skipped ?? 0}</div>
|
||
<div className="lbl">Skipped</div>
|
||
</div>
|
||
</div>
|
||
{s && s.errors.length > 0 && (
|
||
<div style={{ marginTop: 12 }}>
|
||
<div className="pill red" style={{ display: 'inline-block', marginBottom: 8 }}>
|
||
{s.errors.length} error(s)
|
||
</div>
|
||
<ul style={{ margin: 0, paddingLeft: 18 }}>
|
||
{s.errors.map((e, i) => (
|
||
<li key={i} className="small">
|
||
{e}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</div>
|
||
)}
|
||
</>
|
||
)}
|
||
</Modal>
|
||
);
|
||
}
|
||
|
||
function EventModal({
|
||
event,
|
||
onClose,
|
||
onSaved,
|
||
}: {
|
||
event: Event | null;
|
||
onClose: () => void;
|
||
onSaved: (msg: string) => void;
|
||
}) {
|
||
const [name, setName] = useState(event?.name ?? '');
|
||
const [startDate, setStartDate] = useState(event ? event.start_date.slice(0, 10) : '');
|
||
const [endDate, setEndDate] = useState(event ? event.end_date.slice(0, 10) : '');
|
||
const [location, setLocation] = useState(event?.location ?? '');
|
||
const [endpoint, setEndpoint] = useState(event?.sessionize_endpoint ?? '');
|
||
const [endpointError, setEndpointError] = useState<string | null>(null);
|
||
const [pretixApiKey, setPretixApiKey] = useState(event?.pretix_api_key ?? '');
|
||
const [pretixOrganizer, setPretixOrganizer] = useState(event?.pretix_organizer ?? '');
|
||
const [pretixEventSlug, setPretixEventSlug] = useState(event?.pretix_event_slug ?? '');
|
||
const [pretixOptions, setPretixOptions] = useState<PretixOptions | null>(null);
|
||
const [pretixLoading, setPretixLoading] = useState(false);
|
||
const [pretixError, setPretixError] = useState<string | null>(null);
|
||
const [realtimeClock, setRealtimeClock] = useState(event?.use_realtime_clock ?? false);
|
||
const [talkBonus, setTalkBonus] = useState(event?.talk_networking_bonus ?? 2);
|
||
const [busy, setBusy] = useState(false);
|
||
const lastFetchedKey = useRef<string | null>(null);
|
||
|
||
// When the API key changes, fetch the key's organizers + events (debounced)
|
||
// so the admin can pick the organizer + event slug from real data.
|
||
useEffect(() => {
|
||
const key = pretixApiKey.trim();
|
||
if (!key) {
|
||
setPretixOptions(null);
|
||
setPretixError(null);
|
||
lastFetchedKey.current = null;
|
||
return;
|
||
}
|
||
if (lastFetchedKey.current === key) return; // already fetched for this key
|
||
const t = setTimeout(async () => {
|
||
setPretixLoading(true);
|
||
setPretixError(null);
|
||
try {
|
||
const opts = await fetchPretixOptions(key);
|
||
setPretixOptions(opts);
|
||
lastFetchedKey.current = key;
|
||
// If the current selection isn't in the fetched list, keep it (it may
|
||
// be a valid slug the API just didn't return, e.g. inactive event).
|
||
} catch (e) {
|
||
setPretixError(e instanceof Error ? e.message : 'Failed to load Pretix options');
|
||
} finally {
|
||
setPretixLoading(false);
|
||
}
|
||
}, 500);
|
||
return () => clearTimeout(t);
|
||
}, [pretixApiKey]);
|
||
|
||
async function save() {
|
||
if (!name.trim() || !startDate || !endDate) return;
|
||
let source: string | null;
|
||
try {
|
||
source = normalizeSessionizeSource(endpoint);
|
||
setEndpointError(null);
|
||
} catch (error) {
|
||
setEndpointError(error instanceof Error ? error.message : 'Invalid Sessionize source');
|
||
return;
|
||
}
|
||
setBusy(true);
|
||
try {
|
||
await upsertEvent(event?.id ?? null, {
|
||
name: name.trim(),
|
||
start_date: startDate,
|
||
end_date: endDate,
|
||
location: location.trim() || null,
|
||
sessionize_endpoint: source,
|
||
pretix_organizer: pretixOrganizer.trim() || null,
|
||
pretix_event_slug: pretixEventSlug.trim() || null,
|
||
pretix_api_key: pretixApiKey.trim() || null,
|
||
use_realtime_clock: realtimeClock,
|
||
talk_networking_bonus: Math.max(0, Math.min(10, Math.round(talkBonus) || 0)),
|
||
});
|
||
onSaved(event ? 'Event updated' : 'Event created');
|
||
} catch (e) {
|
||
onSaved(e instanceof Error ? e.message : 'Save failed');
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<Modal
|
||
open
|
||
title={event ? 'Edit event' : 'New event'}
|
||
onClose={onClose}
|
||
footer={
|
||
<>
|
||
<button className="btn" onClick={onClose}>
|
||
Cancel
|
||
</button>
|
||
<button className="btn primary" disabled={busy || !name.trim() || !startDate || !endDate} onClick={save}>
|
||
{busy ? 'Saving…' : 'Save'}
|
||
</button>
|
||
</>
|
||
}
|
||
>
|
||
<div className="field">
|
||
<label>Name</label>
|
||
<input className="input" value={name} onChange={(e) => setName(e.target.value)} placeholder="e.g. APPE 2027" />
|
||
</div>
|
||
<div className="grid-2">
|
||
<div className="field">
|
||
<label>Start date</label>
|
||
<input className="input" type="date" value={startDate} onChange={(e) => setStartDate(e.target.value)} />
|
||
</div>
|
||
<div className="field">
|
||
<label>End date</label>
|
||
<input className="input" type="date" value={endDate} onChange={(e) => setEndDate(e.target.value)} />
|
||
</div>
|
||
</div>
|
||
<div className="field">
|
||
<label>Location</label>
|
||
<input className="input" value={location} onChange={(e) => setLocation(e.target.value)} placeholder="—" />
|
||
</div>
|
||
<div className="section">
|
||
<div className="section-head">
|
||
<span className="ico">📅</span>
|
||
<h4>Sessionize</h4>
|
||
{endpoint.trim() ? <span className="pill green">configured</span> : <span className="pill">not set</span>}
|
||
</div>
|
||
<div className="field">
|
||
<label htmlFor="sessionize-source">Sessionize endpoint</label>
|
||
<textarea
|
||
id="sessionize-source"
|
||
className="input"
|
||
rows={3}
|
||
value={endpoint}
|
||
onChange={(e) => { setEndpoint(e.target.value); setEndpointError(null); }}
|
||
aria-invalid={!!endpointError}
|
||
aria-describedby={endpointError ? 'sessionize-source-error' : undefined}
|
||
placeholder="https://sessionize.com/api/v2/<id>/view/GridSmart"
|
||
/>
|
||
{endpointError && <div id="sessionize-source-error" role="alert" className="error">{endpointError}</div>}
|
||
<div className="muted small" style={{ marginTop: 4 }}>
|
||
All / GridSmart URL or Sessionize script tag. Active events sync automatically every 15 minutes.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div className="section">
|
||
<div className="section-head">
|
||
<span className="ico">🎟️</span>
|
||
<h4>Pretix</h4>
|
||
{pretixApiKey.trim() && pretixOrganizer.trim() && pretixEventSlug.trim() ? (
|
||
<span className="pill green">configured</span>
|
||
) : (
|
||
<span className="pill">not set</span>
|
||
)}
|
||
</div>
|
||
<div className="field">
|
||
<label>Pretix API key</label>
|
||
<input
|
||
className="input mono"
|
||
value={pretixApiKey}
|
||
onChange={(e) => setPretixApiKey(e.target.value)}
|
||
placeholder="Paste the Pretix API token"
|
||
autoComplete="off"
|
||
/>
|
||
<div className="muted small" style={{ marginTop: 4 }}>
|
||
Paste the token — the organizers + events below load automatically. The key is stored
|
||
with this event and stays visible here.
|
||
</div>
|
||
</div>
|
||
{pretixApiKey.trim() && pretixLoading && (
|
||
<div className="muted small">Loading Pretix organizers…</div>
|
||
)}
|
||
{pretixApiKey.trim() && pretixError && (
|
||
<div className="pill red" style={{ display: 'inline-block', alignSelf: 'flex-start' }}>
|
||
{pretixError}
|
||
</div>
|
||
)}
|
||
{pretixOptions && (
|
||
<div className="grid-2">
|
||
<div className="field">
|
||
<label>Pretix organizer</label>
|
||
<select
|
||
className="input"
|
||
value={pretixOrganizer}
|
||
onChange={(e) => {
|
||
setPretixOrganizer(e.target.value);
|
||
setPretixEventSlug('');
|
||
}}
|
||
>
|
||
<option value="">— select —</option>
|
||
{pretixOptions.organizers.map((o) => (
|
||
<option key={o.slug} value={o.slug}>
|
||
{o.name} ({o.slug})
|
||
</option>
|
||
))}
|
||
</select>
|
||
</div>
|
||
<div className="field">
|
||
<label>Pretix event slug</label>
|
||
<select
|
||
className="input"
|
||
value={pretixEventSlug}
|
||
onChange={(e) => setPretixEventSlug(e.target.value)}
|
||
disabled={!pretixOrganizer}
|
||
>
|
||
<option value="">— select —</option>
|
||
{(pretixOptions.organizers.find((o) => o.slug === pretixOrganizer)?.events || []).map(
|
||
(ev) => (
|
||
<option key={ev.slug} value={ev.slug}>
|
||
{ev.name} ({ev.slug})
|
||
</option>
|
||
),
|
||
)}
|
||
</select>
|
||
</div>
|
||
</div>
|
||
)}
|
||
<div className="muted small" style={{ maxWidth: 640 }}>
|
||
The sidecar syncs paid Pretix tickets for this event every minute using the key above.
|
||
</div>
|
||
</div>
|
||
<div className="field">
|
||
<div className="row between" style={{ alignItems: 'center' }}>
|
||
<div>
|
||
<label>Real-time clock</label>
|
||
<div className="muted small" style={{ marginTop: 2 }}>
|
||
{realtimeClock
|
||
? 'The app uses the real system clock for Live/Next/Done.'
|
||
: 'The app uses a simulated mid-event clock (demo always looks live).'}
|
||
</div>
|
||
</div>
|
||
<Toggle checked={realtimeClock} onChange={setRealtimeClock} label={realtimeClock ? 'On' : 'Off'} />
|
||
</div>
|
||
</div>
|
||
<div className="field">
|
||
<div className="row between" style={{ alignItems: 'center' }}>
|
||
<div>
|
||
<label>Talk networking bonus</label>
|
||
<div className="muted small" style={{ marginTop: 2, maxWidth: 420 }}>
|
||
Points each party earns when two people who were checked into the same talk accept a
|
||
connection. <b>0 disables</b> the bonus (the +5 connection award still applies).
|
||
</div>
|
||
</div>
|
||
<input
|
||
className="input mono"
|
||
type="number"
|
||
min={0}
|
||
max={10}
|
||
step={1}
|
||
style={{ width: 84, textAlign: 'center' }}
|
||
value={Number.isFinite(talkBonus) ? talkBonus : 0}
|
||
onChange={(e) => setTalkBonus(parseInt(e.target.value, 10) || 0)}
|
||
/>
|
||
</div>
|
||
</div>
|
||
</Modal>
|
||
);
|
||
}
|
||
|
||
function SyncResultModal({ result, onClose }: { result: SyncResult; onClose: () => void }) {
|
||
const s = result.summary;
|
||
return (
|
||
<Modal
|
||
open
|
||
title={`Sessionize sync — ${result.event_name ?? result.event_id}`}
|
||
onClose={onClose}
|
||
footer={
|
||
<button className="btn primary" onClick={onClose}>
|
||
Close
|
||
</button>
|
||
}
|
||
>
|
||
{result.status === 'error' || !s ? (
|
||
<div className="pill red" style={{ display: 'inline-block', padding: '8px 12px' }}>
|
||
{result.error ?? 'Sync failed'}
|
||
</div>
|
||
) : (
|
||
<>
|
||
<div className="stat-grid">
|
||
<div className="stat">
|
||
<div className="val">{s.speakers_created}</div>
|
||
<div className="lbl">Speakers created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.speakers_updated}</div>
|
||
<div className="lbl">Speakers updated</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.sessions_created}</div>
|
||
<div className="lbl">Sessions created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.sessions_updated}</div>
|
||
<div className="lbl">Sessions updated</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.slots_created}</div>
|
||
<div className="lbl">Slots created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.speakers_linked}</div>
|
||
<div className="lbl">Speaker links</div>
|
||
</div>
|
||
</div>
|
||
{result.skipped_speakers && result.skipped_speakers.length > 0 && (
|
||
<div className="muted small">
|
||
Skipped {result.skipped_speakers.length} speaker(s) without an email: {result.skipped_speakers.join(', ')}
|
||
</div>
|
||
)}
|
||
{s.errors.length > 0 && (
|
||
<div style={{ marginTop: 12 }}>
|
||
<div className="pill red" style={{ display: 'inline-block', marginBottom: 8 }}>
|
||
{s.errors.length} error(s)
|
||
</div>
|
||
<ul style={{ margin: 0, paddingLeft: 18 }}>
|
||
{s.errors.map((e, i) => (
|
||
<li key={i} className="small">
|
||
{e}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</div>
|
||
)}
|
||
</>
|
||
)}
|
||
</Modal>
|
||
);
|
||
}
|
||
|
||
function fmtDate(d: string): string {
|
||
return new Date(d + 'T00:00:00').toLocaleDateString([], { month: 'short', day: 'numeric', year: 'numeric' });
|
||
}
|