From b3216d97f3d34d9edca1ef6c7b4081de7a82b12f Mon Sep 17 00:00:00 2001 From: Drobysh Date: Tue, 22 Sep 2026 14:40:04 +0300 Subject: [PATCH] =?UTF-8?q?feat(teacher):=20=D1=80=D0=B5=D0=B6=D0=B8=D0=BC?= =?UTF-8?q?=20flip-=D0=BA=D0=B0=D1=80=D1=82=D0=BE=D1=87=D0=B5=D0=BA=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=83=D1=80=D0=BE=D0=BA=D0=BE=D0=B2=20(22?= =?UTF-8?q?.09.2026)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/LessonCard.tsx | 480 +++++++++++++++++++ src/components/LessonCardStack.tsx | 100 ++++ src/pages/TeacherChatPage.tsx | 710 ++++++++++++++++------------- 3 files changed, 965 insertions(+), 325 deletions(-) create mode 100644 src/components/LessonCard.tsx create mode 100644 src/components/LessonCardStack.tsx diff --git a/src/components/LessonCard.tsx b/src/components/LessonCard.tsx new file mode 100644 index 0000000..e156670 --- /dev/null +++ b/src/components/LessonCard.tsx @@ -0,0 +1,480 @@ +import React from 'react'; +import { radius } from '../styles/theme'; + +// ============ Типы ============ + +export type CardType = 'intro' | 'question' | 'result' | 'test_summary'; + +export interface LessonCardOption { + option_key: string; + text_pl: string; + text_ru: string; + text_uk: string; + text_en: string; + is_correct: boolean; + wrong_because?: string | null; +} + +export interface LessonCardData { + type: CardType; + + // Для intro + levelCode?: string; + topicName?: string; + lessonNumber?: number; + lessonTitle?: string; + explanation?: string; + examples?: Array<{ text_pl: string; text_user: string }>; + + // Для question + questionId?: string; + questionType?: 'recognition' | 'production'; + prompt?: string; + options?: LessonCardOption[]; + optionsLanguage?: 'pl' | 'user'; + + // Для result / test_summary + resultText?: string; + correctCount?: number; + totalCount?: number; + weakSkill?: string; +} + +interface LessonCardProps { + data: LessonCardData; + cardIndex: number; + totalCards: number; + selectedAnswer: string | null; + textInput: string; + hintUsed: boolean; + isFlipping: boolean; + rotation: number; + + onSelectAnswer: (optionKey: string) => void; + onTextChange: (text: string) => void; + onNext: () => void; + onShowHint: () => void; + onStartLesson?: () => void; +} + +const LessonCard: React.FC = ({ + data, + cardIndex, + totalCards, + selectedAnswer, + textInput, + hintUsed, + isFlipping, + rotation, + onSelectAnswer, + onTextChange, + onNext, + onShowHint, + onStartLesson, +}) => { + const textColor = '#1A1A2E'; + const subColor = '#6B7280'; + const brandColor = '#2ECC71'; + const teacherColor = '#8B5CF6'; + + 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 progressPercent = totalCards > 0 ? ((cardIndex + 1) / totalCards) * 100 : 0; + + // ============ Титульная ============ + const renderIntro = () => ( +
+
+
+ {data.levelCode} · {data.topicName} +
+
+ Урок {data.lessonNumber} +
+

+ {data.lessonTitle} +

+
+ + {data.explanation && ( +
+ {data.explanation} +
+ )} + + {data.examples && data.examples.length > 0 && ( +
+
+ Przykłady +
+ {data.examples.map((ex, i) => ( +
+ 🇵🇱 {ex.text_pl} + {ex.text_user && ( + → {ex.text_user} + )} +
+ ))} +
+ )} + +
+ + +
+ ); + + // ============ Вопрос ============ + const renderQuestion = () => { + const isProduction = data.questionType === 'production'; + + return ( +
+ {!hintUsed && ( + + )} + + {hintUsed && ( +
+ 🔒 подсказка +
+ )} + +
+
+ Задание {cardIndex} +
+
+ {data.prompt} +
+
+ + {isProduction ? ( + onTextChange(e.target.value)} + placeholder="Введите ответ..." + style={{ + width: '100%', + padding: '14px', + borderRadius: radius.md, + border: textInput ? `2px solid ${brandColor}` : '1px solid rgba(0,0,0,0.08)', + background: 'rgba(255,255,255,0.9)', + color: textColor, + fontSize: '16px', + fontWeight: 600, + outline: 'none', + boxSizing: 'border-box', + }} + /> + ) : ( +
+ {(data.options || []).map((opt) => { + const isSelected = selectedAnswer === opt.option_key; + const displayText = data.optionsLanguage === 'user' + ? (opt.text_ru || opt.text_en || opt.text_pl) + : opt.text_pl; + + return ( + + ); + })} +
+ )} + +
+ + +
+ ); + }; + + // ============ Результат урока ============ + const renderResult = () => ( +
+
🎉
+
+ {data.resultText || 'Отличная работа!'} +
+ {data.correctCount !== undefined && data.totalCount !== undefined && ( +
+ {data.correctCount} / {data.totalCount} +
+ )} +
+
+ ); + + // ============ Сводка теста ============ + const renderTestSummary = () => { + const percent = data.totalCount && data.correctCount !== undefined + ? Math.round((data.correctCount / data.totalCount) * 100) + : 0; + + return ( +
+
📊
+
+ Результат теста +
+
= 70 ? brandColor : '#E74C3C', + }}> + {data.correctCount} / {data.totalCount} +
+
+ {percent}% +
+ {data.weakSkill && ( +
+ ⚠️ Слабое место: {data.weakSkill} +
+ )} +
+
+ ); + }; + + // ============ Рендер ============ + const renderContent = () => { + switch (data.type) { + case 'intro': return renderIntro(); + case 'question': return renderQuestion(); + case 'result': return renderResult(); + case 'test_summary': return renderTestSummary(); + default: return null; + } + }; + + return ( +
+
+ {/* Прогресс-бар */} +
+
+
+ + {/* Счётчик */} +
+ {cardIndex} / {totalCards} +
+ + {/* Контент */} +
+ {renderContent()} +
+
+
+ ); +}; + +export default LessonCard; \ No newline at end of file diff --git a/src/components/LessonCardStack.tsx b/src/components/LessonCardStack.tsx new file mode 100644 index 0000000..96b2387 --- /dev/null +++ b/src/components/LessonCardStack.tsx @@ -0,0 +1,100 @@ +import React, { useState } from 'react'; +import LessonCard, { type LessonCardData } from './LessonCard'; + +interface LessonCardStackProps { + cards: LessonCardData[]; + onComplete: () => void; + onAnswer?: (questionId: string, answer: string, hintUsed: boolean) => Promise; +} + +const LessonCardStack: React.FC = ({ + cards, + onComplete, + onAnswer, +}) => { + const [currentIndex, setCurrentIndex] = useState(0); + const [rotation, setRotation] = useState(0); + const [isFlipping, setIsFlipping] = useState(false); + const [selectedAnswer, setSelectedAnswer] = useState(null); + const [textInput, setTextInput] = useState(''); + const [hintUsed, setHintUsed] = useState(false); + + const currentCard = cards[currentIndex]; + const totalCards = cards.length; + + const flip = (nextIndex: number) => { + if (isFlipping) return; + + setIsFlipping(true); + setRotation(180); + + setTimeout(() => { + setCurrentIndex(nextIndex); + setRotation(360); + + setSelectedAnswer(null); + setTextInput(''); + setHintUsed(false); + + setTimeout(() => { + setRotation(0); + setIsFlipping(false); + }, 200); + }, 200); + }; + + const handleNext = async () => { + if (currentCard.type === 'question') { + const answer = currentCard.questionType === 'production' ? textInput : (selectedAnswer || ''); + if (onAnswer && currentCard.questionId) { + await onAnswer(currentCard.questionId, answer, hintUsed); + } + } + + if (currentIndex >= totalCards - 1) { + onComplete(); + return; + } + + flip(currentIndex + 1); + }; + + const handleStartLesson = () => { + if (currentIndex < totalCards - 1) { + flip(currentIndex + 1); + } + }; + + const handleShowHint = () => { + setHintUsed(true); + }; + + return ( +
+
+ +
+
+ ); +}; + +export default LessonCardStack; \ No newline at end of file diff --git a/src/pages/TeacherChatPage.tsx b/src/pages/TeacherChatPage.tsx index 1fd11ad..4137d33 100644 --- a/src/pages/TeacherChatPage.tsx +++ b/src/pages/TeacherChatPage.tsx @@ -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(null); + const [cardSessionId, setCardSessionId] = useState(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 = () => { )}
- {/* Сообщения */} -
- {messages.length === 0 && !isStarted && ( -
-
🎓
-

- {t('teacherWelcome')} -

- - -
- )} - - {showContinueButton && ( -
- -
- )} - - {messages.map((msg) => ( -
+ { + setLessonCards(null); + setCardSessionId(null); + setIsStarted(false); + handleStart(); }} - > -
- {msg.text} -
- - {msg.type === 'bot' && !msg.isQuestion && msg.hasPolishText && ( - - )} - - {msg.isQuestion && msg.options && msg.options.length > 0 && ( -
- {msg.options.map((option) => ( - - ))} -
- )} - - {msg.commands && msg.commands.length > 0 && ( -
- {msg.commands.map((cmd) => { - if (cmd === '/show-answer') { - return ( - - ); - } - if (cmd === '/examples') { - return ( - - ); - } - if (cmd === '/practice') { - return ( - - ); - } - return null; - })} -
- )} - - {msg.isCompleted && ( + /> +
+ ) : ( +
+ {messages.length === 0 && !isStarted && ( +
+
🎓
+

+ {t('teacherWelcome')} +

- )} -
- ))} - - {isLoading && ( -
- {[0, 1, 2].map((i) => ( -
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', }} - /> - ))} -
- )} + > + ☰ {t('teacherMenu')} + +
+ )} -
-
+ {showContinueButton && ( +
+ +
+ )} + + {messages.map((msg) => ( +
+
+ {msg.text} +
+ + {msg.type === 'bot' && !msg.isQuestion && msg.hasPolishText && ( + + )} + + {msg.isQuestion && msg.options && msg.options.length > 0 && ( +
+ {msg.options.map((option) => ( + + ))} +
+ )} + + {msg.commands && msg.commands.length > 0 && ( +
+ {msg.commands.map((cmd) => { + if (cmd === '/show-answer') { + return ( + + ); + } + if (cmd === '/examples') { + return ( + + ); + } + if (cmd === '/practice') { + return ( + + ); + } + return null; + })} +
+ )} + + {msg.isCompleted && ( + + )} +
+ ))} + + {isLoading && ( +
+ {[0, 1, 2].map((i) => ( +
+ ))} +
+ )} + +
+
+ )} {isQuestionActive && (