import { useEffect, useMemo, useRef, useState } from "react"; import { Box, ButtonBase, RadioGroup, Typography, useTheme } from "@mui/material"; import { VarimgVariant } from "./VarimgVariant"; import { OwnVarimgImage } from "./OwnVarimgImage"; import { useQuizViewStore } from "@stores/quizView"; import { useRootContainerSize } from "@contexts/RootContainerWidthContext"; import BlankImage from "@icons/BlankImage"; import type { QuizQuestionVarImg } from "@model/questionTypes/varimg"; import moment from "moment"; import { useTranslation } from "react-i18next"; type VarimgProps = { currentQuestion: QuizQuestionVarImg; }; export const Varimg = ({ currentQuestion }: VarimgProps) => { const [isSending, setIsSending] = useState(false); const answers = useQuizViewStore((state) => state.answers); const ownVariants = useQuizViewStore((state) => state.ownVariants); const updateOwnVariant = useQuizViewStore((state) => state.updateOwnVariant); const { t } = useTranslation(); const theme = useTheme(); const isMobile = useRootContainerSize() < 650; const isTablet = useRootContainerSize() < 850; const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {}; const ownVariant = ownVariants.find((variant) => variant.id === currentQuestion.id); const variant = currentQuestion.content.variants.find(({ id }) => answer === id); const ownVariantInQuestion = useMemo( () => currentQuestion.content.variants.find((v) => v.isOwn), [currentQuestion.content.variants] ); const ownVariantData = ownVariants.find((v) => v.id === answer); const ownImageUrl = useMemo(() => { return ownVariantData?.variant.file ? URL.createObjectURL(ownVariantData.variant.file) : ownVariantData?.variant.localImageUrl; }, [ownVariantData]); const inputRef = useRef(null); useEffect(() => { if (!ownVariant) { updateOwnVariant(currentQuestion.id, ""); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const choiceImgUrlAnswer = useMemo(() => { if (variant !== undefined) { if (variant.editedUrlImagesList !== undefined && variant.editedUrlImagesList !== null) { return variant.editedUrlImagesList[isMobile ? "mobile" : isTablet ? "tablet" : "desktop"]; } else { return variant.extendedText; } } }, [variant]); 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; } }, [variant]); const handlePreviewAreaClick = () => { if (ownVariantInQuestion) { inputRef.current?.click(); } }; 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%", width: isMobile ? "100%" : undefined, }} > {currentQuestion.content.variants .filter((v) => { if (!v.isOwn) return true; return v.isOwn && currentQuestion.content.own; }) .map((variant, index) => ( ))} {ownVariantInQuestion && ( )} {(() => { if (answer) { const imageUrl = variant?.isOwn && ownImageUrl ? ownImageUrl : choiceImgUrlAnswer; if (imageUrl) { return ( ); } return ; } if (choiceImgUrlQuestion && choiceImgUrlQuestion.trim().length > 0) { return ( ); } if (currentQuestion.content.replText && currentQuestion.content.replText.trim().length > 0) { return currentQuestion.content.replText; } return isMobile ? t("Select an answer option below") : t("Select an answer option on the left"); })()} ); };