import { Box, FormGroup, RadioGroup, Typography, useTheme } from "@mui/material"; import { useEffect, useMemo } from "react"; import { VariantItem } from "./VariantItem"; import { useRootContainerSize } from "@contexts/RootContainerWidthContext"; import { useQuizViewStore } from "@stores/quizView"; import type { QuizQuestionVariant } from "@model/questionTypes/variant"; import moment from "moment"; type VariantProps = { currentQuestion: QuizQuestionVariant; }; export const Variant = ({ currentQuestion }: VariantProps) => { const theme = useTheme(); const isMobile = useRootContainerSize() < 650; const isTablet = useRootContainerSize() < 850; const answers = useQuizViewStore((state) => state.answers); const ownVariants = useQuizViewStore((state) => state.ownVariants); const updateOwnVariant = useQuizViewStore((state) => state.updateOwnVariant); const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer; const ownVariant = ownVariants.find((variant) => variant.id === currentQuestion.id); const Group = currentQuestion.content.multi ? FormGroup : RadioGroup; useEffect(() => { if (!ownVariant) { updateOwnVariant(currentQuestion.id, ""); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const choiceImgUrlQuestion = useMemo(() => { if ( currentQuestion.content.editedUrlImagesList !== undefined && currentQuestion.content.editedUrlImagesList !== null ) { return currentQuestion.content.editedUrlImagesList[isMobile ? "mobile" : isTablet ? "tablet" : "desktop"]; } else { return currentQuestion.content.back; } }, [currentQuestion]); if (moment.isMoment(answer)) throw new Error("Answer is Moment in Variant question"); return ( {currentQuestion.title} answer === id)} sx={{ display: "flex", flexWrap: "wrap", flexDirection: "row", justifyContent: "space-between", flexBasis: "100%", marginTop: "20px", width: isMobile ? "100%" : undefined, }} > {currentQuestion.content.variants .filter((v) => { if (!v.isOwn) return true; return v.isOwn && currentQuestion.content.own; }) .map((variant, index) => ( ))} {choiceImgUrlQuestion && choiceImgUrlQuestion !== " " && choiceImgUrlQuestion !== null && ( )} ); };