Ported from babyboo and trimmed to back-office labels: list with search and the Missing Chinese filter, edit panel with placeholder checks and an unsaved-changes guard, export, import with preview and summary, and the Languages page with guarded switches. A blank translation is no longer treated as an old placeholder mismatch, so Save blocks a broken new one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
122 lines
5.2 KiB
TypeScript
122 lines
5.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState, type FormEvent } from "react";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { ConfirmDialog } from "@/app/components/ui/confirm-dialog";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@/app/components/ui/sheet";
|
|
import { Textarea } from "@/app/components/ui/textarea";
|
|
import { ErrorState } from "@/app/components/ui/states";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import { useLocales } from "@/app/lib/i18n/LocalesProvider";
|
|
import { buildContent, parseContent, placeholderMismatch, type MessageRow } from "@/app/lib/translationText";
|
|
import { updateMessage } from "@/app/lib/services/translations";
|
|
|
|
const list = (xs: string[]) => (xs.length ? xs.join(", ") : "-");
|
|
|
|
/** Edit panel, ported from babyboo: the code is read-only, placeholders must match English, unsaved edits ask first. */
|
|
export function MessageEditSheet({ message, onClose, onSaved }: { message: MessageRow & { id: number }; onClose: () => void; onSaved: () => void }) {
|
|
const t = useT();
|
|
// Enabled languages only, English first; the stored text of any other language is kept on save.
|
|
const locales = useLocales()
|
|
.filter((l) => l.enabled !== false)
|
|
.sort((a, b) => Number(b.code === "en_US") - Number(a.code === "en_US"));
|
|
const stored = parseContent(message.content);
|
|
const initial = Object.fromEntries(locales.map((l) => [l.code, stored[l.code] ?? ""]));
|
|
const [form, setForm] = useState<Record<string, string>>(initial);
|
|
const [saving, setSaving] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [confirmOpen, setConfirmOpen] = useState(false);
|
|
const dirty = JSON.stringify(form) !== JSON.stringify(initial);
|
|
|
|
// A language already mismatched when opened only warns, so an old row can still be saved.
|
|
const checks = locales
|
|
.filter((l) => l.code !== "en_US" && (form[l.code] ?? "").trim())
|
|
.map((l) => {
|
|
const now = placeholderMismatch(form.en_US ?? "", form[l.code] ?? "");
|
|
// A blank field on open is a new translation, not an old mismatch.
|
|
const before = (initial[l.code] ?? "").trim() ? placeholderMismatch(initial.en_US ?? "", initial[l.code]) : { missing: [], extra: [] };
|
|
return {
|
|
code: l.code,
|
|
now,
|
|
mismatched: now.missing.length + now.extra.length > 0,
|
|
wasMismatched: before.missing.length + before.extra.length > 0,
|
|
};
|
|
});
|
|
const blocked = checks.some((c) => c.mismatched && !c.wasMismatched);
|
|
|
|
const requestClose = () => (dirty ? setConfirmOpen(true) : onClose());
|
|
|
|
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
setSaving(true);
|
|
setError(null);
|
|
try {
|
|
await updateMessage(message.id, buildContent(message.content, form));
|
|
onSaved();
|
|
onClose();
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "MsgsSaveFailed");
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Sheet open onOpenChange={(open) => !open && requestClose()}>
|
|
<SheetContent side="right">
|
|
<SheetHeader>
|
|
<SheetTitle>{t("MsgsEdit")}</SheetTitle>
|
|
</SheetHeader>
|
|
<div className="space-y-4 px-4 pb-4">
|
|
{error && <ErrorState message={t(error)} />}
|
|
<form onSubmit={handleSubmit} className="space-y-4" noValidate>
|
|
<div className="space-y-1">
|
|
<p className="text-sm font-medium">{t("Code")}</p>
|
|
<code className="font-mono text-sm">{message.code}</code>
|
|
</div>
|
|
{locales.map((locale) => {
|
|
const check = checks.find((c) => c.code === locale.code);
|
|
return (
|
|
<div key={locale.code} className="space-y-1.5">
|
|
<Label htmlFor={`message-locale-${locale.code}`}>{locale.label}</Label>
|
|
<Textarea
|
|
id={`message-locale-${locale.code}`}
|
|
value={form[locale.code] ?? ""}
|
|
disabled={saving}
|
|
onChange={(e) => setForm((f) => ({ ...f, [locale.code]: e.target.value }))}
|
|
/>
|
|
{check?.mismatched && (
|
|
<p className="text-sm text-warning" role="alert">
|
|
{t(check.wasMismatched ? "MsgsPlaceholderAlreadyDifferent" : "MsgsPlaceholderMismatch", {
|
|
missing: list(check.now.missing),
|
|
extra: list(check.now.extra),
|
|
})}
|
|
</p>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
<div className="flex gap-2">
|
|
<Button type="submit" disabled={saving || blocked}>{t("Save")}</Button>
|
|
<Button type="button" variant="outline" onClick={requestClose}>{t("Cancel")}</Button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</SheetContent>
|
|
</Sheet>
|
|
{confirmOpen && (
|
|
<ConfirmDialog
|
|
title={t("MsgsDiscardTitle")}
|
|
message={t("MsgsDiscardBody")}
|
|
confirmLabel={t("MsgsDiscardConfirm")}
|
|
destructive
|
|
onConfirm={onClose}
|
|
onCancel={() => setConfirmOpen(false)}
|
|
/>
|
|
)}
|
|
</>
|
|
);
|
|
}
|