import { useEffect, useState } from "react"; import { DevicesResponse, GeneralResponse, getDevices, getGeneral, getQuestions, QuestionsResponse, } from "@api/statistic"; import type { Moment } from "moment"; interface useAnalyticsProps { ready: boolean; quizId: string | undefined; to: Moment | null; from: Moment | null; } export function useAnalytics({ ready, quizId, to, from }: useAnalyticsProps) { const formatTo = to?.unix(); const formatFrom = from?.unix(); const [devices, setDevices] = useState(null); const [general, setGeneral] = useState(null); const [questions, setQuestions] = useState(null); useEffect(() => { if (!quizId || !ready) return; const requestStatistics = async () => { if (!formatTo || !formatFrom) { return; } const [gottenGeneral] = await getGeneral(quizId, formatTo, formatFrom); const [gottenDevices] = await getDevices(quizId, formatTo, formatFrom); const [gottenQuestions] = await getQuestions( quizId, formatTo, formatFrom, ); if (gottenGeneral) { setGeneral(gottenGeneral); } if (gottenDevices) { setDevices(gottenDevices); } if (gottenQuestions) { setQuestions(gottenQuestions); } }; requestStatistics(); }, [ready, quizId, to, from]); return { devices, general, questions }; }