// Shared UI primitives for IELTS Vocab Coach // Storage / date helpers / data access live in data/api.js — this file is // purely presentational components. const { useState, useEffect, useMemo, useRef, useCallback } = React; // ─── Text-to-speech ──────────────────────────────────────────────────────── function speak(text, opts = {}) { if (!('speechSynthesis' in window)) return; window.speechSynthesis.cancel(); const u = new SpeechSynthesisUtterance(text); u.lang = opts.lang || 'en-US'; u.rate = opts.rate || 0.9; u.pitch = opts.pitch || 1; const voices = window.speechSynthesis.getVoices(); const enVoice = voices.find(v => /en-(US|GB)/i.test(v.lang) && /female|samantha|karen|google/i.test(v.name)) || voices.find(v => /en-/i.test(v.lang)); if (enVoice) u.voice = enVoice; window.speechSynthesis.speak(u); } function SpeakButton({ text, size = 16, label }) { const [speaking, setSpeaking] = useState(false); const onClick = (e) => { e.stopPropagation(); if (speaking) { window.speechSynthesis.cancel(); setSpeaking(false); return; } setSpeaking(true); const u = new SpeechSynthesisUtterance(text); u.lang = 'en-US'; u.rate = 0.9; const voices = window.speechSynthesis.getVoices(); const enVoice = voices.find(v => /en-(US|GB)/i.test(v.lang)); if (enVoice) u.voice = enVoice; u.onend = () => setSpeaking(false); u.onerror = () => setSpeaking(false); window.speechSynthesis.cancel(); window.speechSynthesis.speak(u); }; return ( ); } // prime voices list if (typeof window !== 'undefined' && 'speechSynthesis' in window) { window.speechSynthesis.getVoices(); window.speechSynthesis.onvoiceschanged = () => window.speechSynthesis.getVoices(); } // ─── Icons ───────────────────────────────────────────────────────────────── const Icon = ({ name, size = 18 }) => { const paths = { home: <>, book: <>, zap: <>, edit: <>, chart: <>, check: <>, x: <>, right: <>, left: <>, flame: <>, star: <>, target: <>, award: <>, volume: <>, info: <>, shuffle: <>, arrow: <>, send: <>, spark: <>, refresh: <>, calendar: <>, settings: <>, }; return ( {paths[name]} ); }; // ─── Sidebar ─────────────────────────────────────────────────────────────── function Sidebar({ route, setRoute, state }) { const items = [ { id: 'home', label: 'Home', icon: 'home' }, { id: 'vocab', label: 'Vocabulary', icon: 'book' }, { id: 'quiz', label: 'Vocab Quiz', icon: 'zap' }, { id: 'collocations', label: 'Collocations', icon: 'target' }, { id: 'idioms', label: 'Idioms', icon: 'spark' }, { id: 'grammar', label: 'Grammar', icon: 'target' }, { id: 'writing', label: 'Sentence Writing', icon: 'edit' }, { id: 'writing-templates', label: 'Writing Templates', icon: 'info' }, { id: 'analyzer', label: 'Writing Analyzer', icon: 'chart' }, { id: 'paraphrase', label: 'Paraphrasing', icon: 'edit' }, { id: 'examiner', label: 'AI Examiner', icon: 'volume' }, { id: 'cue-cards', label: 'Cue Cards', icon: 'shuffle' }, { id: 'mock', label: 'Mock Tests', icon: 'award' }, { id: 'predictor', label: 'Band Predictor', icon: 'star' }, { id: 'progress', label: 'Progress', icon: 'chart' }, { id: 'exam-readiness', label: 'Exam Readiness', icon: 'check' }, { id: 'settings', label: 'Settings', icon: 'settings' }, ]; const activeRef = useRef(null); // On mobile the sidebar becomes a horizontal-scrolling row — keep the // active item visible when the route changes instead of leaving it // wherever the user last scrolled. useEffect(() => { if (activeRef.current) activeRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' }); }, [route]); return ( ); } // ─── Theme toggle + top bar ─────────────────────────────────────────────── function ThemeToggle() { const [theme, setTheme] = useState(() => document.documentElement.getAttribute('data-theme') || 'light'); const toggle = () => { const next = theme === 'dark' ? 'light' : 'dark'; document.documentElement.setAttribute('data-theme', next); try { localStorage.setItem('ielts-theme', next); } catch (e) {} setTheme(next); }; return ( ); } function TopBar({ state, onSignIn, onSignUp }) { const isGuest = !state.auth?.user; return (
{isGuest && } {isGuest && }
); } // ─── Small reusable pieces ──────────────────────────────────────────────── function StatTile({ label, value, sub, tone }) { return (
{label}
{value}
{sub &&
{sub}
}
); } function WordCard({ word, flipped, onFlip }) { return (
{!flipped ? (
{word.pos} · Level {word.lvl}
{word.w}
Click card to reveal meaning
) : (
{word.b}
{word.d ? (
{word.d}
) : (
Full definition coming soon — the Bangla meaning above is correct and ready to learn.
)} {word.ex && (
"{word.ex}"
)}
)}
); } function Countdown({ percent }) { return (
); } Object.assign(window, { Icon, Sidebar, TopBar, ThemeToggle, StatTile, WordCard, SpeakButton, speak, Countdown, useState, useEffect, useMemo, useRef, useCallback, });