frontAnswerer/lib/components/ViewPublicationPage/Question.tsx

220 lines
6.7 KiB
TypeScript
Raw Normal View History

2024-02-19 14:20:21 +00:00
import { Box, Link, useTheme } from "@mui/material";
2023-12-16 14:55:56 +00:00
import { Footer } from "./Footer";
2023-12-16 14:55:56 +00:00
import { Date } from "./questions/Date";
import { Emoji } from "./questions/Emoji";
2023-12-16 14:55:56 +00:00
import { File } from "./questions/File";
import { Images } from "./questions/Images";
import { Number } from "./questions/Number";
2023-12-16 14:55:56 +00:00
import { Page } from "./questions/Page";
import { Rating } from "./questions/Rating";
import { Select } from "./questions/Select";
import { Text } from "./questions/Text";
import { Variant } from "./questions/Variant";
import { Varimg } from "./questions/Varimg";
2023-12-16 14:55:56 +00:00
import type { RealTypedQuizQuestion } from "../../model/questionTypes/shared";
2023-12-16 14:55:56 +00:00
import { NameplateLogoFQ } from "@icons/NameplateLogoFQ";
import { NameplateLogoFQDark } from "@icons/NameplateLogoFQDark";
import { notReachable } from "@utils/notReachable";
import { quizThemes } from "@utils/themes/Publication/themePublication";
2024-03-01 14:08:09 +00:00
import { DESIGN_LIST } from "@/utils/designList";
import { type ReactNode } from "react";
2024-10-24 12:50:24 +00:00
import { isProduction } from "@/utils/defineDomain";
2025-05-01 13:15:54 +00:00
import { useQuizStore } from "@/stores/useQuizStore";
2025-09-21 09:55:46 +00:00
import { CustomCircularTimer } from "@/ui_kit/timer/CircularTimer";
import { useQuestionTimer } from "@/utils/hooks/FlowControlLogic/useQuestionTimer";
import { useState, useEffect } from "react";
2023-12-16 14:55:56 +00:00
type Props = {
2024-04-23 14:45:49 +00:00
currentQuestion: RealTypedQuizQuestion;
currentQuestionStepNumber: number | null;
nextButton: ReactNode;
prevButton: ReactNode;
questionSelect: ReactNode;
2024-03-01 14:08:09 +00:00
};
export const Question = ({
2024-04-23 14:45:49 +00:00
currentQuestion,
currentQuestionStepNumber,
nextButton,
prevButton,
questionSelect,
}: Props) => {
2024-04-23 14:45:49 +00:00
const theme = useTheme();
const { settings, show_badge, quizId, preview } = useQuizStore();
// Состояние для отслеживания оставшегося времени
const [remainingTime, setRemainingTime] = useState<number>(0);
const [isTimerActive, setIsTimerActive] = useState<boolean>(false);
// Получаем настройки таймера
const timerEnabled = Boolean(settings.questionTimerEnabled);
const timerDuration = settings.cfg.time_of_passing ?? 0;
// Эффект для обновления таймера
useEffect(() => {
if (timerEnabled && timerDuration > 0) {
setRemainingTime(timerDuration);
setIsTimerActive(true);
const interval = setInterval(() => {
setRemainingTime((prev) => {
if (prev <= 1) {
setIsTimerActive(false);
return 0;
}
return prev - 1;
});
}, 1000);
return () => clearInterval(interval);
} else {
setIsTimerActive(false);
setRemainingTime(0);
}
}, [timerEnabled, timerDuration, currentQuestion.id]);
2024-12-22 11:49:56 +00:00
2024-04-23 14:45:49 +00:00
return (
<Box
sx={{
height: "100%",
backgroundPosition: "center",
backgroundSize: "cover",
2024-05-31 16:41:18 +00:00
backgroundImage: settings.cfg.design ? `url(${DESIGN_LIST[settings.cfg.theme]})` : null,
2024-04-23 14:45:49 +00:00
}}
>
<Box
sx={{
height: "100%",
display: "flex",
flexDirection: "column",
background: settings.cfg.design
? quizThemes[settings.cfg.theme].isLight
? "transparent"
: "linear-gradient(90deg,#272626, transparent)"
: theme.palette.background.default,
overflow: "hidden",
}}
>
2024-03-01 14:08:09 +00:00
<Box
2024-04-23 14:45:49 +00:00
sx={{
overflow: "auto",
width: "100%",
flexGrow: 1,
scrollbarWidth: "none",
"&::-webkit-scrollbar": {
width: 0,
},
}}
>
<Box
2024-03-26 00:06:54 +00:00
sx={{
2024-04-23 14:45:49 +00:00
width: "100%",
minHeight: "100%",
maxWidth: "1440px",
padding: "40px 25px 20px",
margin: "0 auto",
display: "flex",
flexDirection: "column",
justifyContent: "space-between",
2024-03-26 00:06:54 +00:00
}}
2024-04-23 14:45:49 +00:00
>
<QuestionByType
key={currentQuestion.id}
question={currentQuestion}
stepNumber={currentQuestionStepNumber}
/>
2025-09-22 05:16:21 +00:00
<Box
sx={{
mt: "20px",
alignSelf: "end",
display: "flex",
flexDirection: "column",
alignItems: "end",
gap: "13px",
}}
>
{timerEnabled && isTimerActive && (
<CustomCircularTimer
duration={timerDuration}
remaining={remainingTime}
showTime={true}
size={76}
thickness={4}
/>
)}
{show_badge && (
2025-09-21 09:55:46 +00:00
<Link
target="_blank"
href={`https://${isProduction ? "" : "s"}quiz.pena.digital/answer/v1.0.0/logo?q=${quizId}`}
>
{quizThemes[settings.cfg.theme].isLight ? (
<NameplateLogoFQ
style={{
fontSize: "34px",
width: "200px",
height: "auto",
}}
/>
) : (
<NameplateLogoFQDark
style={{
fontSize: "34px",
width: "200px",
height: "auto",
}}
/>
)}
</Link>
2025-09-22 05:16:21 +00:00
)}
</Box>
2024-04-23 14:45:49 +00:00
</Box>
2024-03-01 14:08:09 +00:00
</Box>
2024-04-23 14:45:49 +00:00
{questionSelect}
<Footer
stepNumber={currentQuestionStepNumber}
prevButton={prevButton}
nextButton={nextButton}
/>
2024-04-23 14:45:49 +00:00
</Box>
</Box>
);
2023-12-16 14:55:56 +00:00
};
2024-05-31 16:41:18 +00:00
function QuestionByType({ question, stepNumber }: { question: RealTypedQuizQuestion; stepNumber: number | null }) {
2024-04-23 14:45:49 +00:00
switch (question.type) {
case "variant":
return <Variant currentQuestion={question} />;
case "images":
return <Images currentQuestion={question} />;
case "varimg":
return <Varimg currentQuestion={question} />;
case "emoji":
return <Emoji currentQuestion={question} />;
case "text":
return (
<Text
currentQuestion={question}
stepNumber={stepNumber}
/>
);
2024-04-23 14:45:49 +00:00
case "select":
return <Select currentQuestion={question} />;
case "date":
return <Date currentQuestion={question} />;
case "number":
return <Number currentQuestion={question} />;
case "file":
return <File currentQuestion={question} />;
case "page":
return <Page currentQuestion={question} />;
case "rating":
return <Rating currentQuestion={question} />;
default:
notReachable(question);
}
}