import { useRef, useState } from 'react'; import Draggable from 'react-draggable'; import { useTranslation } from 'react-i18next'; import { Challenge as ChallengeType } from '@plebbit/plebbit-react-hooks'; import { getPublicationType } from '../../lib/utils/challenge-utils'; import useChallenges from '../../hooks/use-challenges'; import styles from './challenge-modal.module.css'; import _ from 'lodash'; interface ChallengeProps { challenge: ChallengeType; closeModal: () => void; } const Challenge = ({ challenge, closeModal }: ChallengeProps) => { const { t } = useTranslation(); const challenges = challenge?.[0]?.challenges; const publication = challenge?.[1]; const publicationType = getPublicationType(publication); const { author, content, link, title } = publication || {}; const { displayName } = author || {}; const [currentChallengeIndex, setCurrentChallengeIndex] = useState(0); const [answers, setAnswers] = useState([]); const isTextChallenge = challenges[currentChallengeIndex].type === 'text/plain'; const isImageChallenge = challenges[currentChallengeIndex].type === 'image/png'; const onAnswersChange = (e: React.ChangeEvent) => { setAnswers((prevAnswers) => { const updatedAnswers = [...prevAnswers]; updatedAnswers[currentChallengeIndex] = e.target.value; return updatedAnswers; }); }; const onSubmit = () => { publication.publishChallengeAnswers(answers); setAnswers([]); closeModal(); }; const onEnterKey = (e: React.KeyboardEvent) => { if (e.key !== 'Enter') return; if (challenges[currentChallengeIndex + 1]) { setCurrentChallengeIndex((prev) => prev + 1); } else { onSubmit(); } }; // react-draggable requires a ref to the modal node const nodeRef = useRef(null); return (
Challenge for {publicationType}
{title && (
)} {content && (