import {SpeakingExercise} from "@/interfaces/exam"; import {CommonProps} from "."; import {Fragment, 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 Speaking({id, title, text, type, prompts, onNext, onBack}: SpeakingExercise & CommonProps) { const [recordingDuration, setRecordingDuration] = useState(0); const [isRecording, setIsRecording] = useState(false); const [mediaBlob, setMediaBlob] = useState(); const hasExamEnded = useExamStore((state) => state.hasExamEnded); 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]); return (
{title} {text.split("\\n").map((line, index) => ( {line}
))}
{prompts && prompts.length > 0 && (
You should talk about the following things:
{prompts.map((x, index) => (
  • {x}
  • ))}
    )}
    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" />
    )}
    )} />
    ); }