import { useEffect, useState } from 'react';
import { useAuth } from '../context/AuthContext';
import { useEvent } from '../lib/useEvent';
import { getMyCheckins, getMyFeedback, getMyPreferences, getMySpeakerSessions } from '../lib/api';
import { fmtRange, greeting, slotStatus } from '../lib/time';
import type { Feedback, Preference, Session } from '../lib/types';
import { Sheet, Spinner } from '../components/ui';
/** A single session block inside the Now/Next card (title + room + speaker). */
function SessionBlock({ session, checkedIn }: { session: Session; checkedIn?: boolean }) {
return (
<>
{session.title}
{session.room} ยท {session.speakers}
{checkedIn !== undefined && (
checkedIn ? (
โ Checked in
) : (
Show your QR to staff to check in
)
)}
>
);
}
/** The list of concurrent sessions shown when the user has no pick for the slot. */
function ConcurrentList({ sessions }: { sessions: Session[] }) {
return (
{sessions.map((s) => (
{s.title}
{s.room} ยท {s.speakers}
))}
No pick yet โ tap a session in the Agenda to choose.
);
}
export function Home({ go }: { go: (tab: string) => void }) {
const { contact, registration } = useAuth();
const { event, slots, sessionById, slotById, now, clockMode, loading } = useEvent();
const [prefs, setPrefs] = useState([]);
const [checkins, setCheckins] = useState([]);
const [mySpeakerSessions, setMySpeakerSessions] = useState([]);
const [myFeedback, setMyFeedback] = useState([]);
const [materialSheetOpen, setMaterialSheetOpen] = useState(false);
const email = contact?.email ?? '';
useEffect(() => {
let active = true;
(async () => {
try {
const [p, c, sp, fb] = await Promise.all([
getMyPreferences(),
getMyCheckins(email),
getMySpeakerSessions(email),
getMyFeedback(email),
]);
if (!active) return;
setPrefs(p);
setCheckins(c);
setMySpeakerSessions(sp);
setMyFeedback(fb);
} catch {
/* non-fatal */
}
})();
return () => {
active = false;
};
}, [email]);
if (loading || !contact) {
return (
);
}
if (!event) {
return (
APPE
๐
No active event right now. Check back soon!
);
}
const points = registration?.points ?? 0;
const pickFor = (slotId: number) => prefs.find((p) => p.slot_id === slotId)?.session_id;
const current = slots.find(({ slot }) => slotStatus(slot, now) === 'now');
const next = slots.find(({ slot }) => slotStatus(slot, now) === 'next');
const nowPick = current ? sessionById.get(pickFor(current.slot.id) ?? '') : undefined;
const nextPick = next ? sessionById.get(pickFor(next.slot.id) ?? '') : undefined;
// Sessions the signed-in contact speaks in that still have no material uploaded.
// Service sessions have no material requirement, so they're excluded.
const pendingMaterial = mySpeakerSessions
.map((id) => sessionById.get(id))
.filter((s): s is Session => !!s && !s.material_path && s.slot_type !== 'service_session');
// Pending feedback: sessions the contact is checked into that are over, where
// they are NOT the speaker, and they haven't submitted feedback yet.
const feedbackPending = checkins
.map((id) => sessionById.get(id))
.filter((s): s is Session => !!s)
.filter((s) => {
const sl = slotById.get(s.slot_id);
return !!sl && slotStatus(sl, now) === 'done';
})
.filter((s) => !mySpeakerSessions.includes(s.id))
.filter((s) => !myFeedback.some((f) => f.session_id === s.id));
// All of the contact's picks for today, in slot order.
const picks = prefs
.map((p) => ({ slot: slotById.get(p.slot_id), session: sessionById.get(p.session_id) }))
.filter((r): r is { slot: NonNullable>; session: Session } => !!r.slot && !!r.session)
.sort((a, b) => a.slot.id - b.slot.id);
const firstName = contact.full_name.split(' ')[0];
return (
{greeting(now)}, {firstName} ๐
{event.name}
{clockMode === 'simulated' && (
๐ฌ Demo clock
)}
{pendingMaterial.length > 0 && (
setMaterialSheetOpen(true)}>
๐ {pendingMaterial.length}
)}
go('leaderboard')}>โก {points}
{/* Pending feedback: nudge to rate a session they attended. */}
{feedbackPending.length > 0 && (
go('agenda')}>
โญ {feedbackPending.length === 1 ? 'Rate your last session' : `Rate ${feedbackPending.length} sessions`}
)}
{/* Now: the current slot. With a pick, show it; without, show everything running. */}
{current && (
โ Now
{fmtRange(current.slot)}
{nowPick ? (
) : current.sessions.length > 0 ? (
) : (
Nothing scheduled right now.
)}
)}
{/* Next: the upcoming slot. */}
{next && (
Next
{fmtRange(next.slot)}
{nextPick ? (
) : next.sessions.length > 0 ? (
) : (
Nothing scheduled next.
)}
)}
{/* My picks today */}
{picks.length > 0 ? (
{picks.map((r) => (
go('agenda')}>
{fmtRange(r.slot)}
{r.session.title}
{r.session.room && {r.session.room} }
))}
) : (
No picks yet โ tap a session in the Agenda to choose.
)}
go('scan')}>
๐ท
Scan & connect
go('agenda')}>
๐
Agenda
go('connections')}>
๐ค
Connections
go('leaderboard')}>
๐
Leaderboard
{/* Pending material popup (speakers only, when a session is missing its attachment) */}
setMaterialSheetOpen(false)}>
๐ Pending material
The following sessions are pending to upload their attachments.
{pendingMaterial.map((s) => {
const slot = slots.find((x) => x.slot.id === s.slot_id)?.slot;
return (
{
setMaterialSheetOpen(false);
go('agenda');
}}
>
{slot ? fmtRange(slot) : ''}
{s.title}
{s.room}
);
})}
setMaterialSheetOpen(false)}>
Got it
);
}