Files
Convivo/apps/convivo/src/lib/useEvent.ts
T
f9a8513620 Real-time clock toggle: per-event use_realtime_clock flag
- Migration 20260834000000: events.use_realtime_clock (boolean, default false)
- Admin: Real-time clock toggle in the Events create/edit modal
- Convivo: useEvent picks new Date() vs simulated DEFAULT_NOW from the flag;
  Home shows a 'Demo clock' badge only in simulated mode
- Docs: app-structure, admin-app, schema, next-steps

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-08-25 09:22:28 +02:00

70 lines
2.2 KiB
TypeScript

import { useEffect, useState } from 'react';
import { getActiveEvent, getAgenda, groupAgenda } from './api';
import { DEFAULT_NOW } from './time';
import type { Event, Session, Slot } from './types';
export interface EventData {
event: Event | null;
slots: { slot: Slot; sessions: Session[] }[];
sessionById: Map<string, Session>;
slotById: Map<number, Slot>;
now: Date;
/** Which clock drives slot status: the simulated mid-event clock or the real one. */
clockMode: 'simulated' | 'realtime';
loading: boolean;
error: string | null;
reload: () => void;
}
/** Loads the active event + agenda, exposes them grouped by slot, plus the event clock. */
export function useEvent(): EventData {
const [event, setEvent] = useState<Event | null>(null);
const [slots, setSlots] = useState<{ slot: Slot; sessions: Session[] }[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [nonce, setNonce] = useState(0);
useEffect(() => {
let active = true;
(async () => {
try {
const [ev, rows] = await Promise.all([getActiveEvent(), getAgenda()]);
if (!active) return;
setEvent(ev);
setSlots(groupAgenda(rows));
} catch (e) {
if (active) setError(e instanceof Error ? e.message : 'Failed to load agenda');
} finally {
if (active) setLoading(false);
}
})();
return () => {
active = false;
};
}, [nonce]);
const sessionById = new Map<string, Session>();
const slotById = new Map<number, Slot>();
for (const { slot, sessions } of slots) {
slotById.set(slot.id, slot);
for (const s of sessions) sessionById.set(s.id, s);
}
// The event's `use_realtime_clock` flag decides the clock: the real system
// time, or the simulated mid-event clock (so the demo always looks live).
const clockMode: 'simulated' | 'realtime' = event?.use_realtime_clock ? 'realtime' : 'simulated';
const now = clockMode === 'realtime' ? new Date() : DEFAULT_NOW;
return {
event,
slots,
sessionById,
slotById,
now,
clockMode,
loading,
error,
reload: () => setNonce((n) => n + 1),
};
}