Files
5chan/src/components/challenge-modal/challenge-modal.tsx
T

177 lines
6.0 KiB
TypeScript
Raw Normal View History

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';
import useIsMobile from '../../hooks/use-is-mobile';
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;
if (!answers[currentChallengeIndex]) return;
2024-04-26 19:13:23 +02:00
if (challenges[currentChallengeIndex + 1]) {
setCurrentChallengeIndex((prev) => prev + 1);
} else {
onSubmit();
}
};
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);
const isMobile = useIsMobile();
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,
},
);
const modalContent = (
2025-03-04 10:59:56 +01:00
<animated.div
className={styles.container}
ref={nodeRef}
style={{
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() : {})}>
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>
{title && (
<div className={styles.subject}>
<input type='text' value={title} disabled />
2024-04-26 19:13:23 +02:00
</div>
)}
{content && (
<div className={styles.content}>
<textarea value={content} disabled cols={48} rows={4} wrap='soft' />
2024-04-26 19:13:23 +02:00
</div>
)}
{link && (
<div className={styles.link}>
<input type='text' value={link} disabled />
2024-04-26 19:13:23 +02:00
</div>
)}
<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}`} />}
</div>
</div>
<div className={styles.challengeFooter}>
<div className={styles.counter}>{t('challenge_counter', { index: currentChallengeIndex + 1, total: challenges?.length })}</div>
<span className={styles.buttons}>
{!challenges[currentChallengeIndex + 1] && (
<button onClick={onSubmit} disabled={!answers[currentChallengeIndex]}>
{t('submit')}
</button>
)}
{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>
);
2025-03-04 10:59:56 +01:00
return modalContent;
2024-04-26 19:13:23 +02:00
};
const ChallengeModal = () => {
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;