frontAnswerer/src/widgets/side/QuizSideButton.tsx

121 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-04-24 15:56:11 +00:00
import lightTheme from "@/utils/themes/light";
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";
import QuizDialog from "../shared/QuizDialog";
import RunningStripe from "../shared/RunningStripe";
import { useAutoOpenTimer } from "../shared/useAutoOpenTimer";
import { useQuizCompletionStatus } from "../shared/useQuizCompletionStatus";
2024-05-13 17:36:27 +00:00
import { SideWidgetComponentProps } from "@/model/widget/side";
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
export default function QuizSideButton({
quizId,
position,
buttonBackgroundColor,
buttonTextColor,
dialogDimensions,
fullScreen = false,
buttonFlash = false,
autoOpenTime = 0,
autoShowQuizTime = null,
hideOnMobile = false,
2024-05-13 17:36:27 +00:00
}: SideWidgetComponentProps) {
2024-04-24 15:56:11 +00:00
const [isQuizShown, setIsQuizShown] = useState<boolean>(false);
const isMobile = useMediaQuery("(max-width: 600px)");
const isQuizCompleted = useQuizCompletionStatus(quizId);
const [isFlashEnabled, setIsFlashEnabled] = useState<boolean>(buttonFlash);
const isWidgetHidden = useAutoOpenTimer(autoOpenTime);
2024-05-08 12:12:40 +00:00
const preventQuizAutoShowRef = useRef<boolean>(false);
useEffect(function setAutoShowQuizTimer() {
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]);
function openQuiz() {
2024-05-08 12:12:40 +00:00
preventQuizAutoShowRef.current = true;
setIsQuizShown(true);
setIsFlashEnabled(false);
}
2024-04-24 15:56:11 +00:00
if (hideOnMobile && isMobile) return null;
2024-04-24 15:56:11 +00:00
return createPortal(
<ThemeProvider theme={lightTheme}>
<QuizDialog
open={isQuizShown}
quizId={quizId}
onClose={() => setIsQuizShown(false)}
hideBackdrop
disableScrollLock
paperSx={[
{
m: 0,
},
!(isMobile || fullScreen) && {
position: "absolute",
bottom: PADDING,
right: position === "right" ? PADDING : undefined,
left: position === "left" ? PADDING : undefined,
width: dialogDimensions?.width ?? WIDGET_DEFAULT_WIDTH,
2024-05-04 15:12:22 +00:00
maxWidth: `calc(100% - ${PADDING * 2}px)`,
height: dialogDimensions?.height ?? WIDGET_DEFAULT_HEIGHT,
2024-05-04 15:12:22 +00:00
maxHeight: `calc(100% - ${PADDING * 2}px)`,
},
2024-05-04 15:22:47 +00:00
(isMobile || fullScreen) && {
position: "relative",
2024-05-04 15:12:22 +00:00
width: "100%",
height: "100%",
maxHeight: "100%",
borderRadius: 0,
},
]}
/>
<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,
},
]}
>
Пройти квиз
2024-05-09 15:16:56 +00:00
{!isQuizCompleted && isFlashEnabled && <RunningStripe />}
</Button>
</Fade>
2024-04-24 15:56:11 +00:00
</ThemeProvider>,
document.body
);
}