import {InteractiveSpeakingExercise} from "@/interfaces/exam"; import {CommonProps} from "."; import {useEffect, useState} from "react"; import {BsCheckCircleFill, BsMicFill, BsPauseCircle, BsPlayCircle, BsTrashFill} from "react-icons/bs"; import dynamic from "next/dynamic"; import Button from "../Low/Button"; import useExamStore from "@/stores/examStore"; const Waveform = dynamic(() => import("../Waveform"), {ssr: false}); const ReactMediaRecorder = dynamic(() => import("react-media-recorder").then((mod) => mod.ReactMediaRecorder), { ssr: false, }); export default function InteractiveSpeaking({ id, title, text, type, prompts, updateIndex, onNext, onBack, }: InteractiveSpeakingExercise & CommonProps) { const [recordingDuration, setRecordingDuration] = useState(0); const [isRecording, setIsRecording] = useState(false); const [mediaBlob, setMediaBlob] = useState(); const [promptIndex, setPromptIndex] = useState(0); const [answers, setAnswers] = useState<{prompt: string; blob: string}[]>([]); const hasExamEnded = useExamStore((state) => state.hasExamEnded); useEffect(() => { if (updateIndex) updateIndex(promptIndex); }, [promptIndex, updateIndex]); useEffect(() => { if (hasExamEnded) { onNext({ exercise: id, solutions: mediaBlob ? [{id, solution: mediaBlob}] : [], score: {correct: 1, total: 1, missing: 0}, type, }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [hasExamEnded]); useEffect(() => { let recordingInterval: NodeJS.Timer | undefined = undefined; if (isRecording) { recordingInterval = setInterval(() => setRecordingDuration((prev) => prev + 1), 1000); } else if (recordingInterval) { clearInterval(recordingInterval); } return () => { if (recordingInterval) clearInterval(recordingInterval); }; }, [isRecording]); useEffect(() => { if (promptIndex === answers.length - 1) { setMediaBlob(answers[promptIndex].blob); } }, [answers, promptIndex]); const saveAnswer = () => { const answer = { prompt: prompts[promptIndex].text, blob: mediaBlob!, }; setAnswers((prev) => [...prev, answer]); setMediaBlob(undefined); }; return (
{title}
{prompts && prompts.length > 0 && (
)}
setMediaBlob(blob)} render={({status, startRecording, stopRecording, pauseRecording, resumeRecording, clearBlobUrl, mediaBlobUrl}) => (

Record your answer:

{status === "idle" && ( <>
{status === "idle" && ( { setRecordingDuration(0); startRecording(); setIsRecording(true); }} className="h-5 w-5 text-mti-gray-cool cursor-pointer" /> )} )} {status === "recording" && ( <>
{Math.floor(recordingDuration / 60) .toString(10) .padStart(2, "0")} : {Math.floor(recordingDuration % 60) .toString(10) .padStart(2, "0")}
{ setIsRecording(false); pauseRecording(); }} className="text-red-500 w-8 h-8 cursor-pointer" /> { setIsRecording(false); stopRecording(); }} className="text-mti-purple-light w-8 h-8 cursor-pointer" />
)} {status === "paused" && ( <>
{Math.floor(recordingDuration / 60) .toString(10) .padStart(2, "0")} : {Math.floor(recordingDuration % 60) .toString(10) .padStart(2, "0")}
{ setIsRecording(true); resumeRecording(); }} className="text-mti-purple-light w-8 h-8 cursor-pointer" /> { setIsRecording(false); stopRecording(); }} className="text-mti-purple-light w-8 h-8 cursor-pointer" />
)} {status === "stopped" && mediaBlobUrl && ( <>
{ setRecordingDuration(0); clearBlobUrl(); setMediaBlob(undefined); }} /> { clearBlobUrl(); setRecordingDuration(0); startRecording(); setIsRecording(true); setMediaBlob(undefined); }} className="h-5 w-5 text-mti-gray-cool cursor-pointer" />
)}
)} />
); }