import { useCallback, useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { useSessionQuestionsRtdb } from "../hooks/useSessionQuestionsRtdb"; import { AudienceQuestionCard, type AudienceQuestionItem } from "./AudienceQuestionCard"; import { AskQuestionModal } from "./AskQuestionModal"; type QuestionItem = AudienceQuestionItem & { _id?: string }; const sample: QuestionItem[] = [ { author: "Ashok Kuwal", text: "Role of GINA Guidelines for Iniang and Monitoring Asthma Treatment?", up: 2, down: 3, }, { author: "Ashok Kuwal", text: "Role of GINA Guidelines for Iniang and Monitoring Asthma Treatment?", up: 2, down: 3, }, { author: "Ashok Kuwal", text: "Role of GINA Guidelines for Iniang and Monitoring Asthma Treatment?", up: 2, down: 3, }, ]; type SessionQuestionsCardProps = { title?: string; showQuestionNav?: boolean; eventId?: string; sessionId?: string; }; export function SessionQuestionsCard({ title = "Session Questions", showQuestionNav = false, eventId, sessionId, }: SessionQuestionsCardProps) { const live = Boolean(eventId?.trim() && sessionId?.trim()); const eid = eventId?.trim() ?? ""; const sid = sessionId?.trim() ?? ""; const { items: rtdbItems, loading, error } = useSessionQuestionsRtdb( live ? sid : null, live ? eid : null, ); const items: QuestionItem[] = live ? rtdbItems.map((r) => ({ _id: r._id, author: r.author, text: r.text, up: r.up, down: r.down, })) : sample; const total = items.length; const [index, setIndex] = useState(0); const [askModalOpen, setAskModalOpen] = useState(false); useEffect(() => { if (index >= total && total > 0) setIndex(0); else if (total === 0) setIndex(0); }, [total, index]); const goNext = useCallback(() => { if (total <= 0) return; setIndex((i) => (i + 1) % total); }, [total]); const goPrev = useCallback(() => { if (total <= 0) return; setIndex((i) => (i - 1 + total) % total); }, [total]); useEffect(() => { if (total <= 1) return; const id = window.setInterval(goNext, 2000); return () => window.clearInterval(id); }, [total, goNext]); const safeIndex = total > 0 ? Math.min(index, total - 1) : 0; const current = total > 0 ? items[safeIndex] : null; const viewAllTo = live ? `/questions?event_id=${encodeURIComponent(eid)}&session_id=${encodeURIComponent(sid)}` : null; return (

{title}

{total > 0 ? ( {safeIndex + 1}/{total} ) : null}
{showQuestionNav && total > 1 ? (
) : null}
{live && loading ? (

Loading questions…

) : live && error ? (

{error}

) : current ? (
) : (

No questions yet.

)}
{viewAllTo ? ( View All ) : ( View All )}
{live ? ( setAskModalOpen(false)} eventId={eid} sessionId={sid} idSuffix="-card" /> ) : null}
); }