2024-04-24 15:56:11 +00:00
|
|
|
|
import lightTheme from "@/utils/themes/light";
|
2024-05-08 11:23:15 +00:00
|
|
|
|
import { Button, Fade, ThemeProvider, useMediaQuery } from "@mui/material";
|
2024-05-08 12:12:40 +00:00
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
2024-04-24 15:56:11 +00:00
|
|
|
|
import { createPortal } from "react-dom";
|
2024-05-04 16:18:56 +00:00
|
|
|
|
import QuizDialog from "../shared/QuizDialog";
|
|
|
|
|
import RunningStripe from "../shared/RunningStripe";
|
2024-05-08 11:23:15 +00:00
|
|
|
|
import { useAutoOpenTimer } from "../shared/useAutoOpenTimer";
|
2024-05-04 16:47:47 +00:00
|
|
|
|
import { useQuizCompletionStatus } from "../shared/useQuizCompletionStatus";
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const PADDING = 10;
|
2024-05-04 15:12:22 +00:00
|
|
|
|
const WIDGET_DEFAULT_WIDTH = "600px";
|
|
|
|
|
const WIDGET_DEFAULT_HEIGHT = "800px";
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
quizId: string;
|
|
|
|
|
position: "left" | "right";
|
2024-05-03 19:30:38 +00:00
|
|
|
|
buttonBackgroundColor?: string;
|
2024-05-04 16:58:01 +00:00
|
|
|
|
buttonTextColor?: string;
|
2024-05-04 15:12:22 +00:00
|
|
|
|
dimensions?: { width: string; height: string; };
|
2024-05-04 15:22:47 +00:00
|
|
|
|
fullScreen?: boolean;
|
2024-05-07 10:12:42 +00:00
|
|
|
|
buttonFlash?: boolean;
|
2024-05-08 12:12:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Скрывать виджет первые X секунд
|
|
|
|
|
*/
|
2024-05-08 11:23:15 +00:00
|
|
|
|
autoOpenTime?: number;
|
2024-05-08 12:12:40 +00:00
|
|
|
|
/**
|
2024-05-09 11:39:36 +00:00
|
|
|
|
* Открыть квиз через X секунд, 0 - сразу
|
2024-05-08 12:12:40 +00:00
|
|
|
|
*/
|
2024-05-09 11:39:36 +00:00
|
|
|
|
autoShowQuizTime?: number | null;
|
2024-05-08 11:23:15 +00:00
|
|
|
|
hideOnMobile?: boolean;
|
2024-04-24 15:56:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 10:12:42 +00:00
|
|
|
|
export default function QuizSideButton({
|
|
|
|
|
quizId,
|
|
|
|
|
position,
|
|
|
|
|
buttonBackgroundColor,
|
|
|
|
|
buttonTextColor,
|
|
|
|
|
dimensions,
|
|
|
|
|
fullScreen = false,
|
|
|
|
|
buttonFlash = false,
|
2024-05-08 11:23:15 +00:00
|
|
|
|
autoOpenTime = 0,
|
2024-05-09 11:39:36 +00:00
|
|
|
|
autoShowQuizTime = null,
|
2024-05-08 11:23:15 +00:00
|
|
|
|
hideOnMobile = false,
|
2024-05-07 10:12:42 +00:00
|
|
|
|
}: Props) {
|
2024-04-24 15:56:11 +00:00
|
|
|
|
const [isQuizShown, setIsQuizShown] = useState<boolean>(false);
|
2024-05-04 12:53:56 +00:00
|
|
|
|
const isMobile = useMediaQuery("(max-width: 600px)");
|
2024-05-04 16:47:47 +00:00
|
|
|
|
const isQuizCompleted = useQuizCompletionStatus(quizId);
|
2024-05-07 10:12:42 +00:00
|
|
|
|
const [isFlashEnabled, setIsFlashEnabled] = useState<boolean>(buttonFlash);
|
2024-05-08 11:23:15 +00:00
|
|
|
|
const isWidgetHidden = useAutoOpenTimer(autoOpenTime);
|
2024-05-08 12:12:40 +00:00
|
|
|
|
const preventQuizAutoShowRef = useRef<boolean>(false);
|
|
|
|
|
|
|
|
|
|
useEffect(function setAutoShowQuizTimer() {
|
2024-05-09 11:39:36 +00:00
|
|
|
|
if (autoShowQuizTime === null) return;
|
2024-05-08 12:12:40 +00:00
|
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
|
if (!preventQuizAutoShowRef.current) setIsQuizShown(true);
|
|
|
|
|
}, autoShowQuizTime * 1000);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
};
|
|
|
|
|
}, [autoShowQuizTime]);
|
2024-05-07 10:09:43 +00:00
|
|
|
|
|
|
|
|
|
function openQuiz() {
|
2024-05-08 12:12:40 +00:00
|
|
|
|
preventQuizAutoShowRef.current = true;
|
2024-05-07 10:09:43 +00:00
|
|
|
|
setIsQuizShown(true);
|
|
|
|
|
setIsFlashEnabled(false);
|
|
|
|
|
}
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
2024-05-08 11:23:15 +00:00
|
|
|
|
if (hideOnMobile && isMobile) return null;
|
|
|
|
|
|
2024-04-24 15:56:11 +00:00
|
|
|
|
return createPortal(
|
|
|
|
|
<ThemeProvider theme={lightTheme}>
|
2024-05-03 19:30:38 +00:00
|
|
|
|
<QuizDialog
|
|
|
|
|
open={isQuizShown}
|
|
|
|
|
quizId={quizId}
|
|
|
|
|
onClose={() => setIsQuizShown(false)}
|
|
|
|
|
hideBackdrop
|
|
|
|
|
disableScrollLock
|
2024-05-04 12:53:56 +00:00
|
|
|
|
paperSx={[
|
|
|
|
|
{
|
|
|
|
|
m: 0,
|
|
|
|
|
},
|
2024-05-07 10:09:43 +00:00
|
|
|
|
!(isMobile || fullScreen) && {
|
2024-05-04 12:53:56 +00:00
|
|
|
|
position: "absolute",
|
|
|
|
|
bottom: PADDING,
|
|
|
|
|
right: position === "right" ? PADDING : undefined,
|
|
|
|
|
left: position === "left" ? PADDING : undefined,
|
2024-05-04 15:12:22 +00:00
|
|
|
|
width: dimensions?.width ?? WIDGET_DEFAULT_WIDTH,
|
|
|
|
|
maxWidth: `calc(100% - ${PADDING * 2}px)`,
|
|
|
|
|
height: dimensions?.height ?? WIDGET_DEFAULT_HEIGHT,
|
|
|
|
|
maxHeight: `calc(100% - ${PADDING * 2}px)`,
|
2024-05-04 12:53:56 +00:00
|
|
|
|
},
|
2024-05-04 15:22:47 +00:00
|
|
|
|
(isMobile || fullScreen) && {
|
2024-05-04 12:53:56 +00:00
|
|
|
|
position: "relative",
|
2024-05-04 15:12:22 +00:00
|
|
|
|
width: "100%",
|
2024-05-04 12:53:56 +00:00
|
|
|
|
height: "100%",
|
|
|
|
|
maxHeight: "100%",
|
|
|
|
|
borderRadius: 0,
|
|
|
|
|
},
|
|
|
|
|
]}
|
2024-05-03 19:30:38 +00:00
|
|
|
|
/>
|
2024-05-08 11:23:15 +00:00
|
|
|
|
<Fade in={!isWidgetHidden} timeout={400}>
|
|
|
|
|
<Button
|
|
|
|
|
className="pena-quiz-widget-button"
|
|
|
|
|
variant="contained"
|
|
|
|
|
onClick={openQuiz}
|
|
|
|
|
disableFocusRipple
|
|
|
|
|
sx={[
|
|
|
|
|
{
|
|
|
|
|
display: isQuizShown ? "none" : "block",
|
|
|
|
|
position: "fixed",
|
|
|
|
|
height: "70px",
|
|
|
|
|
width: "600px",
|
|
|
|
|
maxWidth: `calc(100% - ${PADDING * 2}px)`,
|
|
|
|
|
backgroundColor: buttonBackgroundColor,
|
|
|
|
|
color: buttonTextColor,
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
},
|
|
|
|
|
position === "left" && {
|
|
|
|
|
bottom: PADDING,
|
|
|
|
|
left: PADDING,
|
|
|
|
|
},
|
|
|
|
|
position === "right" && {
|
|
|
|
|
bottom: PADDING,
|
|
|
|
|
right: PADDING,
|
|
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
>
|
|
|
|
|
{!isQuizCompleted && isFlashEnabled && <RunningStripe />}
|
|
|
|
|
Пройти квиз
|
|
|
|
|
</Button>
|
|
|
|
|
</Fade>
|
2024-04-24 15:56:11 +00:00
|
|
|
|
</ThemeProvider>,
|
|
|
|
|
document.body
|
|
|
|
|
);
|
|
|
|
|
}
|