import React, { useState, useEffect, useRef } from 'react'; import { Volume2, ArrowRight, RefreshCw, Check, X, Shuffle, Settings, PlayCircle } from 'lucide-react'; const RAW_DATA = [ { id: 1, word: "Elements", definition: "Essential parts of something.", pronunciation: "El-e-ments", example: "We identified the key elements of the story.", level: "B2" }, { id: 2, word: "Parts", definition: "Pieces or segments of a whole.", pronunciation: "Parts", example: "The machine has many moving parts.", level: "A1" }, { id: 3, word: "Fragments", definition: "Small parts broken or separated off something.", pronunciation: "Frag-ments", example: "The vase shattered into tiny fragments.", level: "B2" }, { id: 4, word: "Frustration", definition: "The feeling of being upset or annoyed.", pronunciation: "Frus-tra-tion", example: "He expressed his frustration about the delay.", level: "B2" }, { id: 5, word: "Reach out", definition: "To contact someone for help.", pronunciation: "Reach - out", example: "Don't hesitate to reach out if you need support.", level: "B2" }, { id: 6, word: "Alleviate", definition: "To make a problem less severe.", pronunciation: "Al-le-vi-ate", example: "Ice can help alleviate the swelling.", level: "C1" }, { id: 7, word: "Ease", definition: "Freedom from worry or pain.", pronunciation: "Ease", example: "The news put my mind at ease.", level: "B1" }, { id: 8, word: "Frustrated", definition: "Feeling annoyed because of inability to change something.", pronunciation: "Frus-tra-ted", example: "I get frustrated when the internet is slow.", level: "B2" }, { id: 9, word: "Frustrating", definition: "Causing annoyance or upset.", pronunciation: "Frus-tra-ting", example: "It is frustrating to lose your keys.", level: "B2" }, { id: 10, word: "Recognise", definition: "To acknowledge the existence or validity of something.", pronunciation: "Rec-og-nise", example: "We must recognise the importance of this issue.", level: "B1" }, { id: 11, word: "Recognition", definition: "Acknowledgment of existence or validity.", pronunciation: "Rec-og-ni-tion", example: "She received recognition for her charity work.", level: "B2" }, { id: 12, word: "Identifying", definition: "The process of establishing who or what something is.", pronunciation: "I-den-ti-fy-ing", example: "Identifying the problem is the first step.", level: "B2" }, { id: 13, word: "Identify", definition: "To establish identity.", pronunciation: "I-den-ti-fy", example: "Can you identify the bird by its call?", level: "B2" }, { id: 14, word: "Identification", definition: "The action of identifying.", pronunciation: "I-den-ti-fi-ca-tion", example: "Please show your identification at the door.", level: "B2" }, { id: 15, word: "Priorities", definition: "Things that are regarded as more important than others.", pronunciation: "Pri-or-i-ties", example: "My family and health are my main priorities.", level: "B2" }, { id: 16, word: "Priority", definition: "A thing that is regarded as more important.", pronunciation: "Pri-or-i-ty", example: "Safety is our top priority.", level: "B2" }, { id: 17, word: "Distribute", definition: "To share something out.", pronunciation: "Dis-trib-ute", example: "Please distribute the worksheets to the class.", level: "B2" }, { id: 18, word: "Distribution", definition: "The action of sharing something out.", pronunciation: "Dis-tri-bu-tion", example: "The distribution of food was well organized.", level: "B2" }, { id: 19, word: "Clarify", definition: "To make a statement less confused.", pronunciation: "Clar-i-fy", example: "Could you clarify what you mean?", level: "B2" }, { id: 20, word: "Clarification", definition: "The action of making something clear.", pronunciation: "Clar-i-fi-ca-tion", example: "I need some clarification on this rule.", level: "C1" }, { id: 21, word: "Take into account", definition: "To consider facts or circumstances.", pronunciation: "Take-in-to-ac-count", example: "We must take into account the cost of travel.", level: "B2" }, { id: 22, word: "Consideration", definition: "Careful thought.", pronunciation: "Con-sid-er-a-tion", example: "Thank you for your consideration of my application.", level: "B2" }, { id: 23, word: "Consider", definition: "To think carefully about something.", pronunciation: "Con-sid-er", example: "You should consider all your options.", level: "A2" }, { id: 24, word: "Communicating", definition: "Sharing or exchanging information.", pronunciation: "Com-mu-ni-ca-ting", example: "Communicating clearly is vital in a team.", level: "B1" }, { id: 25, word: "Communicate", definition: "To share or exchange information.", pronunciation: "Com-mu-ni-cate", example: "Animals communicate in many different ways.", level: "B1" }, { id: 26, word: "Communication", definition: "The exchanging of information or news.", pronunciation: "Com-mu-ni-ca-tion", example: "Good communication prevents misunderstandings.", level: "B1" }, { id: 27, word: "Absences", definition: "Occasions of being away.", pronunciation: "Ab-sen-ces", example: "Too many absences will affect your grade.", level: "B2" }, { id: 28, word: "Absent", definition: "Not present.", pronunciation: "Ab-sent", example: "She was absent from the meeting.", level: "B1" }, { id: 29, word: "Absent-minded", definition: "Habitually forgetful.", pronunciation: "Ab-sent-min-ded", example: "He is so absent-minded he forgot his lunch.", level: "C1" }, { id: 30, word: "Effective", definition: "Successful in producing a desired result.", pronunciation: "Ef-fec-tive", example: "This is the most effective solution.", level: "B2" }, { id: 31, word: "Effectiveness", definition: "The degree to which something is successful.", pronunciation: "Ef-fec-tive-ness", example: "We are evaluating the effectiveness of the drug.", level: "B2" }, { id: 32, word: "Propose", definition: "To put forward a plan or suggestion.", pronunciation: "Pro-pose", example: "I propose we take a break now.", level: "B2" }, { id: 33, word: "Proposal", definition: "A plan or suggestion.", pronunciation: "Pro-po-sal", example: "The committee accepted the proposal.", level: "B2" }, { id: 34, word: "Improve", definition: "To make or become better.", pronunciation: "Im-prove", example: "I want to improve my tennis game.", level: "A2" }, { id: 35, word: "Improvement", definition: "A change that makes something better.", pronunciation: "Im-prove-ment", example: "There is room for improvement in your work.", level: "B1" }, { id: 36, word: "Possible", definition: "Able to be done or achieved.", pronunciation: "Pos-si-ble", example: "It is possible to walk there in an hour.", level: "A1" }, { id: 37, word: "Possibility", definition: "A thing that may happen.", pronunciation: "Pos-si-bil-i-ty", example: "There is a possibility of snow tonight.", level: "B1" }, { id: 38, word: "Flexible", definition: "Able to be easily modified.", pronunciation: "Flex-i-ble", example: "My schedule is quite flexible.", level: "B2" }, { id: 39, word: "Flexibility", definition: "The quality of bending easily.", pronunciation: "Flex-i-bil-i-ty", example: "Yoga improves your flexibility.", level: "B2" }, { id: 40, word: "Passion", definition: "Strong and barely controllable emotion.", pronunciation: "Pas-sion", example: "Music is his life's passion.", level: "B2" }, { id: 41, word: "Passionate", definition: "Showing strong feelings.", pronunciation: "Pas-sion-ate", example: "She is a passionate reader.", level: "B2" }, { id: 42, word: "Risking", definition: "Exposing to danger or loss.", pronunciation: "Ris-king", example: "He is risking his savings on this investment.", level: "B2" }, { id: 43, word: "Risk", definition: "A situation involving exposure to danger.", pronunciation: "Risk", example: "There is a high risk of failure.", level: "B1" }, { id: 44, word: "Risky", definition: "Full of the possibility of danger.", pronunciation: "Ris-ky", example: "It was a risky move, but it paid off.", level: "B2" }, { id: 45, word: "Weaknesses", definition: "Qualities that are not strong.", pronunciation: "Weak-nes-ses", example: "Everyone has strengths and weaknesses.", level: "B2" }, { id: 46, word: "Strength", definition: "The quality or state of being strong.", pronunciation: "Strength", example: "She showed great strength during the crisis.", level: "B1" }, { id: 47, word: "Strengths", definition: "Beneficial qualities or attributes.", pronunciation: "Strengths", example: "Listing your strengths can boost confidence.", level: "B1" }, { id: 48, word: "Look forward to", definition: "To await eagerly.", pronunciation: "Look-for-ward-to", example: "I look forward to the holidays.", level: "B1" } ]; export default function App() { const [words, setWords] = useState(RAW_DATA); const [currentIndex, setCurrentIndex] = useState(0); const [userInput, setUserInput] = useState(""); const [feedback, setFeedback] = useState(null); // 'correct', 'incorrect' const [isRevealed, setIsRevealed] = useState(false); const [hasStarted, setHasStarted] = useState(false); // To handle browser autoplay policies const [autoPlay, setAutoPlay] = useState(true); const [voices, setVoices] = useState([]); const inputRef = useRef(null); const currentWord = words[currentIndex]; // 1. Initialize Voices useEffect(() => { // Safety check for SSR environments if (typeof window === 'undefined' || !('speechSynthesis' in window)) return; const loadVoices = () => { const availableVoices = window.speechSynthesis.getVoices(); if (availableVoices.length > 0) { setVoices(availableVoices); } }; // Load immediately if available loadVoices(); // Chrome/others load voices asynchronously window.speechSynthesis.onvoiceschanged = loadVoices; return () => { if (window.speechSynthesis) { window.speechSynthesis.onvoiceschanged = null; } }; }, []); // 2. Focus input on change useEffect(() => { if (inputRef.current && hasStarted) { // Small timeout helps mobile browsers sometimes setTimeout(() => inputRef.current.focus(), 50); } }, [currentIndex, hasStarted]); // 3. Auto-play Logic useEffect(() => { if (hasStarted && autoPlay) { // Debounce slightly to ensure transition feels natural const timer = setTimeout(() => { speakWord(currentWord.word); }, 300); return () => clearTimeout(timer); } }, [currentIndex, hasStarted, autoPlay]); const speakWord = (text) => { if (typeof window === 'undefined' || !('speechSynthesis' in window)) return; window.speechSynthesis.cancel(); // Stop any previous const utterance = new SpeechSynthesisUtterance(text); // Prefer "Google US English" or "Microsoft Zira" or just any EN // Different browsers name voices differently const preferredVoice = voices.find(v => v.name.includes("Google US English") || v.name.includes("Samantha") || (v.lang.startsWith("en-") && v.name.includes("Female")) || v.lang.startsWith("en-") ); if (preferredVoice) { utterance.voice = preferredVoice; } utterance.rate = 0.9; // 10% slower is better for dictation utterance.pitch = 1.0; window.speechSynthesis.speak(utterance); }; const handleStart = () => { setHasStarted(true); // Triggering audio context on first click usually unlocks autoplay for later if (autoPlay) speakWord(currentWord.word); }; const handleShuffle = () => { const shuffled = [...words].sort(() => Math.random() - 0.5); setWords(shuffled); setCurrentIndex(0); resetCard(); }; const resetCard = () => { setUserInput(""); setFeedback(null); setIsRevealed(false); if (inputRef.current) inputRef.current.focus(); }; const handleNext = () => { if (currentIndex < words.length - 1) { setCurrentIndex(prev => prev + 1); resetCard(); } }; const handlePrev = () => { if (currentIndex > 0) { setCurrentIndex(prev => prev - 1); resetCard(); } }; const checkAnswer = (e) => { e.preventDefault(); if (!userInput) return; const cleanInput = userInput.trim().toLowerCase(); const cleanTarget = currentWord.word.trim().toLowerCase(); if (cleanInput === cleanTarget) { setFeedback('correct'); // Optional: Sound effect for correct answer could go here } else { setFeedback('incorrect'); } }; const renderRedactedExample = (text, targetWord) => { const regex = new RegExp(`(${targetWord})`, 'gi'); const parts = text.split(regex); return (
{parts.map((part, i) => { if (part.toLowerCase() === targetWord.toLowerCase()) { return ( setIsRevealed(true)} title="Click to reveal if stuck" > {part} ); } return {part}; })}
); }; const progress = Math.round(((currentIndex + 1) / words.length) * 100); // START SCREEN (Required for Browser Autoplay Policy) if (!hasStarted) { return (This session will read words aloud for you to type. Click start to enable audio permissions.
Dictation Mode • {words.length} Words
The word was
{currentWord.pronunciation}