580 lines
19 KiB
TypeScript
580 lines
19 KiB
TypeScript
import { useState, useEffect, useCallback, useRef } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { useLanguage } from '../i18n/LanguageContext';
|
|
import { fonts, radius } from '../styles/theme';
|
|
import { getNextQuiz, checkQuizAnswer, getQuizProgress, fetchProgress } from '../api';
|
|
import RulesModal from '../components/RulesModal';
|
|
import { useAchievements } from '../hooks/useAchievements';
|
|
|
|
interface QuizData {
|
|
correct_polish: string;
|
|
translation: string;
|
|
options: string[];
|
|
}
|
|
|
|
const WordQuizPage: React.FC = () => {
|
|
const navigate = useNavigate();
|
|
const { t, language } = useLanguage();
|
|
const [quiz, setQuiz] = useState<QuizData | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [totalScore, setTotalScore] = useState(0);
|
|
const [wordsGuessed, setWordsGuessed] = useState(0);
|
|
const [correctStreak, setCorrectStreak] = useState(0);
|
|
const [mistakes, setMistakes] = useState(0);
|
|
const [selectedAnswer, setSelectedAnswer] = useState<string | null>(null);
|
|
const [isCorrect, setIsCorrect] = useState<boolean | null>(null);
|
|
const [isChecking, setIsChecking] = useState(false);
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
const [showRules, setShowRules] = useState(false);
|
|
const [currentLevel, setCurrentLevel] = useState('A1');
|
|
|
|
const { activeAchievement, showAchievement } = useAchievements();
|
|
|
|
const isMountedRef = useRef(true);
|
|
const rulesShownRef = useRef(false);
|
|
const startTimeRef = useRef<number>(0);
|
|
const maxMistakes = 3;
|
|
|
|
const glass = (extra: React.CSSProperties = {}): React.CSSProperties => ({
|
|
background: 'rgba(255,255,255,0.55)',
|
|
backdropFilter: 'blur(24px) saturate(180%)',
|
|
WebkitBackdropFilter: 'blur(24px) saturate(180%)',
|
|
border: '1px solid rgba(255,255,255,0.7)',
|
|
boxShadow: '0 8px 32px rgba(31,38,73,0.10), inset 0 1px 0 rgba(255,255,255,0.85)',
|
|
...extra,
|
|
});
|
|
|
|
const brandColor = '#2ECC71';
|
|
const palette = {
|
|
wordquiz: '#E8933F',
|
|
};
|
|
|
|
useEffect(() => {
|
|
isMountedRef.current = true;
|
|
|
|
// Автопоказ правил при первом входе
|
|
if (!rulesShownRef.current) {
|
|
rulesShownRef.current = true;
|
|
const rulesSeen = localStorage.getItem('filwords_rules_wordquiz');
|
|
if (!rulesSeen) {
|
|
setTimeout(() => {
|
|
if (isMountedRef.current) {
|
|
setShowRules(true);
|
|
localStorage.setItem('filwords_rules_wordquiz', '1');
|
|
}
|
|
}, 500);
|
|
}
|
|
}
|
|
|
|
return () => {
|
|
isMountedRef.current = false;
|
|
};
|
|
}, []);
|
|
|
|
const fetchQuiz = useCallback(() => {
|
|
setLoading(true);
|
|
setSelectedAnswer(null);
|
|
setIsCorrect(null);
|
|
setErrorMessage(null);
|
|
setMistakes(0);
|
|
startTimeRef.current = Date.now();
|
|
|
|
getNextQuiz()
|
|
.then((data: any) => {
|
|
if (isMountedRef.current) {
|
|
setQuiz(data);
|
|
}
|
|
})
|
|
.catch((err: Error) => {
|
|
console.error('Error fetching quiz:', err);
|
|
if (isMountedRef.current) {
|
|
setErrorMessage('Failed to load quiz');
|
|
}
|
|
})
|
|
.finally(() => {
|
|
if (isMountedRef.current) {
|
|
setLoading(false);
|
|
}
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
fetchQuiz();
|
|
fetchProgress()
|
|
.then((data: any) => {
|
|
if (isMountedRef.current) {
|
|
setTotalScore(data.total_score || 0);
|
|
if (data.current_level) {
|
|
setCurrentLevel(data.current_level);
|
|
}
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
getQuizProgress()
|
|
.then((data: any) => {
|
|
if (isMountedRef.current) {
|
|
setWordsGuessed(data.words_guessed || 0);
|
|
setCorrectStreak(data.correct_streak || 0);
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
}, [fetchQuiz]);
|
|
|
|
const handleAnswer = useCallback(async (answer: string) => {
|
|
if (!quiz || isChecking || isCorrect !== null) return;
|
|
|
|
setSelectedAnswer(answer);
|
|
setIsChecking(true);
|
|
|
|
const completionTime = Math.floor((Date.now() - startTimeRef.current) / 1000);
|
|
|
|
try {
|
|
const data: any = await checkQuizAnswer(quiz.correct_polish, answer, mistakes, completionTime);
|
|
|
|
if (isMountedRef.current) {
|
|
if (data.correct) {
|
|
setIsCorrect(true);
|
|
setTotalScore(prev => prev + (data.score || 0));
|
|
setWordsGuessed(prev => prev + 1);
|
|
setCorrectStreak(data.correct_streak || 0);
|
|
|
|
fetchProgress().catch(() => {});
|
|
|
|
// Показываем достижения
|
|
if (data.new_achievements && data.new_achievements.length > 0) {
|
|
data.new_achievements.forEach((ach: any) => {
|
|
showAchievement({
|
|
id: ach.code,
|
|
icon: ach.icon,
|
|
name: ach.name,
|
|
});
|
|
});
|
|
}
|
|
|
|
if (window.Telegram?.WebApp?.HapticFeedback) {
|
|
window.Telegram.WebApp.HapticFeedback.impactOccurred('heavy');
|
|
}
|
|
|
|
setTimeout(() => {
|
|
if (isMountedRef.current) {
|
|
setMistakes(0);
|
|
fetchQuiz();
|
|
}
|
|
}, 1000);
|
|
} else {
|
|
setIsCorrect(false);
|
|
const newMistakes = mistakes + 1;
|
|
setMistakes(newMistakes);
|
|
setCorrectStreak(0);
|
|
|
|
startTimeRef.current = Date.now();
|
|
|
|
if (window.Telegram?.WebApp?.HapticFeedback) {
|
|
window.Telegram.WebApp.HapticFeedback.impactOccurred('medium');
|
|
}
|
|
|
|
if (newMistakes >= maxMistakes) {
|
|
setTimeout(() => {
|
|
if (isMountedRef.current) {
|
|
setMistakes(0);
|
|
fetchQuiz();
|
|
}
|
|
}, 1500);
|
|
} else {
|
|
setTimeout(() => {
|
|
if (isMountedRef.current) {
|
|
setSelectedAnswer(null);
|
|
setIsCorrect(null);
|
|
}
|
|
}, 800);
|
|
}
|
|
}
|
|
}
|
|
} catch (err: any) {
|
|
console.error('Error checking answer:', err);
|
|
if (isMountedRef.current) {
|
|
setErrorMessage(err.message || 'Error checking answer');
|
|
}
|
|
} finally {
|
|
if (isMountedRef.current) {
|
|
setIsChecking(false);
|
|
}
|
|
}
|
|
}, [quiz, isChecking, isCorrect, mistakes, fetchQuiz, showAchievement]);
|
|
|
|
const getOptionStyle = (option: string): React.CSSProperties => {
|
|
if (isCorrect === true && option === quiz?.correct_polish) {
|
|
return {
|
|
background: `linear-gradient(180deg, ${brandColor}E6, ${brandColor}CC)`,
|
|
border: `2px solid ${brandColor}`,
|
|
color: '#12271C',
|
|
fontWeight: 700,
|
|
boxShadow: `0 8px 24px ${brandColor}50, inset 0 1px 0 rgba(255,255,255,0.4)`,
|
|
};
|
|
}
|
|
if (isCorrect === false && option === selectedAnswer) {
|
|
return {
|
|
background: 'linear-gradient(180deg, #E74C3CE6, #E74C3CCC)',
|
|
border: '2px solid #E74C3C',
|
|
color: '#FFFFFF',
|
|
fontWeight: 700,
|
|
boxShadow: '0 8px 24px rgba(231,76,60,0.35), inset 0 1px 0 rgba(255,255,255,0.3)',
|
|
};
|
|
}
|
|
if (selectedAnswer === option && isChecking) {
|
|
return {
|
|
background: `${palette.wordquiz}25`,
|
|
border: `2px solid ${palette.wordquiz}`,
|
|
color: palette.wordquiz,
|
|
fontWeight: 700,
|
|
};
|
|
}
|
|
return {
|
|
background: 'rgba(255,255,255,0.55)',
|
|
backdropFilter: 'blur(24px) saturate(180%)',
|
|
WebkitBackdropFilter: 'blur(24px) saturate(180%)',
|
|
border: '1px solid rgba(255,255,255,0.7)',
|
|
color: '#1A1A2E',
|
|
fontWeight: 700,
|
|
boxShadow: '0 6px 20px rgba(31,38,73,0.08), inset 0 1px 0 rgba(255,255,255,0.85)',
|
|
};
|
|
};
|
|
|
|
if (loading) {
|
|
return (
|
|
<div style={{
|
|
minHeight: '100vh',
|
|
background: '#F4F5F7',
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
color: '#6B7280',
|
|
fontFamily: fonts.main,
|
|
fontSize: '18px',
|
|
}}>
|
|
{t('loading')}...
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div style={{
|
|
minHeight: '100vh',
|
|
background: '#F4F5F7',
|
|
color: '#1A1A2E',
|
|
fontFamily: fonts.main,
|
|
padding: '24px',
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
position: 'relative',
|
|
overflow: 'hidden',
|
|
}}>
|
|
<div style={{ position: 'fixed', top: '-100px', right: '-80px', width: '280px', height: '280px', borderRadius: '50%', background: palette.wordquiz, opacity: 0.28, filter: 'blur(100px)', pointerEvents: 'none', zIndex: 0 }} />
|
|
<div style={{ position: 'fixed', bottom: '-60px', left: '-60px', width: '240px', height: '240px', borderRadius: '50%', background: brandColor, opacity: 0.22, filter: 'blur(90px)', pointerEvents: 'none', zIndex: 0 }} />
|
|
|
|
<style>{`
|
|
@keyframes fadeInUp {
|
|
0% { transform: translateY(10px); opacity: 0; }
|
|
100% { transform: translateY(0); opacity: 1; }
|
|
}
|
|
@keyframes correctPop {
|
|
0% { transform: scale(1); }
|
|
50% { transform: scale(1.05); }
|
|
100% { transform: scale(1); }
|
|
}
|
|
@keyframes shake {
|
|
0%, 100% { transform: translateX(0); }
|
|
25% { transform: translateX(-5px); }
|
|
75% { transform: translateX(5px); }
|
|
}
|
|
@keyframes firePop {
|
|
0% { transform: scale(0); opacity: 0; }
|
|
60% { transform: scale(1.3); opacity: 1; }
|
|
100% { transform: scale(1); opacity: 1; }
|
|
}
|
|
@keyframes achievementSlideDown {
|
|
0% { transform: translateX(-50%) translateY(-80px); opacity: 0; }
|
|
100% { transform: translateX(-50%) translateY(0); opacity: 1; }
|
|
}
|
|
`}</style>
|
|
|
|
<div style={{ position: 'relative', zIndex: 1, display: 'flex', flexDirection: 'column', flex: 1 }}>
|
|
|
|
<RulesModal
|
|
isOpen={showRules}
|
|
onClose={() => setShowRules(false)}
|
|
title={t('wordQuizRulesTitle') || '🎯 Word Quiz Rules'}
|
|
content={t('wordQuizRulesContent') || 'Rules content loading...'}
|
|
/>
|
|
|
|
{/* Плашка достижения */}
|
|
{activeAchievement && (
|
|
<div style={{
|
|
position: 'fixed', top: '16px', left: '50%', transform: 'translateX(-50%)', zIndex: 900,
|
|
background: 'linear-gradient(135deg, #FFD700, #FFA500)',
|
|
color: '#FFFFFF', padding: '14px 20px', borderRadius: radius.lg,
|
|
boxShadow: '0 8px 30px rgba(255, 215, 0, 0.5)',
|
|
display: 'flex', alignItems: 'center', gap: '12px',
|
|
minWidth: '250px', maxWidth: '90vw', animation: 'achievementSlideDown 0.4s ease',
|
|
}}>
|
|
<span style={{ fontSize: '28px', flexShrink: 0 }}>{activeAchievement.icon}</span>
|
|
<div>
|
|
<div style={{ fontSize: '11px', fontWeight: 600, opacity: 0.9, letterSpacing: '1px', textTransform: 'uppercase' }}>
|
|
🏆 {t('achievement') || 'Achievement'}
|
|
</div>
|
|
<div style={{ fontSize: '15px', fontWeight: 800, marginTop: '2px' }}>
|
|
{activeAchievement.name}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div style={{
|
|
display: 'flex',
|
|
justifyContent: 'space-between',
|
|
alignItems: 'center',
|
|
marginBottom: '24px',
|
|
animation: 'fadeInUp 0.3s ease',
|
|
}}>
|
|
<div style={{ display: 'flex', gap: '8px', alignItems: 'center' }}>
|
|
<button
|
|
onClick={() => navigate('/menu')}
|
|
style={{
|
|
...glass({
|
|
padding: '10px 18px',
|
|
borderRadius: radius.md,
|
|
}),
|
|
color: '#1A1A2E',
|
|
fontSize: '14px',
|
|
fontWeight: 600,
|
|
cursor: 'pointer',
|
|
flexShrink: 0,
|
|
transition: 'transform 0.2s ease',
|
|
}}
|
|
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.95)'; }}
|
|
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
|
|
>
|
|
← {t('back')}
|
|
</button>
|
|
<button
|
|
onClick={() => setShowRules(true)}
|
|
style={{
|
|
...glass({
|
|
width: '40px',
|
|
height: '40px',
|
|
borderRadius: radius.md,
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
}),
|
|
color: '#1A1A2E',
|
|
fontSize: '18px',
|
|
fontWeight: 700,
|
|
cursor: 'pointer',
|
|
transition: 'transform 0.2s ease',
|
|
flexShrink: 0,
|
|
}}
|
|
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.95)'; }}
|
|
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
|
|
>
|
|
?
|
|
</button>
|
|
</div>
|
|
|
|
<div style={{ textAlign: 'right', display: 'flex', alignItems: 'center', gap: '10px' }}>
|
|
<div style={{
|
|
fontSize: '14px',
|
|
fontWeight: 800,
|
|
color: brandColor,
|
|
background: `${brandColor}15`,
|
|
padding: '4px 10px',
|
|
borderRadius: radius.sm,
|
|
border: `1px solid ${brandColor}40`,
|
|
}}>
|
|
{currentLevel}
|
|
</div>
|
|
<div>
|
|
<div style={{ fontSize: '11px', color: '#6B7280', marginBottom: '2px' }}>
|
|
{t('score')}
|
|
</div>
|
|
<div style={{
|
|
fontSize: '18px',
|
|
fontWeight: 800,
|
|
color: brandColor,
|
|
}}>
|
|
{totalScore}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{quiz && (
|
|
<div style={{
|
|
flex: 1,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
gap: '20px',
|
|
animation: 'fadeInUp 0.3s ease 0.15s both',
|
|
}}>
|
|
<div style={{
|
|
...glass({
|
|
padding: '24px',
|
|
borderRadius: radius.lg,
|
|
textAlign: 'center',
|
|
}),
|
|
boxShadow: `0 8px 28px rgba(31,38,73,0.10), 0 0 20px ${brandColor}20, inset 0 1px 0 rgba(255,255,255,0.85)`,
|
|
}}>
|
|
<div style={{
|
|
fontSize: '13px',
|
|
color: '#6B7280',
|
|
marginBottom: '8px',
|
|
fontWeight: 500,
|
|
}}>
|
|
{t('chooseTranslation') || 'Choose the correct translation:'}
|
|
</div>
|
|
<div style={{
|
|
fontSize: '22px',
|
|
fontWeight: 700,
|
|
color: brandColor,
|
|
padding: '10px',
|
|
background: `${brandColor}15`,
|
|
borderRadius: radius.md,
|
|
}}>
|
|
{quiz.translation}
|
|
</div>
|
|
</div>
|
|
|
|
<div style={{
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
gap: '12px',
|
|
}}>
|
|
{quiz.options.map((option, index) => (
|
|
<button
|
|
key={`${option}-${index}`}
|
|
onClick={() => handleAnswer(option)}
|
|
disabled={isChecking || isCorrect === true}
|
|
style={{
|
|
padding: '18px',
|
|
borderRadius: radius.lg,
|
|
fontSize: '17px',
|
|
fontWeight: 700,
|
|
cursor: isChecking || isCorrect === true ? 'default' : 'pointer',
|
|
transition: 'all 0.2s ease',
|
|
animation: isCorrect === false && option === selectedAnswer
|
|
? 'shake 0.4s ease'
|
|
: isCorrect === true && option === quiz.correct_polish
|
|
? 'correctPop 0.3s ease'
|
|
: 'none',
|
|
...getOptionStyle(option),
|
|
}}
|
|
onTouchStart={(e) => {
|
|
if (!isChecking && isCorrect === null) {
|
|
e.currentTarget.style.transform = 'scale(0.96)';
|
|
}
|
|
}}
|
|
onTouchEnd={(e) => {
|
|
e.currentTarget.style.transform = 'scale(1)';
|
|
}}
|
|
>
|
|
{option}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
{errorMessage && (
|
|
<div style={{
|
|
textAlign: 'center',
|
|
padding: '12px',
|
|
background: 'rgba(231,76,60,0.08)',
|
|
borderRadius: radius.md,
|
|
color: '#E74C3C',
|
|
fontSize: '14px',
|
|
fontWeight: 600,
|
|
}}>
|
|
❌ {errorMessage}
|
|
</div>
|
|
)}
|
|
|
|
{mistakes >= maxMistakes && (
|
|
<div style={{
|
|
textAlign: 'center',
|
|
padding: '16px',
|
|
background: 'rgba(231,76,60,0.08)',
|
|
borderRadius: radius.lg,
|
|
border: '2px solid rgba(231,76,60,0.3)',
|
|
}}>
|
|
<div style={{ fontSize: '16px', fontWeight: 700, color: '#E74C3C' }}>
|
|
{t('outOfLives') || 'Out of lives!'}
|
|
</div>
|
|
<div style={{ fontSize: '13px', color: '#6B7280', marginTop: '4px' }}>
|
|
+10 {t('points') || 'points'}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{isCorrect === true && (
|
|
<div style={{
|
|
textAlign: 'center',
|
|
padding: '16px',
|
|
background: `${brandColor}15`,
|
|
borderRadius: radius.lg,
|
|
border: `2px solid ${brandColor}40`,
|
|
}}>
|
|
<div style={{ fontSize: '16px', fontWeight: 700, color: brandColor }}>
|
|
✅ {t('correctAnswer')}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
<div style={{
|
|
...glass({
|
|
display: 'flex',
|
|
justifyContent: 'center',
|
|
gap: '24px',
|
|
marginTop: '20px',
|
|
padding: '12px 16px',
|
|
borderRadius: radius.lg,
|
|
}),
|
|
animation: 'fadeInUp 0.3s ease 0.2s both',
|
|
}}>
|
|
<div style={{ textAlign: 'center', minWidth: '60px' }}>
|
|
<div style={{ fontSize: '10px', color: '#6B7280', marginBottom: '2px' }}>
|
|
{t('wordsGuessed') || 'Угадано'}
|
|
</div>
|
|
<div style={{ fontSize: '18px', fontWeight: 800, color: palette.wordquiz }}>
|
|
{wordsGuessed}
|
|
</div>
|
|
</div>
|
|
<div style={{ textAlign: 'center', minWidth: '60px' }}>
|
|
<div style={{ fontSize: '10px', color: '#6B7280', marginBottom: '2px' }}>
|
|
{language === 'pl' ? 'Seria' : language === 'ru' ? 'Серия' : language === 'uk' ? 'Серія' : 'Streak'}
|
|
</div>
|
|
<div style={{
|
|
fontSize: '18px',
|
|
fontWeight: 800,
|
|
color: correctStreak >= 5 ? brandColor : '#1A1A2E',
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
gap: '4px',
|
|
}}>
|
|
{correctStreak >= 5 && (
|
|
<span style={{
|
|
display: 'inline-block',
|
|
animation: 'firePop 0.3s ease',
|
|
fontSize: '16px',
|
|
}}>🔥</span>
|
|
)}
|
|
{correctStreak}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default WordQuizPage; |