frontAnswerer/src/widgets/button/OpenQuizButton.tsx

138 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-04-24 15:56:11 +00:00
import lightTheme from "@/utils/themes/light";
2024-05-09 11:27:54 +00:00
import { Button, ThemeProvider, useMediaQuery } from "@mui/material";
import { useEffect, useRef, useState } from "react";
import QuizDialog from "../shared/QuizDialog";
2024-05-09 11:27:54 +00:00
import RunningStripe from "../shared/RunningStripe";
import { useQuizCompletionStatus } from "../shared/useQuizCompletionStatus";
2024-04-24 15:56:11 +00:00
2024-05-09 11:27:54 +00:00
const WIDGET_DEFAULT_WIDTH = "600px";
const WIDGET_DEFAULT_HEIGHT = "80%";
2024-04-24 15:56:11 +00:00
interface Props {
quizId: string;
2024-05-09 11:27:54 +00:00
fixedSide?: "left" | "right";
dialogDimensions?: { width: string; height: string; };
2024-05-09 11:27:54 +00:00
/**
* Открыть квиз через X секунд, 0 - сразу
2024-05-09 11:27:54 +00:00
*/
autoShowQuizTime?: number | null;
2024-05-09 11:27:54 +00:00
hideOnMobile?: boolean;
openOnLeaveAttempt?: boolean;
buttonFlash?: boolean;
withShadow?: boolean;
rounded?: boolean;
buttonText?: string;
buttonTextColor?: string;
buttonBackgroundColor?: string;
2024-04-24 15:56:11 +00:00
}
2024-05-09 11:27:54 +00:00
export default function OpenQuizButton({
quizId,
fixedSide,
autoShowQuizTime = null,
dialogDimensions,
2024-05-09 11:27:54 +00:00
hideOnMobile,
openOnLeaveAttempt,
buttonFlash = false,
withShadow = false,
rounded = false,
buttonText = "Пройти квиз",
buttonTextColor,
buttonBackgroundColor,
}: Props) {
const isMobile = useMediaQuery("(max-width: 600px)");
const [isQuizShown, setIsQuizShown] = useState<boolean>(false);
const isQuizCompleted = useQuizCompletionStatus(quizId);
const [isFlashEnabled, setIsFlashEnabled] = useState<boolean>(buttonFlash);
const preventQuizAutoShowRef = useRef<boolean>(false);
const preventOpenOnLeaveAttemptRef = useRef<boolean>(false);
useEffect(function setAutoShowQuizTimer() {
if (autoShowQuizTime === null || openOnLeaveAttempt) return;
2024-05-09 11:27:54 +00:00
const timeout = setTimeout(() => {
setIsQuizShown(true);
}, autoShowQuizTime * 1000);
return () => {
clearTimeout(timeout);
};
}, [autoShowQuizTime, openOnLeaveAttempt]);
useEffect(function attachLeaveListener() {
if (!openOnLeaveAttempt) return;
const handleMouseLeave = () => {
if (!preventOpenOnLeaveAttemptRef.current) {
preventOpenOnLeaveAttemptRef.current = true;
setIsQuizShown(true);
}
};
document.addEventListener("mouseleave", handleMouseLeave);
return () => {
document.removeEventListener("mouseleave", handleMouseLeave);
};
}, [openOnLeaveAttempt]);
function openQuiz() {
preventQuizAutoShowRef.current = true;
setIsQuizShown(true);
setIsFlashEnabled(false);
}
if (hideOnMobile && isMobile) return null;
2024-04-24 15:56:11 +00:00
return (
2024-04-24 15:56:11 +00:00
<ThemeProvider theme={lightTheme}>
<Button
className="pena-quiz-widget-button"
2024-05-09 11:27:54 +00:00
onClick={openQuiz}
2024-04-24 15:56:11 +00:00
variant="contained"
2024-05-09 11:27:54 +00:00
disableFocusRipple
2024-04-24 15:56:11 +00:00
sx={[
{
2024-05-09 11:27:54 +00:00
overflow: "hidden",
color: buttonTextColor,
backgroundColor: buttonBackgroundColor,
},
withShadow && {
boxShadow: "0px 0px 8px 0px rgba(0, 0, 0, 0.7)",
},
!rounded && {
borderRadius: 0,
2024-04-24 15:56:11 +00:00
},
Boolean(fixedSide) && {
position: "fixed",
bottom: "50%",
},
fixedSide === "left" && {
left: 0,
transformOrigin: "left",
transform: "rotate(-90deg) translateY(50%) translateX(-50%)",
},
fixedSide === "right" && {
right: 0,
transformOrigin: "right",
transform: "rotate(-90deg) translateY(-50%) translateX(50%)",
},
]}
>
2024-05-09 15:16:56 +00:00
{buttonText}
2024-05-09 11:27:54 +00:00
{!isQuizCompleted && isFlashEnabled && <RunningStripe />}
2024-04-24 15:56:11 +00:00
</Button>
<QuizDialog
2024-05-09 11:27:54 +00:00
open={isQuizShown}
2024-04-24 15:56:11 +00:00
quizId={quizId}
2024-05-09 11:27:54 +00:00
onClose={() => setIsQuizShown(false)}
paperSx={{
width: dialogDimensions?.width ?? WIDGET_DEFAULT_WIDTH,
height: dialogDimensions?.height ?? WIDGET_DEFAULT_HEIGHT,
2024-05-09 11:27:54 +00:00
}}
2024-04-24 15:56:11 +00:00
/>
</ThemeProvider>
2024-04-24 15:56:11 +00:00
);
}