ConfigurationExportModal.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/60 backdrop-grayscale p-4 font-mono">
  58. <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">
  59. {/* Header */}
  60. <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
  61. <div className="flex items-center gap-2">
  62. <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
  63. <h3 className="font-black text-black text-xs md:text-sm uppercase tracking-wider">
  64. [ EXPORT DYNAMIC CONFIGURATIONS // 批量导出配置 ]
  65. </h3>
  66. </div>
  67. <button
  68. onClick={onClose}
  69. className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
  70. >
  71. ✕
  72. </button>
  73. </div>
  74. {/* Content */}
  75. <div className="p-5 space-y-4 bg-white text-xs">
  76. <div className="flex items-center justify-between text-neutral-600">
  77. <span>即将导出全量 <strong className="text-black font-mono font-bold">{configs.length}</strong> 项动态配置:</span>
  78. <button
  79. onClick={handleCopy}
  80. 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]"
  81. >
  82. {copied ? <Check size={11} /> : <Copy size={11} />}
  83. <span>{copied ? '已复制' : '复制 JSON'}</span>
  84. </button>
  85. </div>
  86. <div className="border-2 border-black bg-neutral-900 shadow-[2px_2px_0px_#000] p-3 max-h-72 overflow-y-auto">
  87. <pre className="text-neutral-100 font-mono text-[11px] leading-relaxed">
  88. {jsonString}
  89. </pre>
  90. </div>
  91. <div className="bg-neutral-50 p-3 border-2 border-black text-[11px] text-neutral-600">
  92. [ 说明 ]: 导出的 JSON 备份文件可在任何环境直接通过「导入配置」功能恢复。
  93. </div>
  94. </div>
  95. {/* Footer */}
  96. <div className="px-5 py-3 bg-neutral-200 border-t-2 border-black flex justify-end gap-2">
  97. <button
  98. type="button"
  99. onClick={onClose}
  100. 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]"
  101. >
  102. [ 取消 ]
  103. </button>
  104. <button
  105. type="button"
  106. onClick={handleDownload}
  107. 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"
  108. >
  109. <Download size={13} />
  110. <span>[ 下载 JSON 备份文件 ]</span>
  111. </button>
  112. </div>
  113. </div>
  114. </div>
  115. );
  116. }