feat(teacher): режим flip-карточек для уроков (22.09.2026)

This commit is contained in:
2026-09-22 14:40:04 +03:00
parent 3f9d812ecc
commit b3216d97f3
3 changed files with 965 additions and 325 deletions
+385 -325
View File
@@ -19,6 +19,8 @@ import {
import TeacherMenuModal from '../components/TeacherMenuModal';
import TypingModal from '../components/TypingModal';
import PronunciationModal from '../components/PronunciationModal';
import LessonCardStack from '../components/LessonCardStack';
import { type LessonCardData } from '../components/LessonCard';
interface TeacherMessage {
id: number;
@@ -85,6 +87,11 @@ const TeacherChatPage: React.FC = () => {
const [showTyping, setShowTyping] = useState(false);
const [showPronunciation, setShowPronunciation] = useState(false);
// 🔧 НОВОЕ (22.09.2026): режим карточек для уроков.
// Если lessonCards !== null → рендерим LessonCardStack вместо ленты.
const [lessonCards, setLessonCards] = useState<LessonCardData[] | null>(null);
const [cardSessionId, setCardSessionId] = useState<number | null>(null);
const [explanationData, setExplanationData] = useState<{
wrongAnswer: string;
correctAnswer: string;
@@ -381,44 +388,41 @@ const TeacherChatPage: React.FC = () => {
try {
const data = await teacherStart();
if (data.session_id) setSessionId(data.session_id);
if (data.session_id) {
setSessionId(data.session_id);
setCardSessionId(data.session_id);
}
if (data.lesson_id) setCurrentLessonId(data.lesson_id);
if (data.topic_id) setCurrentTopicId(data.topic_id);
if (data.topic_name) setCurrentTopicName(data.topic_name);
// 🔧 НОВОЕ (22.09.2026): собираем массив карточек.
if (data.question) {
const lessonCardMsg: TeacherMessage = {
id: idRef.current++,
type: 'bot',
text: data.text,
topicId: data.topic_id,
topicName: data.topic_name,
hasPolishText: hasPolishContent(data.text),
isLessonCard: true,
};
setMessages(prev => [...prev, lessonCardMsg]);
const cards: LessonCardData[] = [];
await new Promise(resolve => setTimeout(resolve, 400));
// 1. Титульная
cards.push({
type: 'intro',
levelCode: 'A1', // TODO: подтянуть из data
topicName: data.topic_name || '',
lessonNumber: 1, // TODO: подтянуть из data
lessonTitle: data.text.split('\n')[0] || '',
explanation: data.text, // временно весь text
examples: [],
});
const questionMsg: TeacherMessage = {
id: idRef.current++,
type: 'bot',
text: getLocalized(data.question.question, 'prompt'),
isQuestion: true,
questionType: data.question.question.question_type,
// 2. Первый вопрос
cards.push({
type: 'question',
questionId: data.question.question.question_id,
questionType: data.question.question.question_type,
prompt: getLocalized(data.question.question, 'prompt'),
options: data.question.options,
optionsLanguage: data.question.question.options_language || 'pl',
sessionId: data.session_id,
lessonId: data.lesson_id,
topicId: data.topic_id,
topicName: data.topic_name,
questionIndex: data.question_index,
totalQuestions: data.total_questions,
};
});
setLessonCards(cards);
setCurrentQuestionId(data.question.question.question_id);
setMessages(prev => [...prev, questionMsg]);
setIsStarted(true);
} else {
const botMessage: TeacherMessage = {
@@ -438,6 +442,47 @@ const TeacherChatPage: React.FC = () => {
}
};
// 🔧 НОВОЕ (22.09.2026): обработка ответа в режиме карточек.
const handleCardAnswer = async (questionId: string, answer: string, _hintUsed: boolean) => {
if (!cardSessionId) return;
try {
const timeTaken = Date.now() - startTimeRef.current;
startTimeRef.current = Date.now();
const data = await teacherAnswer(cardSessionId, questionId, answer, timeTaken);
// Если сессия завершена — добавляем result-карточку
if (data.session_completed && lessonCards) {
const stats = data.session_stats || { total: 0, correct_count: 0 };
const resultCard: LessonCardData = {
type: 'result',
resultText: data.reaction || 'Отличная работа!',
correctCount: stats.correct_count,
totalCount: stats.total,
};
setLessonCards(prev => prev ? [...prev, resultCard] : [resultCard]);
return;
}
// Если есть следующий вопрос — добавляем карточку
if (data.question && lessonCards) {
const nextCard: LessonCardData = {
type: 'question',
questionId: data.question.question.question_id,
questionType: data.question.question.question_type,
prompt: getLocalized(data.question.question, 'prompt'),
options: data.question.options,
optionsLanguage: data.question.question.options_language || 'pl',
};
setLessonCards(prev => prev ? [...prev, nextCard] : [nextCard]);
setCurrentQuestionId(data.question.question.question_id);
}
} catch (e) {
console.error('Error in handleCardAnswer:', e);
}
};
const handleAnswer = async (answer: string) => {
if (!sessionId || !currentQuestionId || isLoading) return;
@@ -1067,327 +1112,342 @@ const TeacherChatPage: React.FC = () => {
)}
</div>
{/* Сообщения */}
<div style={{
flex: 1,
overflowY: 'auto',
overflowX: 'hidden',
display: 'flex',
flexDirection: 'column',
gap: '10px',
paddingBottom: '12px',
WebkitOverflowScrolling: 'touch',
minHeight: 0,
}}>
{messages.length === 0 && !isStarted && (
<div style={{ textAlign: 'center', padding: '40px 20px', animation: 'fadeInUp 0.3s ease' }}>
<div style={{ fontSize: '64px', marginBottom: '16px' }}>🎓</div>
<p style={{ fontSize: '16px', color: subColor, marginBottom: '24px' }}>
{t('teacherWelcome')}
</p>
<button
onClick={handleStart}
disabled={isLoading}
style={{
padding: '16px 32px',
borderRadius: radius.lg,
border: 'none',
background: `linear-gradient(135deg, ${teacherColor}, #6B46C1)`,
color: '#FFFFFF',
fontSize: '16px',
fontWeight: 700,
cursor: 'pointer',
boxShadow: `0 4px 15px ${teacherColor}40`,
marginBottom: '10px',
width: '100%',
maxWidth: '280px',
}}
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
{t('teacherStart')}
</button>
<button
onClick={() => setShowMenuModal(true)}
disabled={isLoading}
style={{
...glass({ padding: '14px 24px', borderRadius: radius.lg }),
color: textColor,
fontSize: '14px',
fontWeight: 600,
cursor: 'pointer',
width: '100%',
maxWidth: '280px',
}}
>
☰ {t('teacherMenu')}
</button>
</div>
)}
{showContinueButton && (
<div style={{ textAlign: 'center', padding: '16px' }}>
<button
onClick={handleStart}
disabled={isLoading}
style={{
padding: '12px 24px',
borderRadius: radius.md,
border: 'none',
background: `linear-gradient(135deg, ${teacherColor}, #6B46C1)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: 'pointer',
boxShadow: `0 4px 12px ${teacherColor}40`,
}}
>
{t('teacherContinue')} →
</button>
</div>
)}
{messages.map((msg) => (
<div
key={msg.id}
style={{
alignSelf: msg.type === 'user' ? 'flex-end' : msg.type === 'system' ? 'center' : 'flex-start',
maxWidth: '85%',
position: 'relative',
{/* 🔧 НОВОЕ (22.09.2026): если урок в режиме карточек — рендерим LessonCardStack */}
{lessonCards ? (
<div style={{ flex: 1, minHeight: 0 }}>
<LessonCardStack
cards={lessonCards}
onAnswer={handleCardAnswer}
onComplete={() => {
setLessonCards(null);
setCardSessionId(null);
setIsStarted(false);
handleStart();
}}
>
<div
style={{
padding: '12px 16px',
borderRadius: msg.type === 'user' ? '16px 16px 4px 16px' : msg.type === 'system' ? '16px' : '16px 16px 16px 4px',
background: msg.type === 'user'
? `linear-gradient(135deg, ${brandColor}, #27AE60)`
: msg.type === 'system'
? `${teacherColor}20`
: 'rgba(255,255,255,0.8)',
color: msg.type === 'user' ? '#FFFFFF' : textColor,
fontSize: '14px',
lineHeight: '1.5',
whiteSpace: 'pre-line',
border: msg.type === 'user' ? 'none' : `1px solid ${msg.type === 'system' ? teacherColor : 'rgba(0,0,0,0.06)'}`,
boxShadow: msg.type === 'user' ? `0 2px 10px ${brandColor}30` : '0 2px 8px rgba(31,38,73,0.06)',
animation: 'messageIn 0.3s ease',
wordBreak: 'break-word',
}}
>
{msg.text}
</div>
{msg.type === 'bot' && !msg.isQuestion && msg.hasPolishText && (
<button
onClick={() => playText(msg.text, msg.id)}
style={{
position: 'absolute',
bottom: '-8px',
right: '-4px',
width: '24px',
height: '24px',
borderRadius: '50%',
border: `1px solid ${brandColor}40`,
background: isPlaying === msg.id ? `${brandColor}30` : 'rgba(255,255,255,0.9)',
color: brandColor,
fontSize: '10px',
cursor: 'pointer',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
animation: isPlaying === msg.id ? 'pulse 0.5s ease infinite' : 'none',
boxShadow: '0 2px 8px rgba(31,38,73,0.08)',
}}
>
🔊
</button>
)}
{msg.isQuestion && msg.options && msg.options.length > 0 && (
<div style={{ display: 'flex', flexDirection: 'column', gap: '8px', marginTop: '10px' }}>
{msg.options.map((option) => (
<button
key={option.option_key}
onClick={() => handleOptionClick(option)}
disabled={isLoading}
style={{
padding: '12px 16px',
borderRadius: radius.md,
border: `1px solid ${teacherColor}30`,
background: `${teacherColor}10`,
color: textColor,
fontSize: '14px',
fontWeight: 600,
cursor: 'pointer',
transition: 'all 0.2s ease',
textAlign: 'left',
}}
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.97)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
{msg.optionsLanguage === 'user' ? getLocalized(option, 'text') : option.text_pl}
</button>
))}
</div>
)}
{msg.commands && msg.commands.length > 0 && (
<div style={{
display: 'flex',
flexDirection: 'column',
gap: '8px',
marginTop: '12px',
width: '100%',
maxWidth: '280px',
}}>
{msg.commands.map((cmd) => {
if (cmd === '/show-answer') {
return (
<button
key={cmd}
onClick={() => handleCommand(cmd)}
disabled={isLoading}
style={{
width: '100%',
padding: '14px 20px',
borderRadius: radius.lg,
border: 'none',
background: `linear-gradient(135deg, ${teacherColor}, #6B46C1)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: 'pointer',
boxShadow: `0 4px 15px ${teacherColor}40`,
transition: 'all 0.2s ease',
textAlign: 'center',
}}
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
✅ {t('teacherShowAnswer')}
</button>
);
}
if (cmd === '/examples') {
return (
<button
key={cmd}
onClick={() => handleCommand(cmd)}
disabled={isLoading || isLoadingExamples}
style={{
width: '100%',
padding: '14px 20px',
borderRadius: radius.lg,
border: 'none',
background: isLoadingExamples
? 'rgba(0,0,0,0.1)'
: `linear-gradient(135deg, #4FA9E8, #3B82F6)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: isLoadingExamples ? 'default' : 'pointer',
boxShadow: isLoadingExamples ? 'none' : '0 4px 15px rgba(79,169,232,0.4)',
transition: 'all 0.2s ease',
textAlign: 'center',
}}
onTouchStart={(e) => { if (!isLoadingExamples) e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
💡 {t('testExamplesButton')}
</button>
);
}
if (cmd === '/practice') {
return (
<button
key={cmd}
onClick={() => handleCommand(cmd)}
disabled={isLoading || isStartingPractice}
style={{
width: '100%',
padding: '14px 20px',
borderRadius: radius.lg,
border: 'none',
background: isStartingPractice
? 'rgba(0,0,0,0.1)'
: `linear-gradient(135deg, ${brandColor}, #27AE60)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: isStartingPractice ? 'default' : 'pointer',
boxShadow: isStartingPractice ? 'none' : `0 4px 15px ${brandColor}40`,
transition: 'all 0.2s ease',
textAlign: 'center',
}}
onTouchStart={(e) => { if (!isStartingPractice) e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
🎯 {t('testPracticeButton')}
</button>
);
}
return null;
})}
</div>
)}
{msg.isCompleted && (
/>
</div>
) : (
<div style={{
flex: 1,
overflowY: 'auto',
overflowX: 'hidden',
display: 'flex',
flexDirection: 'column',
gap: '10px',
paddingBottom: '12px',
WebkitOverflowScrolling: 'touch',
minHeight: 0,
}}>
{messages.length === 0 && !isStarted && (
<div style={{ textAlign: 'center', padding: '40px 20px', animation: 'fadeInUp 0.3s ease' }}>
<div style={{ fontSize: '64px', marginBottom: '16px' }}>🎓</div>
<p style={{ fontSize: '16px', color: subColor, marginBottom: '24px' }}>
{t('teacherWelcome')}
</p>
<button
onClick={handleStart}
disabled={isLoading}
style={{
marginTop: '10px',
padding: '14px 20px',
padding: '16px 32px',
borderRadius: radius.lg,
border: 'none',
background: `linear-gradient(135deg, ${teacherColor}, #6B46C1)`,
color: '#FFFFFF',
fontSize: '14px',
fontSize: '16px',
fontWeight: 700,
cursor: 'pointer',
boxShadow: `0 4px 15px ${teacherColor}40`,
transition: 'all 0.2s ease',
marginBottom: '10px',
width: '100%',
maxWidth: '280px',
}}
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
{t('teacherNewLesson')} →
{t('teacherStart')}
</button>
)}
</div>
))}
{isLoading && (
<div style={{
alignSelf: 'flex-start',
padding: '10px 14px',
borderRadius: '16px 16px 16px 4px',
background: 'rgba(255,255,255,0.8)',
border: '1px solid rgba(0,0,0,0.06)',
display: 'flex',
gap: '4px',
alignItems: 'center',
}}>
{[0, 1, 2].map((i) => (
<div
key={i}
<button
onClick={() => setShowMenuModal(true)}
disabled={isLoading}
style={{
width: '6px',
height: '6px',
borderRadius: '50%',
background: teacherColor,
animation: `typingDot 0.8s ease infinite ${i * 0.15}s`,
...glass({ padding: '14px 24px', borderRadius: radius.lg }),
color: textColor,
fontSize: '14px',
fontWeight: 600,
cursor: 'pointer',
width: '100%',
maxWidth: '280px',
}}
/>
))}
</div>
)}
>
☰ {t('teacherMenu')}
</button>
</div>
)}
<div ref={messagesEndRef} />
</div>
{showContinueButton && (
<div style={{ textAlign: 'center', padding: '16px' }}>
<button
onClick={handleStart}
disabled={isLoading}
style={{
padding: '12px 24px',
borderRadius: radius.md,
border: 'none',
background: `linear-gradient(135deg, ${teacherColor}, #6B46C1)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: 'pointer',
boxShadow: `0 4px 12px ${teacherColor}40`,
}}
>
{t('teacherContinue')} →
</button>
</div>
)}
{messages.map((msg) => (
<div
key={msg.id}
style={{
alignSelf: msg.type === 'user' ? 'flex-end' : msg.type === 'system' ? 'center' : 'flex-start',
maxWidth: '85%',
position: 'relative',
}}
>
<div
style={{
padding: '12px 16px',
borderRadius: msg.type === 'user' ? '16px 16px 4px 16px' : msg.type === 'system' ? '16px' : '16px 16px 16px 4px',
background: msg.type === 'user'
? `linear-gradient(135deg, ${brandColor}, #27AE60)`
: msg.type === 'system'
? `${teacherColor}20`
: 'rgba(255,255,255,0.8)',
color: msg.type === 'user' ? '#FFFFFF' : textColor,
fontSize: '14px',
lineHeight: '1.5',
whiteSpace: 'pre-line',
border: msg.type === 'user' ? 'none' : `1px solid ${msg.type === 'system' ? teacherColor : 'rgba(0,0,0,0.06)'}`,
boxShadow: msg.type === 'user' ? `0 2px 10px ${brandColor}30` : '0 2px 8px rgba(31,38,73,0.06)',
animation: 'messageIn 0.3s ease',
wordBreak: 'break-word',
}}
>
{msg.text}
</div>
{msg.type === 'bot' && !msg.isQuestion && msg.hasPolishText && (
<button
onClick={() => playText(msg.text, msg.id)}
style={{
position: 'absolute',
bottom: '-8px',
right: '-4px',
width: '24px',
height: '24px',
borderRadius: '50%',
border: `1px solid ${brandColor}40`,
background: isPlaying === msg.id ? `${brandColor}30` : 'rgba(255,255,255,0.9)',
color: brandColor,
fontSize: '10px',
cursor: 'pointer',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
animation: isPlaying === msg.id ? 'pulse 0.5s ease infinite' : 'none',
boxShadow: '0 2px 8px rgba(31,38,73,0.08)',
}}
>
🔊
</button>
)}
{msg.isQuestion && msg.options && msg.options.length > 0 && (
<div style={{ display: 'flex', flexDirection: 'column', gap: '8px', marginTop: '10px' }}>
{msg.options.map((option) => (
<button
key={option.option_key}
onClick={() => handleOptionClick(option)}
disabled={isLoading}
style={{
padding: '12px 16px',
borderRadius: radius.md,
border: `1px solid ${teacherColor}30`,
background: `${teacherColor}10`,
color: textColor,
fontSize: '14px',
fontWeight: 600,
cursor: 'pointer',
transition: 'all 0.2s ease',
textAlign: 'left',
}}
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.97)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
{msg.optionsLanguage === 'user' ? getLocalized(option, 'text') : option.text_pl}
</button>
))}
</div>
)}
{msg.commands && msg.commands.length > 0 && (
<div style={{
display: 'flex',
flexDirection: 'column',
gap: '8px',
marginTop: '12px',
width: '100%',
maxWidth: '280px',
}}>
{msg.commands.map((cmd) => {
if (cmd === '/show-answer') {
return (
<button
key={cmd}
onClick={() => handleCommand(cmd)}
disabled={isLoading}
style={{
width: '100%',
padding: '14px 20px',
borderRadius: radius.lg,
border: 'none',
background: `linear-gradient(135deg, ${teacherColor}, #6B46C1)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: 'pointer',
boxShadow: `0 4px 15px ${teacherColor}40`,
transition: 'all 0.2s ease',
textAlign: 'center',
}}
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
✅ {t('teacherShowAnswer')}
</button>
);
}
if (cmd === '/examples') {
return (
<button
key={cmd}
onClick={() => handleCommand(cmd)}
disabled={isLoading || isLoadingExamples}
style={{
width: '100%',
padding: '14px 20px',
borderRadius: radius.lg,
border: 'none',
background: isLoadingExamples
? 'rgba(0,0,0,0.1)'
: `linear-gradient(135deg, #4FA9E8, #3B82F6)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: isLoadingExamples ? 'default' : 'pointer',
boxShadow: isLoadingExamples ? 'none' : '0 4px 15px rgba(79,169,232,0.4)',
transition: 'all 0.2s ease',
textAlign: 'center',
}}
onTouchStart={(e) => { if (!isLoadingExamples) e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
💡 {t('testExamplesButton')}
</button>
);
}
if (cmd === '/practice') {
return (
<button
key={cmd}
onClick={() => handleCommand(cmd)}
disabled={isLoading || isStartingPractice}
style={{
width: '100%',
padding: '14px 20px',
borderRadius: radius.lg,
border: 'none',
background: isStartingPractice
? 'rgba(0,0,0,0.1)'
: `linear-gradient(135deg, ${brandColor}, #27AE60)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: isStartingPractice ? 'default' : 'pointer',
boxShadow: isStartingPractice ? 'none' : `0 4px 15px ${brandColor}40`,
transition: 'all 0.2s ease',
textAlign: 'center',
}}
onTouchStart={(e) => { if (!isStartingPractice) e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
🎯 {t('testPracticeButton')}
</button>
);
}
return null;
})}
</div>
)}
{msg.isCompleted && (
<button
onClick={handleStart}
disabled={isLoading}
style={{
marginTop: '10px',
padding: '14px 20px',
borderRadius: radius.lg,
border: 'none',
background: `linear-gradient(135deg, ${teacherColor}, #6B46C1)`,
color: '#FFFFFF',
fontSize: '14px',
fontWeight: 700,
cursor: 'pointer',
boxShadow: `0 4px 15px ${teacherColor}40`,
transition: 'all 0.2s ease',
width: '100%',
maxWidth: '280px',
}}
onTouchStart={(e) => { e.currentTarget.style.transform = 'scale(0.96)'; }}
onTouchEnd={(e) => { e.currentTarget.style.transform = 'scale(1)'; }}
>
{t('teacherNewLesson')} →
</button>
)}
</div>
))}
{isLoading && (
<div style={{
alignSelf: 'flex-start',
padding: '10px 14px',
borderRadius: '16px 16px 16px 4px',
background: 'rgba(255,255,255,0.8)',
border: '1px solid rgba(0,0,0,0.06)',
display: 'flex',
gap: '4px',
alignItems: 'center',
}}>
{[0, 1, 2].map((i) => (
<div
key={i}
style={{
width: '6px',
height: '6px',
borderRadius: '50%',
background: teacherColor,
animation: `typingDot 0.8s ease infinite ${i * 0.15}s`,
}}
/>
))}
</div>
)}
<div ref={messagesEndRef} />
</div>
)}
{isQuestionActive && (
<div style={{