2025-01-24 13:03:07 +01:00
|
|
|
import { useRef, useState, useEffect } from 'react';
|
2024-04-26 19:13:23 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-28 17:12:19 +02:00
|
|
|
import { Challenge as ChallengeType } from '@plebbit/plebbit-react-hooks';
|
|
|
|
|
import { getPublicationType } from '../../lib/utils/challenge-utils';
|
2024-05-29 16:17:37 +02:00
|
|
|
import useIsMobile from '../../hooks/use-is-mobile';
|
2024-05-31 10:28:57 +02:00
|
|
|
import useChallengesStore from '../../stores/use-challenges-store';
|
2024-04-26 19:13:23 +02:00
|
|
|
import styles from './challenge-modal.module.css';
|
2024-04-28 20:08:41 +02:00
|
|
|
import _ from 'lodash';
|
2025-03-04 10:59:56 +01:00
|
|
|
import { useSpring, animated } from '@react-spring/web';
|
|
|
|
|
import { useDrag } from '@use-gesture/react';
|
2024-04-26 19:13:23 +02:00
|
|
|
|
|
|
|
|
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<string[]>([]);
|
|
|
|
|
|
|
|
|
|
const isTextChallenge = challenges[currentChallengeIndex].type === 'text/plain';
|
|
|
|
|
const isImageChallenge = challenges[currentChallengeIndex].type === 'image/png';
|
|
|
|
|
|
|
|
|
|
const onAnswersChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
setAnswers((prevAnswers) => {
|
|
|
|
|
const updatedAnswers = [...prevAnswers];
|
|
|
|
|
updatedAnswers[currentChallengeIndex] = e.target.value;
|
|
|
|
|
return updatedAnswers;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmit = () => {
|
|
|
|
|
publication.publishChallengeAnswers(answers);
|
|
|
|
|
setAnswers([]);
|
|
|
|
|
closeModal();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onEnterKey = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
|
if (e.key !== 'Enter') return;
|
2025-01-24 15:49:54 +01:00
|
|
|
if (!answers[currentChallengeIndex]) return;
|
2024-04-26 19:13:23 +02:00
|
|
|
if (challenges[currentChallengeIndex + 1]) {
|
|
|
|
|
setCurrentChallengeIndex((prev) => prev + 1);
|
|
|
|
|
} else {
|
|
|
|
|
onSubmit();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-24 13:03:07 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
const onEscapeKey = (e: KeyboardEvent) => {
|
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
|
closeModal();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener('keydown', onEscapeKey);
|
|
|
|
|
return () => document.removeEventListener('keydown', onEscapeKey);
|
|
|
|
|
}, [closeModal]);
|
|
|
|
|
|
2025-03-04 10:59:56 +01:00
|
|
|
const nodeRef = useRef<HTMLDivElement>(null);
|
2024-05-29 16:17:37 +02:00
|
|
|
const isMobile = useIsMobile();
|
2024-04-28 19:01:53 +02:00
|
|
|
|
2025-03-04 10:59:56 +01:00
|
|
|
const [{ x, y }, api] = useSpring(() => ({
|
|
|
|
|
x: window.innerWidth / 2 - 150,
|
2025-03-04 15:59:32 +01:00
|
|
|
y: window.innerHeight / 2 - 200,
|
2025-03-04 10:59:56 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const bind = useDrag(
|
|
|
|
|
({ active, event, offset: [ox, oy] }) => {
|
|
|
|
|
if (active) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
document.body.style.userSelect = 'none';
|
|
|
|
|
document.body.style.webkitUserSelect = 'none';
|
|
|
|
|
} else {
|
|
|
|
|
document.body.style.userSelect = '';
|
|
|
|
|
document.body.style.webkitUserSelect = '';
|
|
|
|
|
}
|
|
|
|
|
api.start({ x: ox, y: oy, immediate: true });
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: () => [x.get(), y.get()],
|
|
|
|
|
filterTaps: true,
|
|
|
|
|
bounds: undefined,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2024-04-29 15:30:40 +02:00
|
|
|
const modalContent = (
|
2025-03-04 10:59:56 +01:00
|
|
|
<animated.div
|
|
|
|
|
className={styles.container}
|
|
|
|
|
ref={nodeRef}
|
|
|
|
|
style={{
|
2025-06-04 12:59:06 +02:00
|
|
|
x: isMobile ? window.innerWidth / 2 - 150 : x,
|
|
|
|
|
y: isMobile ? window.innerHeight / 2 - 200 : y,
|
2025-03-04 10:59:56 +01:00
|
|
|
touchAction: 'none',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div className={`challengeHandle ${styles.title}`} {...(!isMobile ? bind() : {})}>
|
2024-04-29 15:30:40 +02:00
|
|
|
Challenge for {publicationType}
|
|
|
|
|
<button className={styles.closeIcon} onClick={closeModal} title='close' />
|
|
|
|
|
</div>
|
|
|
|
|
<div className={styles.publication}>
|
|
|
|
|
<div className={styles.name}>
|
|
|
|
|
<input type='text' value={displayName || _.capitalize(t('anonymous'))} disabled />
|
2024-04-26 19:13:23 +02:00
|
|
|
</div>
|
2024-04-29 15:30:40 +02:00
|
|
|
{title && (
|
|
|
|
|
<div className={styles.subject}>
|
|
|
|
|
<input type='text' value={title} disabled />
|
2024-04-26 19:13:23 +02:00
|
|
|
</div>
|
2024-04-29 15:30:40 +02:00
|
|
|
)}
|
|
|
|
|
{content && (
|
|
|
|
|
<div className={styles.content}>
|
|
|
|
|
<textarea value={content} disabled cols={48} rows={4} wrap='soft' />
|
2024-04-26 19:13:23 +02:00
|
|
|
</div>
|
2024-04-29 15:30:40 +02:00
|
|
|
)}
|
|
|
|
|
{link && (
|
|
|
|
|
<div className={styles.link}>
|
|
|
|
|
<input type='text' value={link} disabled />
|
2024-04-26 19:13:23 +02:00
|
|
|
</div>
|
2024-04-29 15:30:40 +02:00
|
|
|
)}
|
|
|
|
|
<div className={styles.challengeContainer}>
|
|
|
|
|
<input
|
|
|
|
|
className={styles.challengeAnswer}
|
|
|
|
|
type='text'
|
|
|
|
|
autoComplete='off'
|
|
|
|
|
autoCorrect='off'
|
|
|
|
|
spellCheck='false'
|
|
|
|
|
placeholder='TYPE THE ANSWER HERE AND PRESS ENTER'
|
|
|
|
|
onKeyDown={onEnterKey}
|
|
|
|
|
onChange={onAnswersChange}
|
|
|
|
|
value={answers[currentChallengeIndex] || ''}
|
|
|
|
|
autoFocus
|
|
|
|
|
/>
|
|
|
|
|
<div className={styles.challengeMediaWrapper}>
|
|
|
|
|
{isTextChallenge && <div className={styles.challengeMedia}>{challenges[currentChallengeIndex]?.challenge}</div>}
|
2024-05-06 21:40:17 +02:00
|
|
|
{isImageChallenge && <img alt='' className={styles.challengeMedia} src={`data:image/png;base64,${challenges[currentChallengeIndex]?.challenge}`} />}
|
2024-04-29 15:30:40 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={styles.challengeFooter}>
|
|
|
|
|
<div className={styles.counter}>{t('challenge_counter', { index: currentChallengeIndex + 1, total: challenges?.length })}</div>
|
|
|
|
|
<span className={styles.buttons}>
|
2025-01-24 15:49:54 +01:00
|
|
|
{!challenges[currentChallengeIndex + 1] && (
|
|
|
|
|
<button onClick={onSubmit} disabled={!answers[currentChallengeIndex]}>
|
|
|
|
|
{t('submit')}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
2024-04-29 15:30:40 +02:00
|
|
|
{challenges.length > 1 && (
|
|
|
|
|
<button disabled={!challenges[currentChallengeIndex - 1]} onClick={() => setCurrentChallengeIndex((prev) => prev - 1)}>
|
|
|
|
|
{t('previous')}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
{challenges[currentChallengeIndex + 1] && <button onClick={() => setCurrentChallengeIndex((prev) => prev + 1)}>{t('next')}</button>}
|
|
|
|
|
</span>
|
2024-04-26 19:13:23 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-03-04 10:59:56 +01:00
|
|
|
</animated.div>
|
2024-04-29 15:30:40 +02:00
|
|
|
);
|
|
|
|
|
|
2025-03-04 10:59:56 +01:00
|
|
|
return modalContent;
|
2024-04-26 19:13:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ChallengeModal = () => {
|
2024-05-31 10:28:57 +02:00
|
|
|
const { challenges, removeChallenge } = useChallengesStore();
|
2024-04-26 19:13:23 +02:00
|
|
|
const isOpen = !!challenges.length;
|
|
|
|
|
const closeModal = () => removeChallenge();
|
|
|
|
|
|
|
|
|
|
return isOpen && <Challenge challenge={challenges[0]} closeModal={closeModal} />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ChallengeModal;
|