import { useEffect, useMemo, useState } from 'react' import { Link, useParams } from 'react-router' import { ArrowLeft, CalendarClock, ExternalLink, Lock } from 'lucide-react' import { ApiError } from '../api/client' import { fetchLearnerLiveSession, joinLearnerLiveSession, type LearnerLiveSessionDetail, } from '../learner-live-courses' type LoadState = 'loading' | 'ready' | 'error' | 'not-found' function formatDateTime(value?: number, timezone?: string): string { if (!value) return 'TBA' try { return new Intl.DateTimeFormat(undefined, { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: '2-digit', timeZone: timezone, }).format(new Date(value)) } catch { return new Date(value).toLocaleString() } } export function LearnerLiveSessionPage() { const { id = '', sessionId = '' } = useParams() const [detail, setDetail] = useState(null) const [state, setState] = useState('loading') const [error, setError] = useState('') const [joinError, setJoinError] = useState('') const [joining, setJoining] = useState(false) useEffect(() => { let cancelled = false const load = async () => { try { setState('loading') const result = await fetchLearnerLiveSession(id, sessionId) if (cancelled) return if (!result.session.id || !result.course.id) { setState('not-found') return } setDetail(result) setState('ready') } catch (err) { if (cancelled) return if (err instanceof ApiError && err.status === 404) { setState('not-found') return } setError(err instanceof Error ? err.message : 'Unable to load live session') setState('error') } } void load() return () => { cancelled = true } }, [id, sessionId]) const canJoin = useMemo(() => Boolean(detail?.access.allowed), [detail]) const handleJoin = async () => { if (!detail) return setJoinError('') setJoining(true) try { const result = await joinLearnerLiveSession(detail.course.id, detail.session.id) if (!result.joinUrl) { throw new Error('Join URL is not available yet') } window.open(result.joinUrl, '_blank', 'noopener,noreferrer') } catch (err) { setJoinError(err instanceof Error ? err.message : 'Failed to join session') } finally { setJoining(false) } } if (state === 'loading') { return

Loading session details...

} if (state === 'not-found') { return (

Session not found

This session may not be available for your learner account.

Back to live sessions
) } if (state === 'error') { return

{error}

} return (
Learner live sessions

{detail?.course.title}

{detail?.session.title}

{formatDateTime(detail?.session.startsAt, detail?.session.timezone || detail?.cohort.timezone)}

{!canJoin && (

Session Locked

{detail?.access.message || 'Access to this session is currently restricted.'}

)}

Cohort: {detail?.cohort.title}

{canJoin ? ( ) : (

Join is enabled when enrollment and entitlement checks pass.

)} {joinError &&

{joinError}

}
) }