| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 'use client';
- import { useState } from 'react';
- import { X, Download, FileJson, Check, Copy } from 'lucide-react';
- import { toast } from 'react-hot-toast';
- import { DynamicConfiguration } from '@/types';
- interface ConfigurationExportModalProps {
- isOpen: boolean;
- onClose: () => void;
- configs: DynamicConfiguration[];
- }
- export default function ConfigurationExportModal({
- isOpen,
- onClose,
- configs,
- }: ConfigurationExportModalProps) {
- const [copied, setCopied] = useState(false);
- if (!isOpen) return null;
- const exportPayload = configs.map((c) => ({
- config_key: c.config_key,
- config_value: c.config_value,
- description: c.description,
- type: c.type,
- }));
- const jsonString = JSON.stringify(exportPayload, null, 2);
- const handleDownload = () => {
- if (configs.length === 0) {
- toast.error('没有可导出的配置数据');
- return;
- }
- const blob = new Blob([jsonString], { type: 'application/json;charset=utf-8;' });
- const url = URL.createObjectURL(blob);
- const link = document.createElement('a');
- link.href = url;
- link.download = `configurations_backup_${new Date().toISOString().slice(0, 10)}.json`;
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- URL.revokeObjectURL(url);
- toast.success(`成功导出 ${configs.length} 项系统配置`);
- onClose();
- };
- const handleCopy = async () => {
- if (configs.length === 0) {
- toast.error('没有可复制的数据');
- return;
- }
- try {
- await navigator.clipboard.writeText(jsonString);
- setCopied(true);
- toast.success('已复制 JSON 到剪贴板');
- setTimeout(() => setCopied(false), 2000);
- } catch {
- toast.error('复制失败');
- }
- };
- return (
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm p-4 animate-in fade-in zoom-in-95 duration-200">
- <div className="bg-white rounded-2xl shadow-2xl w-full max-w-lg overflow-hidden border border-slate-100">
- {/* Header */}
- <div className="px-6 py-4 border-b border-slate-100 flex justify-between items-center bg-slate-50/50">
- <div className="flex items-center gap-2.5">
- <div className="w-8 h-8 rounded-lg bg-blue-100 text-blue-600 flex items-center justify-center">
- <Download size={18} />
- </div>
- <div>
- <h3 className="font-bold text-base text-slate-800">导出系统配置备份</h3>
- <p className="text-xs text-slate-400">将当前全部配置导出为标准 JSON 备份文件</p>
- </div>
- </div>
- <button onClick={onClose} className="text-slate-400 hover:text-slate-600 p-1 rounded-lg">
- <X size={20} />
- </button>
- </div>
- {/* Body */}
- <div className="p-6 space-y-4">
- <div className="bg-slate-50 p-4 rounded-xl border border-slate-200 flex items-center justify-between text-xs">
- <div>
- <span className="text-slate-400 block">待导出配置项</span>
- <strong className="text-slate-900 text-sm font-mono">{configs.length} 项</strong>
- </div>
- <div className="text-right">
- <span className="text-slate-400 block">备份格式</span>
- <strong className="text-blue-700 text-sm font-mono">JSON (.json)</strong>
- </div>
- </div>
- <div className="bg-slate-900 rounded-xl p-3 border border-slate-800 relative">
- <div className="flex justify-between items-center pb-2 mb-2 border-b border-slate-800 text-[11px] text-slate-400 font-mono">
- <span>preview_backup.json</span>
- <button
- type="button"
- onClick={handleCopy}
- className="text-blue-400 hover:text-blue-300 flex items-center gap-1 font-sans"
- >
- {copied ? <Check size={12} /> : <Copy size={12} />}
- {copied ? '已复制' : '复制 JSON'}
- </button>
- </div>
- <pre className="text-xs font-mono text-emerald-400 max-h-48 overflow-y-auto whitespace-pre-wrap leading-relaxed select-all">
- {jsonString}
- </pre>
- </div>
- </div>
- {/* Footer */}
- <div className="px-6 py-4 bg-slate-50 flex justify-end gap-3 border-t border-slate-100">
- <button
- type="button"
- onClick={onClose}
- className="px-4 py-2 text-sm text-slate-600 hover:bg-slate-200 rounded-xl transition"
- >
- 取消
- </button>
- <button
- type="button"
- onClick={handleDownload}
- disabled={configs.length === 0}
- className="flex items-center gap-2 px-5 py-2 text-sm font-bold bg-slate-900 text-white rounded-xl hover:bg-slate-800 transition disabled:opacity-50 shadow-sm"
- >
- <Download size={16} /> 立即下载 JSON
- </button>
- </div>
- </div>
- </div>
- );
- }
|