ConfigurationExportModal.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use client';
  2. import { useState } from 'react';
  3. import { X, Download, FileJson, Check, Copy } from 'lucide-react';
  4. import { toast } from 'react-hot-toast';
  5. import { DynamicConfiguration } from '@/types';
  6. interface ConfigurationExportModalProps {
  7. isOpen: boolean;
  8. onClose: () => void;
  9. configs: DynamicConfiguration[];
  10. }
  11. export default function ConfigurationExportModal({
  12. isOpen,
  13. onClose,
  14. configs,
  15. }: ConfigurationExportModalProps) {
  16. const [copied, setCopied] = useState(false);
  17. if (!isOpen) return null;
  18. const exportPayload = configs.map((c) => ({
  19. config_key: c.config_key,
  20. config_value: c.config_value,
  21. description: c.description,
  22. type: c.type,
  23. }));
  24. const jsonString = JSON.stringify(exportPayload, null, 2);
  25. const handleDownload = () => {
  26. if (configs.length === 0) {
  27. toast.error('没有可导出的配置数据');
  28. return;
  29. }
  30. const blob = new Blob([jsonString], { type: 'application/json;charset=utf-8;' });
  31. const url = URL.createObjectURL(blob);
  32. const link = document.createElement('a');
  33. link.href = url;
  34. link.download = `configurations_backup_${new Date().toISOString().slice(0, 10)}.json`;
  35. document.body.appendChild(link);
  36. link.click();
  37. document.body.removeChild(link);
  38. URL.revokeObjectURL(url);
  39. toast.success(`成功导出 ${configs.length} 项系统配置`);
  40. onClose();
  41. };
  42. const handleCopy = async () => {
  43. if (configs.length === 0) {
  44. toast.error('没有可复制的数据');
  45. return;
  46. }
  47. try {
  48. await navigator.clipboard.writeText(jsonString);
  49. setCopied(true);
  50. toast.success('已复制 JSON 到剪贴板');
  51. setTimeout(() => setCopied(false), 2000);
  52. } catch {
  53. toast.error('复制失败');
  54. }
  55. };
  56. return (
  57. <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">
  58. <div className="bg-white rounded-2xl shadow-2xl w-full max-w-lg overflow-hidden border border-slate-100">
  59. {/* Header */}
  60. <div className="px-6 py-4 border-b border-slate-100 flex justify-between items-center bg-slate-50/50">
  61. <div className="flex items-center gap-2.5">
  62. <div className="w-8 h-8 rounded-lg bg-blue-100 text-blue-600 flex items-center justify-center">
  63. <Download size={18} />
  64. </div>
  65. <div>
  66. <h3 className="font-bold text-base text-slate-800">导出系统配置备份</h3>
  67. <p className="text-xs text-slate-400">将当前全部配置导出为标准 JSON 备份文件</p>
  68. </div>
  69. </div>
  70. <button onClick={onClose} className="text-slate-400 hover:text-slate-600 p-1 rounded-lg">
  71. <X size={20} />
  72. </button>
  73. </div>
  74. {/* Body */}
  75. <div className="p-6 space-y-4">
  76. <div className="bg-slate-50 p-4 rounded-xl border border-slate-200 flex items-center justify-between text-xs">
  77. <div>
  78. <span className="text-slate-400 block">待导出配置项</span>
  79. <strong className="text-slate-900 text-sm font-mono">{configs.length} 项</strong>
  80. </div>
  81. <div className="text-right">
  82. <span className="text-slate-400 block">备份格式</span>
  83. <strong className="text-blue-700 text-sm font-mono">JSON (.json)</strong>
  84. </div>
  85. </div>
  86. <div className="bg-slate-900 rounded-xl p-3 border border-slate-800 relative">
  87. <div className="flex justify-between items-center pb-2 mb-2 border-b border-slate-800 text-[11px] text-slate-400 font-mono">
  88. <span>preview_backup.json</span>
  89. <button
  90. type="button"
  91. onClick={handleCopy}
  92. className="text-blue-400 hover:text-blue-300 flex items-center gap-1 font-sans"
  93. >
  94. {copied ? <Check size={12} /> : <Copy size={12} />}
  95. {copied ? '已复制' : '复制 JSON'}
  96. </button>
  97. </div>
  98. <pre className="text-xs font-mono text-emerald-400 max-h-48 overflow-y-auto whitespace-pre-wrap leading-relaxed select-all">
  99. {jsonString}
  100. </pre>
  101. </div>
  102. </div>
  103. {/* Footer */}
  104. <div className="px-6 py-4 bg-slate-50 flex justify-end gap-3 border-t border-slate-100">
  105. <button
  106. type="button"
  107. onClick={onClose}
  108. className="px-4 py-2 text-sm text-slate-600 hover:bg-slate-200 rounded-xl transition"
  109. >
  110. 取消
  111. </button>
  112. <button
  113. type="button"
  114. onClick={handleDownload}
  115. disabled={configs.length === 0}
  116. 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"
  117. >
  118. <Download size={16} /> 立即下载 JSON
  119. </button>
  120. </div>
  121. </div>
  122. </div>
  123. );
  124. }