import React, { useEffect, useState, useRef } from 'react'; import { StyledModal } from './styled/CaptchaModal.styled'; import useGeneralStore from '../hooks/stores/useGeneralStore'; import Modal from 'react-modal'; import Draggable from 'react-draggable'; const CaptchaModal = () => { const { challengesArray, pendingComment, selectedStyle, setCaptchaResponse, isCaptchaOpen, setIsCaptchaOpen, selectedShortCid, } = useGeneralStore(state => state); const [imageSources, setImageSources] = useState([]); const [currentChallengeIndex, setCurrentChallengeIndex] = useState(0); const [totalChallenges, setTotalChallenges] = useState(0); const [isMobile, setIsMobile] = useState(window.innerWidth <= 480); const responseRef = useRef(); const nodeRef = useRef(null); useEffect(() => { const handleResize = () => setIsMobile(window.innerWidth <= 480); window.addEventListener('resize', handleResize); // Cleanup function return () => { window.removeEventListener('resize', handleResize); }; }, [setIsMobile]); useEffect(() => { if (challengesArray) { const challenges = challengesArray.challenges; const decryptedChallenges = []; for (let i = 0; i < challenges?.length; i++) { const imageString = challenges[i].challenge; const imageSource = `data:image/png;base64,${imageString}`; decryptedChallenges.push(imageSource); } setImageSources(decryptedChallenges); setTotalChallenges(decryptedChallenges.length); } }, [challengesArray]); const handleKeyDown = (event) => { if (event.key === "Enter") { event.preventDefault(); submitCaptcha(); } }; const handleReturnKeyDown = () => { submitCaptcha((response) => { useGeneralStore.getState().setCaptchaResponse(response); useGeneralStore.getState().resolveCaptchaPromise(response); }); }; const submitCaptcha = (callback) => { setCaptchaResponse(responseRef.current.value); setIsCaptchaOpen(false); if (callback) { callback(responseRef.current.value); } }; return ( setIsCaptchaOpen(false)} contentLabel="Captcha Modal" shouldCloseOnEsc={false} shouldCloseOnOverlayClick={false} selectedStyle={selectedStyle} overlayClassName="hide-modal-overlay">
{pendingComment.parentCid ? ("Challenges for Reply to c/" + selectedShortCid) : "Challenges for New Thread"}
{pendingComment.author?.displayName ? (
) : null} {pendingComment.title ? (
) : null} {pendingComment.content ? (