ConfigurationModal.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use client';
  2. import { useState, useEffect, useCallback } from 'react';
  3. import { X, Save, Loader2, AlertCircle, Sparkles, KeyRound, Info, Braces } from 'lucide-react';
  4. import { toast } from 'react-hot-toast';
  5. import JsonConfigEditor from '@/components/common/JsonConfigEditor';
  6. import api from '@/lib/api';
  7. import { DynamicConfiguration } from '@/types';
  8. type ConfigurationForm = DynamicConfiguration;
  9. const emptyForm: ConfigurationForm = {
  10. config_key: '',
  11. config_value: '',
  12. description: '',
  13. type: 'json',
  14. };
  15. const COMMON_TYPES = ['json', 'string', 'boolean', 'number'];
  16. interface ConfigurationModalProps {
  17. isOpen: boolean;
  18. onClose: () => void;
  19. config: DynamicConfiguration | null;
  20. onSuccess: () => void;
  21. }
  22. export default function ConfigurationModal({
  23. isOpen,
  24. onClose,
  25. config,
  26. onSuccess,
  27. }: ConfigurationModalProps) {
  28. const [form, setForm] = useState<ConfigurationForm>(emptyForm);
  29. const [submitting, setSubmitting] = useState(false);
  30. const [valueJsonError, setValueJsonError] = useState<string | null>(null);
  31. useEffect(() => {
  32. if (isOpen) {
  33. if (config) {
  34. let displayValue = config.config_value;
  35. try {
  36. displayValue = JSON.stringify(JSON.parse(config.config_value), null, 2);
  37. } catch {
  38. // keep as is
  39. }
  40. setForm({
  41. ...config,
  42. config_value: displayValue,
  43. type: config.type || 'json',
  44. });
  45. } else {
  46. setForm(emptyForm);
  47. }
  48. setValueJsonError(null);
  49. }
  50. }, [isOpen, config]);
  51. const handleValueValidityChange = useCallback((isValid: boolean, error: string | null) => {
  52. setValueJsonError(isValid ? null : error || 'JSON 语法错误');
  53. }, []);
  54. const handleSubmit = async (e: React.FormEvent) => {
  55. e.preventDefault();
  56. if (!form.config_key.trim()) {
  57. toast.error('配置 Key 不能为空');
  58. return;
  59. }
  60. if (form.type === 'json' && valueJsonError) {
  61. toast.error('请修复 JSON 语法错误后再保存');
  62. return;
  63. }
  64. setSubmitting(true);
  65. try {
  66. if (config && config.config_key) {
  67. await api.put(`/api/dynamic-configurations/key/${encodeURIComponent(config.config_key)}`, {
  68. config_value: form.config_value,
  69. description: form.description || undefined,
  70. type: form.type || undefined,
  71. });
  72. toast.success(`配置 "${form.config_key}" 更新成功`);
  73. } else {
  74. await api.post('/api/dynamic-configurations', {
  75. config_key: form.config_key.trim(),
  76. config_value: form.config_value,
  77. description: form.description || undefined,
  78. type: form.type || undefined,
  79. });
  80. toast.success(`配置 "${form.config_key}" 创建成功`);
  81. }
  82. onSuccess();
  83. onClose();
  84. } catch (e: any) {
  85. toast.error('操作失败: ' + (e.response?.data?.message || e.message || '未知错误'));
  86. } finally {
  87. setSubmitting(false);
  88. }
  89. };
  90. if (!isOpen) return null;
  91. return (
  92. <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-0 md:p-4 font-mono">
  93. <div className="bg-neutral-50 w-full h-full md:w-full md:max-w-5xl md:h-[88vh] border-2 border-black shadow-[8px_8px_0px_#000] flex flex-col overflow-hidden animate-in zoom-in-95 duration-200">
  94. {/* Header */}
  95. <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none shrink-0">
  96. <div className="flex items-center gap-2">
  97. <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
  98. <h3 className="font-black text-black text-xs md:text-sm uppercase tracking-wider">
  99. {config ? `[ EDIT CONFIG // ${config.config_key} ]` : '[ NEW CONFIG KEY // 新建动态配置项 ]'}
  100. </h3>
  101. </div>
  102. <button
  103. onClick={onClose}
  104. className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
  105. >
  106. ✕
  107. </button>
  108. </div>
  109. {/* Modal Form */}
  110. <form onSubmit={handleSubmit} className="flex-1 flex flex-col overflow-hidden">
  111. <div className="flex-1 flex flex-col lg:flex-row overflow-hidden">
  112. {/* 左侧:JSON / 文本代码编辑器 */}
  113. <div className="flex-1 flex flex-col bg-white border-r-2 border-black overflow-hidden">
  114. <div className="px-4 py-2 border-b-2 border-black bg-neutral-100 flex items-center justify-between shrink-0">
  115. <span className="text-xs font-black uppercase text-black">
  116. [ 01 // 配置 Payload Value 编辑器 ]
  117. </span>
  118. <span className="text-[10px] text-neutral-500 font-mono">
  119. TYPE: {form.type.toUpperCase()}
  120. </span>
  121. </div>
  122. <div className="flex-1 relative overflow-hidden bg-neutral-900">
  123. <JsonConfigEditor
  124. value={form.config_value}
  125. onChange={(configValue) => setForm({ ...form, config_value: configValue })}
  126. onValidityChange={handleValueValidityChange}
  127. />
  128. </div>
  129. </div>
  130. {/* 右侧:属性侧栏 */}
  131. <div className="w-full lg:w-[340px] bg-neutral-50 p-5 shrink-0 overflow-y-auto space-y-4 text-xs">
  132. <div className="border-b-2 border-black pb-2">
  133. <span className="text-xs font-black uppercase text-black">
  134. [ 02 // 配置属性元数据 ]
  135. </span>
  136. </div>
  137. {/* Config Key */}
  138. <div className="space-y-1">
  139. <label className="font-bold uppercase text-black block">
  140. 配置 KEY <span className="text-black">*</span>
  141. </label>
  142. <input
  143. type="text"
  144. value={form.config_key}
  145. onChange={(e) => setForm({ ...form, config_key: e.target.value })}
  146. className="w-full px-3 py-2 border-2 border-black bg-white text-xs font-mono text-black outline-none shadow-[2px_2px_0px_#000] disabled:bg-neutral-200"
  147. placeholder="EXCHANGE_RATES"
  148. required
  149. disabled={Boolean(config)}
  150. />
  151. <p className="text-[10px] text-neutral-500">唯一键标识,后端以此键调取</p>
  152. </div>
  153. {/* Type */}
  154. <div className="space-y-1">
  155. <label className="font-bold uppercase text-black block">
  156. 数据类型 (Type)
  157. </label>
  158. <input
  159. type="text"
  160. value={form.type}
  161. onChange={(e) => setForm({ ...form, type: e.target.value })}
  162. className="w-full px-3 py-2 border-2 border-black bg-white text-xs font-mono text-black outline-none shadow-[2px_2px_0px_#000]"
  163. placeholder="json"
  164. />
  165. <div className="flex flex-wrap gap-1 mt-1.5">
  166. {COMMON_TYPES.map((t) => (
  167. <button
  168. key={t}
  169. type="button"
  170. onClick={() => setForm({ ...form, type: t })}
  171. className={`text-[10px] px-2 py-0.5 border border-black font-mono font-bold uppercase transition ${
  172. form.type === t
  173. ? 'bg-amber-300 text-black shadow-[1px_1px_0px_#000]'
  174. : 'bg-white text-neutral-700 hover:bg-neutral-100'
  175. }`}
  176. >
  177. {t}
  178. </button>
  179. ))}
  180. </div>
  181. </div>
  182. {/* Description */}
  183. <div className="space-y-1">
  184. <label className="font-bold uppercase text-black block">
  185. 用途说明描述
  186. </label>
  187. <textarea
  188. rows={5}
  189. value={form.description}
  190. onChange={(e) => setForm({ ...form, description: e.target.value })}
  191. className="w-full px-3 py-2 border-2 border-black bg-white text-xs font-sans text-black outline-none resize-none shadow-[2px_2px_0px_#000]"
  192. placeholder="记录该配置的作用、业务逻辑及变更注意事项..."
  193. />
  194. </div>
  195. </div>
  196. </div>
  197. {/* Footer */}
  198. <div className="px-5 py-3 border-t-2 border-black bg-neutral-200 flex items-center justify-between shrink-0">
  199. <div className="text-xs font-bold text-rose-800 flex items-center gap-1">
  200. {valueJsonError && (
  201. <>
  202. <AlertCircle size={14} />
  203. <span>{valueJsonError}</span>
  204. </>
  205. )}
  206. </div>
  207. <div className="flex gap-2">
  208. <button
  209. type="button"
  210. onClick={onClose}
  211. className="px-4 py-1.5 bg-white hover:bg-neutral-100 border-2 border-black text-xs font-bold text-black shadow-[2px_2px_0px_#000] uppercase"
  212. disabled={submitting}
  213. >
  214. [ 取消 ]
  215. </button>
  216. <button
  217. type="submit"
  218. disabled={submitting || (Boolean(valueJsonError) && form.type === 'json')}
  219. className="flex items-center gap-1 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 disabled:opacity-50"
  220. >
  221. {submitting ? <Loader2 size={13} className="animate-spin" /> : <Save size={13} />}
  222. <span>[ 保存并生效 ]</span>
  223. </button>
  224. </div>
  225. </div>
  226. </form>
  227. </div>
  228. </div>
  229. );
  230. }