| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 'use client';
- import { useState, useEffect, useCallback } from 'react';
- import { X, Save, Loader2, AlertCircle, Sparkles, KeyRound, Info, Braces } from 'lucide-react';
- import { toast } from 'react-hot-toast';
- import JsonConfigEditor from '@/components/common/JsonConfigEditor';
- import api from '@/lib/api';
- import { DynamicConfiguration } from '@/types';
- type ConfigurationForm = DynamicConfiguration;
- const emptyForm: ConfigurationForm = {
- config_key: '',
- config_value: '',
- description: '',
- type: 'json',
- };
- const COMMON_TYPES = ['json', 'string', 'boolean', 'number'];
- interface ConfigurationModalProps {
- isOpen: boolean;
- onClose: () => void;
- config: DynamicConfiguration | null;
- onSuccess: () => void;
- }
- export default function ConfigurationModal({
- isOpen,
- onClose,
- config,
- onSuccess,
- }: ConfigurationModalProps) {
- const [form, setForm] = useState<ConfigurationForm>(emptyForm);
- const [submitting, setSubmitting] = useState(false);
- const [valueJsonError, setValueJsonError] = useState<string | null>(null);
- useEffect(() => {
- if (isOpen) {
- if (config) {
- let displayValue = config.config_value;
- try {
- displayValue = JSON.stringify(JSON.parse(config.config_value), null, 2);
- } catch {
- // keep as is
- }
- setForm({
- ...config,
- config_value: displayValue,
- type: config.type || 'json',
- });
- } else {
- setForm(emptyForm);
- }
- setValueJsonError(null);
- }
- }, [isOpen, config]);
- const handleValueValidityChange = useCallback((isValid: boolean, error: string | null) => {
- setValueJsonError(isValid ? null : error || 'JSON 语法错误');
- }, []);
- const handleSubmit = async (e: React.FormEvent) => {
- e.preventDefault();
- if (!form.config_key.trim()) {
- toast.error('配置 Key 不能为空');
- return;
- }
- if (form.type === 'json' && valueJsonError) {
- toast.error('请修复 JSON 语法错误后再保存');
- return;
- }
- setSubmitting(true);
- try {
- if (config && config.config_key) {
- await api.put(`/api/dynamic-configurations/key/${encodeURIComponent(config.config_key)}`, {
- config_value: form.config_value,
- description: form.description || undefined,
- type: form.type || undefined,
- });
- toast.success(`配置 "${form.config_key}" 更新成功`);
- } else {
- await api.post('/api/dynamic-configurations', {
- config_key: form.config_key.trim(),
- config_value: form.config_value,
- description: form.description || undefined,
- type: form.type || undefined,
- });
- toast.success(`配置 "${form.config_key}" 创建成功`);
- }
- onSuccess();
- onClose();
- } catch (e: any) {
- toast.error('操作失败: ' + (e.response?.data?.message || e.message || '未知错误'));
- } finally {
- setSubmitting(false);
- }
- };
- if (!isOpen) return null;
- return (
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-0 md:p-4 font-mono">
- <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">
-
- {/* 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 shrink-0">
- <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">
- {config ? `[ EDIT CONFIG // ${config.config_key} ]` : '[ NEW CONFIG KEY // 新建动态配置项 ]'}
- </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>
- {/* Modal Form */}
- <form onSubmit={handleSubmit} className="flex-1 flex flex-col overflow-hidden">
- <div className="flex-1 flex flex-col lg:flex-row overflow-hidden">
-
- {/* 左侧:JSON / 文本代码编辑器 */}
- <div className="flex-1 flex flex-col bg-white border-r-2 border-black overflow-hidden">
- <div className="px-4 py-2 border-b-2 border-black bg-neutral-100 flex items-center justify-between shrink-0">
- <span className="text-xs font-black uppercase text-black">
- [ 01 // 配置 Payload Value 编辑器 ]
- </span>
- <span className="text-[10px] text-neutral-500 font-mono">
- TYPE: {form.type.toUpperCase()}
- </span>
- </div>
- <div className="flex-1 relative overflow-hidden bg-neutral-900">
- <JsonConfigEditor
- value={form.config_value}
- onChange={(configValue) => setForm({ ...form, config_value: configValue })}
- onValidityChange={handleValueValidityChange}
- />
- </div>
- </div>
- {/* 右侧:属性侧栏 */}
- <div className="w-full lg:w-[340px] bg-neutral-50 p-5 shrink-0 overflow-y-auto space-y-4 text-xs">
- <div className="border-b-2 border-black pb-2">
- <span className="text-xs font-black uppercase text-black">
- [ 02 // 配置属性元数据 ]
- </span>
- </div>
- {/* Config Key */}
- <div className="space-y-1">
- <label className="font-bold uppercase text-black block">
- 配置 KEY <span className="text-black">*</span>
- </label>
- <input
- type="text"
- value={form.config_key}
- onChange={(e) => setForm({ ...form, config_key: e.target.value })}
- 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"
- placeholder="EXCHANGE_RATES"
- required
- disabled={Boolean(config)}
- />
- <p className="text-[10px] text-neutral-500">唯一键标识,后端以此键调取</p>
- </div>
- {/* Type */}
- <div className="space-y-1">
- <label className="font-bold uppercase text-black block">
- 数据类型 (Type)
- </label>
- <input
- type="text"
- value={form.type}
- onChange={(e) => setForm({ ...form, type: e.target.value })}
- 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]"
- placeholder="json"
- />
- <div className="flex flex-wrap gap-1 mt-1.5">
- {COMMON_TYPES.map((t) => (
- <button
- key={t}
- type="button"
- onClick={() => setForm({ ...form, type: t })}
- className={`text-[10px] px-2 py-0.5 border border-black font-mono font-bold uppercase transition ${
- form.type === t
- ? 'bg-amber-300 text-black shadow-[1px_1px_0px_#000]'
- : 'bg-white text-neutral-700 hover:bg-neutral-100'
- }`}
- >
- {t}
- </button>
- ))}
- </div>
- </div>
- {/* Description */}
- <div className="space-y-1">
- <label className="font-bold uppercase text-black block">
- 用途说明描述
- </label>
- <textarea
- rows={5}
- value={form.description}
- onChange={(e) => setForm({ ...form, description: e.target.value })}
- 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]"
- placeholder="记录该配置的作用、业务逻辑及变更注意事项..."
- />
- </div>
- </div>
- </div>
- {/* Footer */}
- <div className="px-5 py-3 border-t-2 border-black bg-neutral-200 flex items-center justify-between shrink-0">
- <div className="text-xs font-bold text-rose-800 flex items-center gap-1">
- {valueJsonError && (
- <>
- <AlertCircle size={14} />
- <span>{valueJsonError}</span>
- </>
- )}
- </div>
- <div className="flex 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 text-black shadow-[2px_2px_0px_#000] uppercase"
- disabled={submitting}
- >
- [ 取消 ]
- </button>
- <button
- type="submit"
- disabled={submitting || (Boolean(valueJsonError) && form.type === 'json')}
- 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"
- >
- {submitting ? <Loader2 size={13} className="animate-spin" /> : <Save size={13} />}
- <span>[ 保存并生效 ]</span>
- </button>
- </div>
- </div>
- </form>
- </div>
- </div>
- );
- }
|