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.
351 lines
13 KiB
TypeScript
351 lines
13 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useRef, useState } from "react";
|
||
import { schreibAufgaben, schreibRedemittel, schreibTasks, type SchreibTask } from "@/data/schreiben";
|
||
import Ar from "@/components/Ar";
|
||
import { recordResult, useProgress } from "@/lib/progress";
|
||
import { fireConfetti } from "@/components/AppShell";
|
||
|
||
export default function SchreibenPage() {
|
||
const [aufgabe, setAufgabe] = useState<1 | 2 | 3>(1);
|
||
const [task, setTask] = useState<SchreibTask | null>(null);
|
||
const p = useProgress();
|
||
const info = schreibAufgaben.find((a) => a.aufgabe === aufgabe)!;
|
||
|
||
if (task) return <Editor key={task.id} task={task} onBack={() => setTask(null)} />;
|
||
|
||
return (
|
||
<div>
|
||
<h1>✍️ Schreiben</h1>
|
||
<p className="sub">
|
||
Drei Aufgaben wie in der Prüfung. Schreib selbst – dann prüfen wir zusammen! <Ar>ثلاث مهام كما في الامتحان. اكتب بنفسك ثم نراجع معاً!</Ar>
|
||
</p>
|
||
<div className="tabs">
|
||
{schreibAufgaben.map((a) => (
|
||
<button key={a.aufgabe} className={aufgabe === a.aufgabe ? "active" : ""} onClick={() => setAufgabe(a.aufgabe)}>
|
||
Aufgabe {a.aufgabe}
|
||
</button>
|
||
))}
|
||
</div>
|
||
<div className="card" style={{ background: "var(--yellow-soft)", borderColor: "var(--yellow)" }}>
|
||
<div className="row">
|
||
<h3 style={{ margin: 0 }}>{info.de}</h3>
|
||
<span className="spacer" />
|
||
<span className="pill yellow">⏱ {info.time}</span>
|
||
</div>
|
||
<div className="ar">{info.ar}</div>
|
||
<ul style={{ margin: "8px 0 0", paddingLeft: 20 }}>
|
||
{info.tips.map((t, i) => (
|
||
<li key={i} style={{ marginBottom: 4 }}>
|
||
{t.de}
|
||
<br />
|
||
<Ar>
|
||
<span className="small muted">{t.ar}</span>
|
||
</Ar>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</div>
|
||
<div className="grid" style={{ marginTop: 12 }}>
|
||
{schreibTasks
|
||
.filter((t) => t.aufgabe === aufgabe)
|
||
.map((t) => {
|
||
const score = p.done[t.id];
|
||
return (
|
||
<button key={t.id} className="tile" onClick={() => setTask(t)}>
|
||
<div className="row">
|
||
<span style={{ fontSize: 28 }}>{aufgabe === 2 ? "💬" : "✉️"}</span>
|
||
<div style={{ flex: 1 }}>
|
||
<div className="t" style={{ marginTop: 0 }}>
|
||
{t.title}
|
||
</div>
|
||
<div className="s ar" style={{ textAlign: "left" }}>
|
||
{t.titleAr}
|
||
</div>
|
||
</div>
|
||
{score !== undefined ? <span className={`pill ${score >= 60 ? "green" : "yellow"}`}>{score} %</span> : <span className="pill">~{t.words} Wörter</span>}
|
||
</div>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const connectors = ["weil", "dass", "deshalb", "deswegen", "aber", "außerdem", "obwohl", "wenn", "denn", "trotzdem", "damit", "zuerst", "dann", "danach", "einerseits", "andererseits", "zum beispiel", "meiner meinung nach", "ob", "als", "sondern", "darum", "also", "falls"];
|
||
|
||
interface Check {
|
||
ok: boolean;
|
||
de: string;
|
||
ar: string;
|
||
}
|
||
|
||
function selfCheck(task: SchreibTask, text: string, ticked: boolean[]): Check[] {
|
||
const lower = text.toLowerCase();
|
||
const words = text.trim().split(/\s+/).filter(Boolean).length;
|
||
const found = connectors.filter((c) => new RegExp(`(^|[^a-zäöüß])${c}([^a-zäöüß]|$)`, "i").test(lower));
|
||
const checks: Check[] = [
|
||
{
|
||
ok: words >= task.words * 0.8,
|
||
de: `Länge: ${words} Wörter (Ziel ca. ${task.words})`,
|
||
ar: words >= task.words * 0.8 ? "الطول مناسب" : "النص قصير جداً – اكتب أكثر",
|
||
},
|
||
{
|
||
ok: found.length >= (task.aufgabe === 3 ? 2 : 3),
|
||
de: `Konnektoren: ${found.length ? found.join(", ") : "keine"}`,
|
||
ar: "استخدم أدوات ربط مثل weil, deshalb, außerdem",
|
||
},
|
||
{
|
||
ok: ticked.every(Boolean),
|
||
de: "Alle Leitpunkte bearbeitet (selbst abhaken)",
|
||
ar: "هل كتبت عن كل النقاط؟",
|
||
},
|
||
];
|
||
if (task.aufgabe !== 2) {
|
||
checks.push({
|
||
ok: /^\s*(liebe|lieber|hallo|hi|sehr geehrte|sehr geehrter|guten tag)/i.test(text),
|
||
de: "Anrede am Anfang",
|
||
ar: task.aufgabe === 3 ? "ابدأ بـ Sehr geehrte/r …" : "ابدأ بـ Liebe/Lieber …",
|
||
});
|
||
checks.push({
|
||
ok: /(grüße|grüßen|gruß|bis bald|tschüss)/i.test(text),
|
||
de: "Grußformel am Ende",
|
||
ar: task.aufgabe === 3 ? "اختم بـ Mit freundlichen Grüßen" : "اختم بـ Liebe Grüße",
|
||
});
|
||
}
|
||
if (task.aufgabe === 3) {
|
||
checks.push({
|
||
ok: /\b(Sie|Ihnen|Ihr|Ihre)\b/.test(text) && !/\b(du|dich|dir|dein|deine)\b/i.test(text),
|
||
de: "Formell: „Sie“ (groß) statt „du“",
|
||
ar: "في الرسالة الرسمية استخدم Sie وليس du",
|
||
});
|
||
}
|
||
if (task.aufgabe === 1) {
|
||
checks.push({
|
||
ok: /\b(du|dich|dir|dein|deine|deinen|euch|ihr)\b/i.test(text),
|
||
de: "Informell: „du“ an den Freund / die Freundin",
|
||
ar: "في الرسالة الشخصية استخدم du",
|
||
});
|
||
}
|
||
return checks;
|
||
}
|
||
|
||
interface AiResult {
|
||
points: number;
|
||
summaryDe: string;
|
||
summaryAr: string;
|
||
corrected: string;
|
||
mistakes: { wrong: string; right: string; explainAr: string }[];
|
||
}
|
||
|
||
function Editor({ task, onBack }: { task: SchreibTask; onBack: () => void }) {
|
||
const draftKey = `b1meister:draft:${task.id}`;
|
||
const [text, setText] = useState("");
|
||
const [ticked, setTicked] = useState<boolean[]>(task.points.map(() => false));
|
||
const [checks, setChecks] = useState<Check[] | null>(null);
|
||
const [showModel, setShowModel] = useState(false);
|
||
const [seconds, setSeconds] = useState(0);
|
||
const [aiEnabled, setAiEnabled] = useState(false);
|
||
const [ai, setAi] = useState<AiResult | null>(null);
|
||
const [aiLoading, setAiLoading] = useState(false);
|
||
const [aiError, setAiError] = useState("");
|
||
const awarded = useRef(false);
|
||
const ref = useRef<HTMLTextAreaElement>(null);
|
||
|
||
useEffect(() => {
|
||
try {
|
||
setText(localStorage.getItem(draftKey) ?? "");
|
||
} catch {
|
||
/* ignorieren */
|
||
}
|
||
fetch("/api/coach")
|
||
.then((r) => r.json())
|
||
.then((d: { enabled: boolean }) => setAiEnabled(d.enabled))
|
||
.catch(() => setAiEnabled(false));
|
||
const id = setInterval(() => setSeconds((s) => s + 1), 1000);
|
||
return () => clearInterval(id);
|
||
}, [draftKey]);
|
||
|
||
const update = (v: string) => {
|
||
setText(v);
|
||
try {
|
||
localStorage.setItem(draftKey, v);
|
||
} catch {
|
||
/* ignorieren */
|
||
}
|
||
};
|
||
|
||
const insert = (s: string) => {
|
||
const el = ref.current;
|
||
if (!el) return update(text + s);
|
||
const start = el.selectionStart ?? text.length;
|
||
const end = el.selectionEnd ?? text.length;
|
||
const v = text.slice(0, start) + s + " " + text.slice(end);
|
||
update(v);
|
||
setTimeout(() => {
|
||
el.focus();
|
||
el.selectionStart = el.selectionEnd = start + s.length + 1;
|
||
}, 0);
|
||
};
|
||
|
||
const words = text.trim().split(/\s+/).filter(Boolean).length;
|
||
|
||
const award = (score: number) => {
|
||
if (awarded.current) return;
|
||
awarded.current = true;
|
||
recordResult(task.id, "schreiben", score, 10 + Math.round(score / 10));
|
||
if (score >= 80) fireConfetti();
|
||
};
|
||
|
||
const runCheck = () => {
|
||
const c = selfCheck(task, text, ticked);
|
||
setChecks(c);
|
||
award(Math.round((c.filter((x) => x.ok).length / c.length) * 100));
|
||
};
|
||
|
||
const runAi = async () => {
|
||
setAiLoading(true);
|
||
setAiError("");
|
||
try {
|
||
const r = await fetch("/api/coach", {
|
||
method: "POST",
|
||
headers: { "content-type": "application/json" },
|
||
body: JSON.stringify({ mode: "write", task: task.situation, points: task.points.map((p) => p.de), words: task.words, text }),
|
||
});
|
||
if (!r.ok) throw new Error(String(r.status));
|
||
const d = (await r.json()) as AiResult;
|
||
setAi(d);
|
||
award(Math.round((d.points / 20) * 100));
|
||
} catch {
|
||
setAiError("Die KI-Korrektur ist gerade nicht erreichbar. Nutze den Selbstcheck. • التصحيح الآلي غير متاح حالياً.");
|
||
} finally {
|
||
setAiLoading(false);
|
||
}
|
||
};
|
||
|
||
const mm = String(Math.floor(seconds / 60)).padStart(2, "0");
|
||
const ss = String(seconds % 60).padStart(2, "0");
|
||
|
||
return (
|
||
<div>
|
||
<button className="btn secondary small" style={{ marginTop: 16 }} onClick={onBack}>
|
||
← Zurück
|
||
</button>
|
||
<h1 style={{ fontSize: 24 }}>{task.title}</h1>
|
||
<div className="ar muted">{task.titleAr}</div>
|
||
|
||
<div className="card" style={{ marginTop: 12 }}>
|
||
<p style={{ marginTop: 0, fontWeight: 700 }}>{task.situation}</p>
|
||
<Ar block>{task.situationAr}</Ar>
|
||
<div style={{ marginTop: 10, fontWeight: 800 }}>Schreiben Sie etwas zu diesen Punkten:</div>
|
||
{task.points.map((pt, i) => (
|
||
<label key={i} className="row" style={{ marginTop: 6, alignItems: "flex-start", cursor: "pointer" }}>
|
||
<input
|
||
type="checkbox"
|
||
checked={ticked[i]}
|
||
onChange={(e) => setTicked((t) => t.map((x, j) => (j === i ? e.target.checked : x)))}
|
||
style={{ width: 20, height: 20, marginTop: 2 }}
|
||
/>
|
||
<span style={{ flex: 1 }}>
|
||
{pt.de} <Ar><span className="small muted">{pt.ar}</span></Ar>
|
||
</span>
|
||
</label>
|
||
))}
|
||
</div>
|
||
|
||
<h2>Redemittel • عبارات مفيدة</h2>
|
||
<p className="small muted" style={{ marginTop: -6 }}>
|
||
Antippen zum Einfügen • اضغط للإدراج
|
||
</p>
|
||
<div className="row">
|
||
{schreibRedemittel[task.aufgabe].map((r, i) => (
|
||
<button key={i} className="word-chip" title={r.ar} onClick={() => insert(r.de)}>
|
||
{r.de}
|
||
</button>
|
||
))}
|
||
</div>
|
||
|
||
<div className="row" style={{ marginTop: 16 }}>
|
||
<span className="pill">{words} Wörter</span>
|
||
<span className="pill yellow timer">⏱ {mm}:{ss}</span>
|
||
<span className="spacer" />
|
||
<span className="small muted">Entwurf wird automatisch gespeichert</span>
|
||
</div>
|
||
<textarea ref={ref} rows={11} value={text} onChange={(e) => update(e.target.value)} placeholder="Schreib hier deinen Text … • اكتب نصك هنا" style={{ marginTop: 8, lineHeight: 1.6 }} spellCheck={false} />
|
||
<div className="bar green" style={{ marginTop: 8 }}>
|
||
<div style={{ width: `${Math.min(100, (words / task.words) * 100)}%` }} />
|
||
</div>
|
||
|
||
<div className="grid grid-2" style={{ marginTop: 14 }}>
|
||
<button className="btn green" disabled={words < 10} onClick={runCheck}>
|
||
✅ Selbstcheck
|
||
</button>
|
||
<button className="btn purple" disabled={words < 10 || aiLoading || !aiEnabled} onClick={runAi} title={aiEnabled ? "" : "ANTHROPIC_API_KEY in .env.local setzen"}>
|
||
{aiLoading ? "⏳ Prüfe …" : "🤖 KI-Korrektur"}
|
||
</button>
|
||
</div>
|
||
{!aiEnabled && <div className="small muted" style={{ marginTop: 6 }}>🤖 KI-Korrektur: in den Einstellungen erklärt, wie man sie aktiviert.</div>}
|
||
{aiError && <div className="feedback no">{aiError}</div>}
|
||
|
||
{checks && (
|
||
<div className="card fade-in" style={{ marginTop: 14 }}>
|
||
<h3>Selbstcheck • التحقق الذاتي</h3>
|
||
{checks.map((c, i) => (
|
||
<div key={i} className={`feedback ${c.ok ? "ok" : "no"}`}>
|
||
{c.ok ? "✅" : "⚠️"} {c.de}
|
||
{!c.ok && (
|
||
<div className="ar" style={{ fontWeight: 600 }}>
|
||
{c.ar}
|
||
</div>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{ai && (
|
||
<div className="card fade-in" style={{ marginTop: 14, borderColor: "var(--purple)" }}>
|
||
<div className="row">
|
||
<h3 style={{ margin: 0 }}>🤖 KI-Feedback</h3>
|
||
<span className="spacer" />
|
||
<span className="pill purple">{ai.points} / 20 Punkte</span>
|
||
</div>
|
||
<p>{ai.summaryDe}</p>
|
||
<div className="ar">{ai.summaryAr}</div>
|
||
{ai.mistakes.length > 0 && (
|
||
<>
|
||
<h3 style={{ marginTop: 12 }}>Fehler • الأخطاء</h3>
|
||
{ai.mistakes.map((m, i) => (
|
||
<div key={i} style={{ marginBottom: 8 }}>
|
||
<span style={{ textDecoration: "line-through", color: "var(--red)" }}>{m.wrong}</span> → <b style={{ color: "var(--green)" }}>{m.right}</b>
|
||
<div className="ar small muted">{m.explainAr}</div>
|
||
</div>
|
||
))}
|
||
</>
|
||
)}
|
||
<details>
|
||
<summary>Korrigierter Text</summary>
|
||
<div className="reading" style={{ marginTop: 8 }}>
|
||
{ai.corrected}
|
||
</div>
|
||
</details>
|
||
</div>
|
||
)}
|
||
|
||
<div style={{ marginTop: 16 }}>
|
||
<button className="btn secondary block" onClick={() => setShowModel((s) => !s)}>
|
||
📝 {showModel ? "Musterlösung ausblenden" : "Musterlösung zeigen"} • نموذج الإجابة
|
||
</button>
|
||
{showModel && (
|
||
<div className="card fade-in" style={{ marginTop: 10 }}>
|
||
<div className="reading">{task.model}</div>
|
||
<hr style={{ border: "none", borderTop: "1px solid var(--line)", margin: "14px 0" }} />
|
||
<div className="ar">{task.modelAr}</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|