Feat: 强制更新结构图,控制台打印结构图不包含的条目

This commit is contained in:
2025-11-04 21:42:58 +08:00
parent f0c9dbfd36
commit 61dee0b127
9 changed files with 213 additions and 17 deletions
+6 -2
View File
@@ -73,7 +73,11 @@ function ExportLanguageModalImpl({
}
// 追加结构外的剩余键,避免丢数据
for (const p of Object.keys(flat)) {
if (!added.has(p)) setByPath(p, flat[p]!);
if (!added.has(p)) {
console.warn(`在结构图中未找到路径: ${p}`);
setByPath(p, flat[p]!);
}
}
try {
return JSON.stringify(root, null, 2);
@@ -115,7 +119,7 @@ function ExportLanguageModalImpl({
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-3xl">
<DialogHeader>
<DialogTitle>导出语言 JSON</DialogTitle>
<DialogTitle>导出 JSON</DialogTitle>
</DialogHeader>
<div className="space-y-3 min-w-0">
<div className="flex flex-wrap gap-2">
+34 -2
View File
@@ -10,6 +10,7 @@ import {
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Checkbox } from "../ui/checkbox";
type Props = {
open: boolean;
@@ -17,6 +18,7 @@ type Props = {
projectId: string;
hasStructure: boolean;
onImported: () => void | Promise<void>;
languages: string[];
};
function ImportLanguageModalImpl({
@@ -25,11 +27,13 @@ function ImportLanguageModalImpl({
projectId,
hasStructure,
onImported,
languages,
}: Props) {
const [lang, setLang] = useState("");
const [importing, setImporting] = useState(false);
const [error, setError] = useState<string | null>(null);
const fileRef = useRef<HTMLInputElement | null>(null);
const [forceOverride, setForceOverride] = useState(false);
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
@@ -57,14 +61,20 @@ function ImportLanguageModalImpl({
} catch {
throw new Error("JSON 解析失败,请检查文件内容");
}
if (!hasStructure) {
// 如果没有结构或勾选强制覆盖,则根据文件重建结构
if (!hasStructure || forceOverride) {
const root = buildStructureFromObject(json);
await upsertStructure({ projectId, root });
console.log("构建结构成功", root);
}
// 导入翻译(可能包含结构外的键,注意需要保留额外处理)
const values = flattenValues(json);
await upsertLanguageTranslations(projectId, language, values);
setLang("");
if (fileRef.current) fileRef.current.value = "";
setForceOverride(false);
onOpenChange(false);
await onImported();
} catch (e) {
@@ -81,13 +91,14 @@ function ImportLanguageModalImpl({
if (!importing) {
onOpenChange(v);
if (!v) setError(null);
if (!v) setForceOverride(false);
}
}}
>
<DialogContent>
<DialogHeader>
<DialogTitle>
{hasStructure ? "导入语言 JSON" : "导入翻译 JSON 并构建结构"}
{hasStructure ? "导入 JSON" : "导入 JSON 并构建结构"}
</DialogTitle>
</DialogHeader>
<form onSubmit={handleSubmit} className="mt-2 space-y-3">
@@ -108,11 +119,31 @@ function ImportLanguageModalImpl({
</label>
<Input
placeholder="en"
list="lang"
value={lang}
onChange={(e) => setLang(e.target.value)}
aria-label="语言代号"
/>
<datalist id="lang">
{languages.map((lang) => (
<option key={lang} value={lang} />
))}
</datalist>
</div>
{hasStructure && (
<div>
<label className="inline-flex items-center gap-2 text-sm select-none">
<Checkbox
checked={forceOverride}
onCheckedChange={(checked) => setForceOverride(checked === true)}
/>
强制覆盖结构
</label>
<div className="mt-1 text-xs text-orange-500">
勾选后将使用上传文件的结构覆盖当前项目结构,此操作不可撤销。
</div>
</div>
)}
{error && (
<div className="text-sm text-red-600" role="alert">
{error}
@@ -125,6 +156,7 @@ function ImportLanguageModalImpl({
onClick={() => {
onOpenChange(false);
setError(null);
setForceOverride(false);
}}
disabled={importing}
>
+30
View File
@@ -0,0 +1,30 @@
import * as React from "react"
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
import { CheckIcon } from "lucide-react"
import { cn } from "@/lib/utils"
function Checkbox({
className,
...props
}: React.ComponentProps<typeof CheckboxPrimitive.Root>) {
return (
<CheckboxPrimitive.Root
data-slot="checkbox"
className={cn(
"peer border-input dark:bg-input/30 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground dark:data-[state=checked]:bg-primary data-[state=checked]:border-primary focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive size-4 shrink-0 rounded-[4px] border shadow-xs transition-shadow outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50",
className
)}
{...props}
>
<CheckboxPrimitive.Indicator
data-slot="checkbox-indicator"
className="grid place-content-center text-current transition-none"
>
<CheckIcon className="size-3.5" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
)
}
export { Checkbox }
+38
View File
@@ -0,0 +1,38 @@
import {
CircleCheckIcon,
InfoIcon,
Loader2Icon,
OctagonXIcon,
TriangleAlertIcon,
} from "lucide-react";
import { useTheme } from "next-themes";
import { Toaster as Sonner, type ToasterProps } from "sonner";
const Toaster = ({ ...props }: ToasterProps) => {
const { theme = "system" } = useTheme();
return (
<Sonner
theme={theme as ToasterProps["theme"]}
className="toaster group"
icons={{
success: <CircleCheckIcon className="size-4" />,
info: <InfoIcon className="size-4" />,
warning: <TriangleAlertIcon className="size-4" />,
error: <OctagonXIcon className="size-4" />,
loading: <Loader2Icon className="size-4 animate-spin" />,
}}
style={
{
"--normal-bg": "var(--popover)",
"--normal-text": "var(--popover-foreground)",
"--normal-border": "var(--border)",
"--border-radius": "var(--radius)",
} as React.CSSProperties
}
{...props}
/>
);
};
export { Toaster };