| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- '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/60 backdrop-grayscale p-4 font-mono">
- <div className="bg-neutral-50 border-2 border-black shadow-[8px_8px_0px_#000] w-full max-w-2xl overflow-hidden animate-in zoom-in-95 duration-200">
-
- {/* Header */}
- <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
- <div className="flex items-center gap-2">
- <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
- <h3 className="font-black text-black text-xs md:text-sm uppercase tracking-wider">
- [ EXPORT DYNAMIC CONFIGURATIONS // 批量导出配置 ]
- </h3>
- </div>
- <button
- onClick={onClose}
- className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
- >
- ✕
- </button>
- </div>
- {/* Content */}
- <div className="p-5 space-y-4 bg-white text-xs">
- <div className="flex items-center justify-between text-neutral-600">
- <span>即将导出全量 <strong className="text-black font-mono font-bold">{configs.length}</strong> 项动态配置:</span>
- <button
- onClick={handleCopy}
- className="px-2 py-0.5 border border-black bg-white hover:bg-neutral-100 font-bold uppercase transition text-[10px] flex items-center gap-1 shadow-[1px_1px_0px_#000]"
- >
- {copied ? <Check size={11} /> : <Copy size={11} />}
- <span>{copied ? '已复制' : '复制 JSON'}</span>
- </button>
- </div>
- <div className="border-2 border-black bg-neutral-900 shadow-[2px_2px_0px_#000] p-3 max-h-72 overflow-y-auto">
- <pre className="text-neutral-100 font-mono text-[11px] leading-relaxed">
- {jsonString}
- </pre>
- </div>
- <div className="bg-neutral-50 p-3 border-2 border-black text-[11px] text-neutral-600">
- [ 说明 ]: 导出的 JSON 备份文件可在任何环境直接通过「导入配置」功能恢复。
- </div>
- </div>
- {/* Footer */}
- <div className="px-5 py-3 bg-neutral-200 border-t-2 border-black flex justify-end gap-2">
- <button
- type="button"
- onClick={onClose}
- className="px-4 py-1.5 bg-white hover:bg-neutral-100 border-2 border-black text-xs font-bold uppercase shadow-[2px_2px_0px_#000]"
- >
- [ 取消 ]
- </button>
- <button
- type="button"
- onClick={handleDownload}
- className="flex items-center gap-1.5 px-5 py-1.5 bg-amber-300 hover:bg-amber-400 text-black border-2 border-black text-xs font-black shadow-[2px_2px_0px_#000] transition active:translate-x-0.5 active:translate-y-0.5 uppercase"
- >
- <Download size={13} />
- <span>[ 下载 JSON 备份文件 ]</span>
- </button>
- </div>
- </div>
- </div>
- );
- }
|