'use client'; import { useState } from 'react'; import { X, Upload, FileText, CheckCircle2, AlertCircle, Sparkles } from 'lucide-react'; import { toast } from 'react-hot-toast'; import api from '@/lib/api'; import { DynamicConfiguration } from '@/types'; interface ConfigurationImportModalProps { isOpen: boolean; onClose: () => void; onSuccess: () => void; existingKeys: string[]; } export default function ConfigurationImportModal({ isOpen, onClose, onSuccess, existingKeys, }: ConfigurationImportModalProps) { const [jsonInput, setJsonInput] = useState(''); const [overwrite, setOverwrite] = useState(true); const [submitting, setSubmitting] = useState(false); if (!isOpen) return null; let parsedList: DynamicConfiguration[] = []; let parseError: string | null = null; if (jsonInput.trim()) { try { const data = JSON.parse(jsonInput); if (Array.isArray(data)) { parsedList = data .filter((item) => item && typeof item === 'object' && item.config_key) .map((item) => ({ config_key: String(item.config_key).trim(), config_value: typeof item.config_value === 'string' ? item.config_value : JSON.stringify(item.config_value ?? ''), description: String(item.description || ''), type: String(item.type || 'json'), })); } else if (typeof data === 'object' && data !== null) { if (data.config_key) { parsedList = [ { config_key: String(data.config_key).trim(), config_value: typeof data.config_value === 'string' ? data.config_value : JSON.stringify(data.config_value ?? ''), description: String(data.description || ''), type: String(data.type || 'json'), }, ]; } else { parsedList = Object.entries(data).map(([k, v]) => ({ config_key: k.trim(), config_value: typeof v === 'string' ? v : JSON.stringify(v), description: '', type: typeof v === 'object' ? 'json' : typeof v, })); } } } catch (e: any) { parseError = e.message; } } const handleFileUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const reader = new FileReader(); reader.onload = (event) => { const content = event.target?.result as string; setJsonInput(content); }; reader.readAsText(file); }; const handleImport = async () => { if (parsedList.length === 0) { toast.error('无可导入的有效配置数据'); return; } setSubmitting(true); let successCount = 0; let skipCount = 0; try { for (const item of parsedList) { const isExists = existingKeys.includes(item.config_key); if (isExists && !overwrite) { skipCount++; continue; } try { if (isExists && overwrite) { await api.put(`/api/dynamic-configurations/key/${encodeURIComponent(item.config_key)}`, { config_value: item.config_value, description: item.description || undefined, type: item.type || undefined, }); successCount++; } else if (!isExists) { await api.post('/api/dynamic-configurations', { config_key: item.config_key, config_value: item.config_value, description: item.description || undefined, type: item.type || undefined, }); successCount++; } } catch { skipCount++; } } toast.success(`导入完成: 成功 ${successCount} 项, 跳过 ${skipCount} 项`); onClose(); onSuccess(); } catch { toast.error('导入过程中发生错误'); } finally { setSubmitting(false); } }; return (
{/* Header */}

[ IMPORT CONFIGURATIONS // 导入配置数据 ]

{/* Body */}
{/* File Upload Zone */}

[ 点击选择 JSON 配置文件或拖拽入此区域 ]

支持 configurations_backup.json

{/* Textarea Paste */}