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
159 lines
6.6 KiB
TypeScript
159 lines
6.6 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { Upload } from "lucide-react";
|
|
import { Badge } from "@/app/components/ui/badge";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog";
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/app/components/ui/table";
|
|
import { ErrorState } from "@/app/components/ui/states";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import { buildImportPreview, type ImportPreviewRow, type MessageRow } from "@/app/lib/translationText";
|
|
import { exportMessages, importMessages, type ImportResult } from "@/app/lib/services/translations";
|
|
|
|
/** Rows need a code and string content, and back-office area if any; the rest are reported by file row (1-based). */
|
|
function splitRows(parsed: unknown[]) {
|
|
const valid: MessageRow[] = [];
|
|
const at: number[] = [];
|
|
const bad: { index: number; code: string }[] = [];
|
|
parsed.forEach((r, i) => {
|
|
const row = r as Partial<MessageRow> | null;
|
|
if (row && typeof row.code === "string" && row.code.trim() && typeof row.content === "string" && (row.area == null || row.area === "A")) {
|
|
valid.push(row as MessageRow);
|
|
at.push(i + 1);
|
|
} else {
|
|
bad.push({ index: i + 1, code: typeof row?.code === "string" ? row.code : "?" });
|
|
}
|
|
});
|
|
return { valid, at, bad };
|
|
}
|
|
|
|
/** Import, ported from babyboo: preview create/update by code, confirm, then a per-row summary. */
|
|
export function ImportMessagesDialog({ onClose, onImported }: { onClose(): void; onImported(): void }) {
|
|
const t = useT();
|
|
const [rows, setRows] = useState<MessageRow[] | null>(null);
|
|
const [rowAt, setRowAt] = useState<number[]>([]);
|
|
const [badRows, setBadRows] = useState<{ index: number; code: string }[]>([]);
|
|
const [preview, setPreview] = useState<ImportPreviewRow[]>([]);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [result, setResult] = useState<ImportResult | null>(null);
|
|
const [importing, setImporting] = useState(false);
|
|
|
|
async function handleFile(file: File) {
|
|
setError(null);
|
|
setResult(null);
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(await file.text());
|
|
} catch {
|
|
return setError("MsgsImportInvalidJson");
|
|
}
|
|
if (!Array.isArray(parsed)) return setError("MsgsImportNotArray");
|
|
try {
|
|
const { valid, at, bad } = splitRows(parsed);
|
|
setPreview(buildImportPreview(valid, await exportMessages()));
|
|
setBadRows(bad);
|
|
setRowAt(at);
|
|
setRows(valid);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "MsgsImportReadFailed");
|
|
}
|
|
}
|
|
|
|
async function handleConfirm() {
|
|
if (!rows) return;
|
|
setImporting(true);
|
|
setError(null);
|
|
try {
|
|
setResult(await importMessages(rows));
|
|
onImported();
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "MsgsImportFailed");
|
|
} finally {
|
|
setImporting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open onOpenChange={(open) => !open && onClose()}>
|
|
<DialogContent className="max-h-[90vh] overflow-y-auto">
|
|
<DialogHeader>
|
|
<DialogTitle>{t("MsgsImport")}</DialogTitle>
|
|
</DialogHeader>
|
|
{error && <ErrorState message={t(error)} />}
|
|
{!rows && (
|
|
<Input type="file" accept="application/json" aria-label={t("MsgsImportFileLabel")}
|
|
onChange={(e) => e.target.files?.[0] && handleFile(e.target.files[0])} />
|
|
)}
|
|
{rows && !result && (
|
|
<>
|
|
<p className="text-sm text-muted-foreground">
|
|
{t("MsgsImportPreview", {
|
|
created: preview.filter((p) => p.action === "create").length,
|
|
updated: preview.filter((p) => p.action === "update").length,
|
|
})}
|
|
</p>
|
|
{badRows.length > 0 && (
|
|
<ul className="list-inside list-disc text-sm text-destructive">
|
|
{badRows.map((b) => (
|
|
<li key={b.index}>{t("MsgsImportRowError", { index: b.index, code: b.code, error: t("MsgsImportRowInvalid") })}</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
<div className="max-h-64 overflow-y-auto rounded-xl border bg-card">
|
|
<Table>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead>{t("Code")}</TableHead>
|
|
<TableHead>{t("Action")}</TableHead>
|
|
<TableHead>{t("MsgsImportEnglishCol")}</TableHead>
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{preview.map((row, i) => (
|
|
<TableRow key={`${i}|${row.code}`}>
|
|
<TableCell className="font-mono">{row.code}</TableCell>
|
|
<TableCell>
|
|
<Badge variant={row.action === "update" ? "outline" : "default"}>
|
|
{row.action === "update" ? t("MsgsWillUpdate") : t("MsgsWillCreate")}
|
|
</Badge>
|
|
</TableCell>
|
|
<TableCell className="text-sm text-muted-foreground">
|
|
{row.action === "update" && <span className="line-through">{row.currentEn ?? "-"}</span>}
|
|
{row.action === "update" && " / "}
|
|
<span>{row.incomingEn ?? "-"}</span>
|
|
</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => setRows(null)} disabled={importing}>{t("MsgsImportChooseAnother")}</Button>
|
|
<Button onClick={handleConfirm} disabled={importing || rows.length === 0}>
|
|
<Upload aria-hidden="true" />
|
|
{t("MsgsConfirmImport")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</>
|
|
)}
|
|
{result && (
|
|
<div role="status">
|
|
<p>{t("MsgsImportSummary", { created: result.created, updated: result.updated, errors: result.errors.length })}</p>
|
|
{result.errors.length > 0 && (
|
|
<ul className="mt-2 list-inside list-disc text-sm text-destructive">
|
|
{result.errors.map((e) => (
|
|
<li key={`${e.index}-${e.code}`}>
|
|
{t("MsgsImportRowError", { index: rowAt[e.index] ?? e.index + 1, code: e.code || "?", error: t(e.error) })}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|