Files
chadiapp/app/grammatik/page.tsx
T
X4Lo 1f1d28a34d Add B1 Meister: OeSD B1 exam trainer (German-Arabic)
Next.js app with Hoeren, Lesen, Schreiben, Sprechen, Grammatik and Wortschatz sections, flashcards with spaced repetition, speaking chat, XP/streaks/badges, daily reminders (PWA) and Docker Compose setup for Coolify.
2026-11-05 19:11:46 +00:00

232 lines
8.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useMemo, useState } from "react";
import { lessons, type GrammarEx, type Lesson } from "@/data/grammatik";
import Ar from "@/components/Ar";
import SpeakButton from "@/components/SpeakButton";
import ResultCard from "@/components/ResultCard";
import { recordResult, useProgress } from "@/lib/progress";
import { sfx } from "@/lib/sfx";
export default function GrammatikPage() {
const [lesson, setLesson] = useState<Lesson | null>(null);
const p = useProgress();
if (lesson) return <LessonView key={lesson.id} lesson={lesson} onBack={() => setLesson(null)} />;
return (
<div>
<h1>🧩 Grammatik</h1>
<p className="sub">
Die wichtigsten B1-Themen – auf Arabisch erklärt, mit Übungen. <Ar>أهم مواضيع B1 – مشروحة بالعربية مع تمارين.</Ar>
</p>
<div className="grid grid-2">
{lessons.map((l) => {
const score = p.done[l.id];
return (
<button key={l.id} className="tile" onClick={() => setLesson(l)}>
<div className="row" style={{ alignItems: "flex-start" }}>
<span className="emoji">{l.emoji}</span>
<span className="spacer" />
{score !== undefined && <span className={`pill ${score >= 60 ? "green" : "yellow"}`}>{score} %</span>}
</div>
<div className="t">{l.title}</div>
<div className="s ar" style={{ textAlign: "left" }}>
{l.titleAr}
</div>
</button>
);
})}
</div>
</div>
);
}
function LessonView({ lesson, onBack }: { lesson: Lesson; onBack: () => void }) {
const [round, setRound] = useState(0);
const [results, setResults] = useState<(boolean | null)[]>(() => lesson.exercises.map(() => null));
const [awarded, setAwarded] = useState<{ c: number; t: number; xp: number } | null>(null);
const onAnswer = (i: number, ok: boolean) => {
const next = results.map((r, j) => (j === i ? ok : r));
setResults(next);
if (next.every((r) => r !== null) && !awarded) {
const c = next.filter(Boolean).length;
const xp = 5 + c * 2;
recordResult(lesson.id, "grammatik", (c / next.length) * 100, xp);
setAwarded({ c, t: next.length, xp });
}
};
return (
<div>
<button className="btn secondary small" style={{ marginTop: 16 }} onClick={onBack}>
← Zurück
</button>
<h1 style={{ fontSize: 24 }}>
{lesson.emoji} {lesson.title}
</h1>
<div className="ar muted">{lesson.titleAr}</div>
<div className="card" style={{ marginTop: 12, background: "var(--purple-soft)", borderColor: "var(--purple)" }}>
<h3>📚 الشرح</h3>
{lesson.explainAr.map((t, i) => (
<p key={i} className="ar" style={{ margin: "4px 0" }}>
{t}
</p>
))}
</div>
<h2>Regeln • القواعد</h2>
<div className="card">
{lesson.rules.map((r, i) => (
<div key={i} style={{ padding: "8px 0", borderBottom: i < lesson.rules.length - 1 ? "1px dashed var(--line)" : "none" }}>
<div style={{ fontWeight: 800 }}>{r.de}</div>
<div className="ar small muted">{r.ar}</div>
</div>
))}
</div>
<h2>Beispiele • أمثلة</h2>
{lesson.examples.map((e, i) => (
<div key={i} className="card row" style={{ flexWrap: "nowrap" }}>
<div style={{ flex: 1 }}>
<div style={{ fontWeight: 700 }}>{e.de}</div>
<Ar block>
<span className="small muted">{e.ar}</span>
</Ar>
</div>
<SpeakButton text={e.de} />
</div>
))}
<h2>Übungen • تمارين</h2>
<div key={round}>
{lesson.exercises.map((ex, i) => (
<div key={i} className="card" style={{ marginTop: 12 }}>
<div className="row" style={{ marginBottom: 6 }}>
<span className="pill">{i + 1}</span>
<span className="small muted">{ex.kind === "order" ? "Bring die Wörter in die richtige Reihenfolge • رتّب الكلمات" : "Wähle die richtige Antwort • اختر الإجابة الصحيحة"}</span>
</div>
{ex.kind === "mc" ? <McEx ex={ex} onAnswer={(ok) => onAnswer(i, ok)} /> : <OrderEx ex={ex} onAnswer={(ok) => onAnswer(i, ok)} />}
</div>
))}
</div>
{awarded && (
<ResultCard
correct={awarded.c}
total={awarded.t}
xp={awarded.xp}
onRetry={() => {
setResults(lesson.exercises.map(() => null));
setAwarded(null);
setRound((r) => r + 1);
}}
/>
)}
</div>
);
}
function McEx({ ex, onAnswer }: { ex: Extract<GrammarEx, { kind: "mc" }>; onAnswer: (ok: boolean) => void }) {
const [chosen, setChosen] = useState<number | null>(null);
return (
<div>
<div style={{ fontWeight: 800, fontSize: 17 }}>{ex.q}</div>
{ex.ar && (
<Ar block>
<span className="small muted">{ex.ar}</span>
</Ar>
)}
<div className="grid grid-2">
{ex.options.map((o, i) => {
let cls = "option";
if (chosen !== null && i === ex.answer) cls += " correct";
else if (chosen === i) cls += " wrong";
return (
<button
key={i}
className={cls}
disabled={chosen !== null}
onClick={() => {
setChosen(i);
const ok = i === ex.answer;
if (ok) sfx.correct();
else sfx.wrong();
onAnswer(ok);
}}
>
{o}
</button>
);
})}
</div>
{chosen !== null && (
<div className={`feedback ${chosen === ex.answer ? "ok" : "no"}`}>
{chosen === ex.answer ? "Richtig! 🎉" : `Richtig wäre: ${ex.options[ex.answer]}`}
{ex.why && <span style={{ fontWeight: 600 }}> – {ex.why}</span>}
</div>
)}
</div>
);
}
function OrderEx({ ex, onAnswer }: { ex: Extract<GrammarEx, { kind: "order" }>; onAnswer: (ok: boolean) => void }) {
const shuffled = useMemo(() => {
const idx = ex.words.map((_, i) => i);
for (let k = 0; k < 20; k++) {
idx.sort(() => Math.random() - 0.5);
if (idx.some((v, i) => v !== i)) break;
}
return idx;
}, [ex]);
const [picked, setPicked] = useState<number[]>([]);
const [checked, setChecked] = useState<boolean | null>(null);
const target = ex.words.join(" ");
const current = picked.map((i) => ex.words[i]).join(" ");
const check = () => {
const ok = current === target;
setChecked(ok);
if (ok) sfx.correct();
else sfx.wrong();
onAnswer(ok);
};
return (
<div>
<Ar block>
<span className="small">{ex.ar}</span>
</Ar>
<div className="drop-zone" style={{ marginTop: 8, borderColor: checked === null ? undefined : checked ? "var(--green)" : "var(--red)" }}>
{picked.length === 0 && <span className="muted small">Tippe die Wörter an …</span>}
{picked.map((wi, k) => (
<button key={k} className="word-chip" disabled={checked !== null} onClick={() => setPicked((p) => p.filter((_, j) => j !== k))}>
{ex.words[wi]}
</button>
))}
</div>
<div className="row" style={{ marginTop: 10 }}>
{shuffled
.filter((i) => !picked.includes(i))
.map((i) => (
<button key={i} className="word-chip" disabled={checked !== null} onClick={() => setPicked((p) => [...p, i])}>
{ex.words[i]}
</button>
))}
</div>
{checked === null ? (
<button className="btn green small" style={{ marginTop: 10 }} disabled={picked.length !== ex.words.length} onClick={check}>
Prüfen ✔
</button>
) : (
<div className={`feedback ${checked ? "ok" : "no"}`}>
{checked ? "Perfekt! 🎉" : "Richtig wäre:"} {!checked && <b>{target}</b>}
<span style={{ marginLeft: 8 }}>
<SpeakButton text={target} />
</span>
</div>
)}
</div>
);
}