File size: 8,324 Bytes
4fd1234 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | import { useState } from 'react'
import { genderCheck, translateText } from '../api'
import { speak } from '../tts'
// Shared "quick tools" β the Gender Checker and Translator from the Tools
// screen, also reusable as a side panel while doing exercises (see
// Exercises.jsx).
// ββ Gender Checker βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const GENDER_COLORS = { Masc: '#4A90D9', Fem: '#D96B8A' }
const GENDER_LABELS = { Masc: 'Masculine β', Fem: 'Feminine β' }
export function GenderChecker() {
const [word, setWord] = useState('')
const [data, setData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
const handleCheck = async () => {
const w = word.trim()
if (!w) return
setLoading(true)
setError('')
try {
const result = await genderCheck(w)
setData(result)
} catch (e) {
setError(`Could not check this word: ${e.message}`)
} finally {
setLoading(false)
}
}
const color = data?.gender ? GENDER_COLORS[data.gender] : 'var(--fc-ink)'
return (
<div>
<p className="fc-muted">
Type a French noun to see its gender, articles (le/la, un/une), an example sentence, and a tip for remembering it.
</p>
<div className="fc-row">
<input
className="fc-input"
value={word}
onChange={(e) => setWord(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleCheck()}
placeholder="e.g. pomme, restaurant, voitureβ¦"
/>
<button className="fc-btn fc-btn-primary" onClick={handleCheck} disabled={loading || !word.trim()}>
{loading ? 'Checkingβ¦' : 'π Check'}
</button>
</div>
{error && <div className="fc-status fc-error">β {error}</div>}
{data && (
<div className="fc-card fc-gender-result">
<div className="fc-gender-headword" style={{ color }}>
{data.article ? `${data.article} ` : ''}{data.word}
<button className="fc-btn fc-btn-secondary fc-btn-icon" onClick={() => speak(`${data.article} ${data.word}`.trim())}>
π
</button>
</div>
{data.gender && (
<div className="fc-row fc-gender-pills">
<span className="fc-pill" style={{ color, background: 'transparent', border: `1px solid ${color}` }}>
{GENDER_LABELS[data.gender]}
</span>
<span className="fc-pill">{data.article} {data.word}</span>
<span className="fc-pill">{data.indefinite_article} {data.word}</span>
</div>
)}
{data.example && (
<div className="fc-gender-example">
<div className="fc-gender-example-fr">
{data.example}
<button className="fc-btn fc-btn-secondary fc-btn-icon" onClick={() => speak(data.example)}>π</button>
</div>
{data.example_translation && <div className="fc-muted">{data.example_translation}</div>}
</div>
)}
{data.pattern_note && <div className="fc-gender-pattern">π‘ {data.pattern_note}</div>}
</div>
)}
</div>
)
}
// ββ Translator βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const DIRECTIONS = [
{ id: 'en_fr', label: 'English β French' },
{ id: 'fr_en', label: 'French β English' },
]
export function TranslatorWidget({ lessonText, onRemove }) {
const [text, setText] = useState('')
const [direction, setDirection] = useState('en_fr')
const [useContext, setUseContext] = useState(false)
const [data, setData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
const handleTranslate = async () => {
const t = text.trim()
if (!t) return
setLoading(true)
setError('')
try {
const result = await translateText(t, direction, useContext ? lessonText : '')
setData(result)
} catch (e) {
setError(`Could not translate: ${e.message}`)
} finally {
setLoading(false)
}
}
const switchDirection = (id) => {
if (id === direction) return
setDirection(id)
setData(null)
}
return (
<div className="fc-card fc-translator-widget">
{onRemove && (
<button className="fc-btn fc-btn-secondary fc-btn-icon fc-translator-remove" onClick={onRemove} title="Remove this translator">
β
</button>
)}
<div className="fc-row">
{DIRECTIONS.map((d) => (
<button
key={d.id}
className={`fc-subtab${direction === d.id ? ' fc-subtab-active' : ''}`}
onClick={() => switchDirection(d.id)}
>
{d.label}
</button>
))}
</div>
<textarea
className="fc-textarea"
rows={3}
value={text}
onChange={(e) => setText(e.target.value)}
placeholder={direction === 'en_fr' ? 'Type something in Englishβ¦' : 'Tapez quelque chose en franΓ§aisβ¦'}
/>
<div className="fc-row">
<button className="fc-btn fc-btn-primary" onClick={handleTranslate} disabled={loading || !text.trim()}>
{loading ? 'Translatingβ¦' : 'π Translate'}
</button>
{direction === 'fr_en' && text.trim() && (
<button className="fc-btn fc-btn-secondary" onClick={() => speak(text)}>π Hear input</button>
)}
{lessonText?.trim() && (
<label className="fc-checkbox">
<input type="checkbox" checked={useContext} onChange={(e) => setUseContext(e.target.checked)} />
Use my current lesson as context
</label>
)}
</div>
{error && <div className="fc-status fc-error">β {error}</div>}
{data && (
<div className="fc-translate-result">
<div className="fc-translate-main">
{data.translation}
{direction === 'en_fr' && data.translation && (
<button className="fc-btn fc-btn-secondary fc-btn-icon" onClick={() => speak(data.translation)}>π</button>
)}
</div>
{data.alternatives?.length > 0 && (
<div className="fc-translate-alts">
<span className="fc-muted">Also: </span>
{data.alternatives.join(' Β· ')}
</div>
)}
{data.example_fr && (
<div className="fc-translate-example">
<div>
{data.example_fr}
<button className="fc-btn fc-btn-secondary fc-btn-icon" onClick={() => speak(data.example_fr)}>π</button>
</div>
{data.example_en && <div className="fc-muted">{data.example_en}</div>}
</div>
)}
</div>
)}
</div>
)
}
let widgetIdCounter = 0
function nextWidgetId() {
widgetIdCounter += 1
return widgetIdCounter
}
export function TranslatorPanel({ lessonText }) {
const [widgetIds, setWidgetIds] = useState(() => [nextWidgetId()])
const addWidget = () => {
setWidgetIds((ids) => (ids.length < 3 ? [...ids, nextWidgetId()] : ids))
}
const removeWidget = (id) => {
setWidgetIds((ids) => (ids.length > 1 ? ids.filter((i) => i !== id) : ids))
}
return (
<div>
<p className="fc-muted">
Translate a word or phrase between English and French β with alternatives and an example in context.
Add more translators to check a few words or sentences side by side.
</p>
<div className="fc-translator-grid">
{widgetIds.map((id) => (
<TranslatorWidget
key={id}
lessonText={lessonText}
onRemove={widgetIds.length > 1 ? () => removeWidget(id) : null}
/>
))}
</div>
{widgetIds.length < 3 && (
<div className="fc-row" style={{ marginTop: 12 }}>
<button className="fc-btn" onClick={addWidget}>+ Add another translator</button>
</div>
)}
</div>
)
}
|