import { useState } from "react"; import { Box, Modal, Typography, useTheme } from "@mui/material"; import { UploadFile } from "./UploadFile"; import { UploadedFile } from "./UploadedFile"; import { useRootContainerSize } from "@contexts/RootContainerWidthContext"; import { useQuizViewStore } from "@stores/quizView"; import { ACCEPT_SEND_FILE_TYPES_MAP } from "@/components/ViewPublicationPage/tools/fileUpload"; import type { QuizQuestionFile } from "@model/questionTypes/file"; import { useTranslation } from "react-i18next"; import { sendFile } from "@api/quizRelase"; import { enqueueSnackbar } from "notistack"; export type ModalWarningType = "errorType" | "errorSize" | "picture" | "video" | "audio" | "document" | null; type FileProps = { currentQuestion: QuizQuestionFile; }; export const File = ({ currentQuestion }: FileProps) => { const theme = useTheme(); const { t } = useTranslation(); const answers = useQuizViewStore((state) => state.answers); const updateAnswer = useQuizViewStore((state) => state.updateAnswer); const setFileUploading = useQuizViewStore((state) => state.setFileUploading); const [modalWarningType, setModalWarningType] = useState(null); const [isSending, setIsSending] = useState(false); const [isUploading, setIsUploading] = useState(false); const isMobile = useRootContainerSize() < 500; const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer as string; const handleFileUpload = async (file: File) => { if (!file) return; try { setIsUploading(true); setFileUploading(currentQuestion.id, true); const result = await sendFile({ questionId: currentQuestion.id, body: { name: file.name, file, preview: false, }, qid: window.location.pathname.split("/").pop() || "", }); if (result) { updateAnswer(currentQuestion.id, result.fileName, 0); enqueueSnackbar(t("File uploaded successfully")); } } catch (error) { console.error(error); enqueueSnackbar(t("Failed to upload file")); } finally { setIsUploading(false); setFileUploading(currentQuestion.id, false); } }; return ( {currentQuestion.title} {answer?.split("|")[0] ? ( ) : ( )} {answer && currentQuestion.content.type === "picture" && ( )} {answer && currentQuestion.content.type === "video" && ( setModalWarningType(null)} > ); }; const CurrentModal = ({ status }: { status: ModalWarningType }) => { const { t } = useTranslation(); switch (status) { case null: return null; case "errorType": return {t("Incorrect file type selected")}; case "errorSize": return {t("File is too big. Maximum size is 50 MB")}; default: return ( <> {t("Acceptable file extensions")}: {ACCEPT_SEND_FILE_TYPES_MAP[status].join(" ")} ); } };