|
| 1 | +import { DeleteOutlined, PlusOutlined, SaveOutlined } from '@ant-design/icons'; |
| 2 | +import { useQuery, useQueryClient } from '@tanstack/react-query'; |
| 3 | +import { |
| 4 | + Button, |
| 5 | + Card, |
| 6 | + Form, |
| 7 | + Input, |
| 8 | + message, |
| 9 | + Modal, |
| 10 | + Popconfirm, |
| 11 | + Space, |
| 12 | + Spin, |
| 13 | + Table, |
| 14 | + Typography, |
| 15 | +} from 'antd'; |
| 16 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 17 | +import { JSONEditor, type Content, type OnChange } from 'vanilla-jsoneditor'; |
| 18 | +import { api } from '@/services/api'; |
| 19 | + |
| 20 | +const { Title } = Typography; |
| 21 | + |
| 22 | +interface ConfigItem { |
| 23 | + key: string; |
| 24 | + value: string; |
| 25 | +} |
| 26 | + |
| 27 | +// JSON Editor wrapper component |
| 28 | +const JsonEditorWrapper = ({ |
| 29 | + value, |
| 30 | + onChange, |
| 31 | +}: { |
| 32 | + value: string; |
| 33 | + onChange: (value: string) => void; |
| 34 | +}) => { |
| 35 | + const containerRef = useRef<HTMLDivElement>(null); |
| 36 | + const editorRef = useRef<JSONEditor | null>(null); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (containerRef.current && !editorRef.current) { |
| 40 | + const handleChange: OnChange = ( |
| 41 | + content: Content, |
| 42 | + _previousContent: Content, |
| 43 | + { contentErrors }, |
| 44 | + ) => { |
| 45 | + if (!contentErrors) { |
| 46 | + if ('json' in content && content.json !== undefined) { |
| 47 | + onChange(JSON.stringify(content.json, null, 2)); |
| 48 | + } else if ('text' in content) { |
| 49 | + onChange(content.text); |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + editorRef.current = new JSONEditor({ |
| 55 | + target: containerRef.current, |
| 56 | + props: { |
| 57 | + content: { text: value }, |
| 58 | + onChange: handleChange, |
| 59 | + mode: 'text', |
| 60 | + }, |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + return () => { |
| 65 | + if (editorRef.current) { |
| 66 | + editorRef.current.destroy(); |
| 67 | + editorRef.current = null; |
| 68 | + } |
| 69 | + }; |
| 70 | + }, []); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + if (editorRef.current) { |
| 74 | + editorRef.current.update({ text: value }); |
| 75 | + } |
| 76 | + }, [value]); |
| 77 | + |
| 78 | + return <div ref={containerRef} style={{ height: 300 }} />; |
| 79 | +}; |
| 80 | + |
| 81 | +export const Component = () => { |
| 82 | + const queryClient = useQueryClient(); |
| 83 | + const [isModalOpen, setIsModalOpen] = useState(false); |
| 84 | + const [editingItem, setEditingItem] = useState<ConfigItem | null>(null); |
| 85 | + const [form] = Form.useForm(); |
| 86 | + const [jsonValue, setJsonValue] = useState(''); |
| 87 | + |
| 88 | + const { data, isLoading, refetch } = useQuery({ |
| 89 | + queryKey: ['adminConfig'], |
| 90 | + queryFn: () => api.getAdminConfig(), |
| 91 | + }); |
| 92 | + |
| 93 | + const configList: ConfigItem[] = data?.data |
| 94 | + ? Object.entries(data.data).map(([key, value]) => ({ key, value })) |
| 95 | + : []; |
| 96 | + |
| 97 | + const handleAdd = () => { |
| 98 | + setEditingItem(null); |
| 99 | + form.resetFields(); |
| 100 | + setJsonValue(''); |
| 101 | + setIsModalOpen(true); |
| 102 | + }; |
| 103 | + |
| 104 | + const handleEdit = (record: ConfigItem) => { |
| 105 | + setEditingItem(record); |
| 106 | + form.setFieldsValue({ key: record.key }); |
| 107 | + // Pretty print JSON if possible |
| 108 | + try { |
| 109 | + setJsonValue(JSON.stringify(JSON.parse(record.value), null, 2)); |
| 110 | + } catch { |
| 111 | + setJsonValue(record.value); |
| 112 | + } |
| 113 | + setIsModalOpen(true); |
| 114 | + }; |
| 115 | + |
| 116 | + const handleSave = async () => { |
| 117 | + try { |
| 118 | + const values = await form.validateFields(); |
| 119 | + const key = values.key; |
| 120 | + |
| 121 | + // Validate JSON and submit a compact string |
| 122 | + let parsedValue: unknown; |
| 123 | + try { |
| 124 | + parsedValue = JSON.parse(jsonValue); |
| 125 | + } catch { |
| 126 | + message.error('请输入有效的 JSON 格式'); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const compactValue = JSON.stringify(parsedValue); |
| 131 | + await api.setAdminConfig(key, compactValue); |
| 132 | + message.success('保存成功'); |
| 133 | + setIsModalOpen(false); |
| 134 | + queryClient.invalidateQueries({ queryKey: ['adminConfig'] }); |
| 135 | + } catch (error) { |
| 136 | + message.error((error as Error).message); |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + const handleDelete = useCallback( |
| 141 | + async (key: string) => { |
| 142 | + try { |
| 143 | + await api.deleteAdminConfig(key); |
| 144 | + message.success('已删除'); |
| 145 | + refetch(); |
| 146 | + } catch (error) { |
| 147 | + message.error((error as Error).message); |
| 148 | + } |
| 149 | + }, |
| 150 | + [refetch], |
| 151 | + ); |
| 152 | + |
| 153 | + const columns = [ |
| 154 | + { |
| 155 | + title: 'Key', |
| 156 | + dataIndex: 'key', |
| 157 | + key: 'key', |
| 158 | + width: 200, |
| 159 | + }, |
| 160 | + { |
| 161 | + title: 'Value', |
| 162 | + dataIndex: 'value', |
| 163 | + key: 'value', |
| 164 | + render: (value: string) => { |
| 165 | + try { |
| 166 | + const parsed = JSON.parse(value); |
| 167 | + return ( |
| 168 | + <pre className="m-0 max-h-24 overflow-auto text-xs bg-gray-100 p-2 rounded"> |
| 169 | + {JSON.stringify(parsed, null, 2)} |
| 170 | + </pre> |
| 171 | + ); |
| 172 | + } catch { |
| 173 | + return <span className="text-gray-600">{value}</span>; |
| 174 | + } |
| 175 | + }, |
| 176 | + }, |
| 177 | + { |
| 178 | + title: '操作', |
| 179 | + key: 'action', |
| 180 | + width: 150, |
| 181 | + render: (_: unknown, record: ConfigItem) => ( |
| 182 | + <Space> |
| 183 | + <Button type="link" onClick={() => handleEdit(record)}> |
| 184 | + 编辑 |
| 185 | + </Button> |
| 186 | + <Popconfirm |
| 187 | + title="确定删除此配置?" |
| 188 | + onConfirm={() => handleDelete(record.key)} |
| 189 | + > |
| 190 | + <Button type="link" danger icon={<DeleteOutlined />} /> |
| 191 | + </Popconfirm> |
| 192 | + </Space> |
| 193 | + ), |
| 194 | + }, |
| 195 | + ]; |
| 196 | + |
| 197 | + return ( |
| 198 | + <div className="p-6"> |
| 199 | + <Card> |
| 200 | + <div className="flex justify-between items-center mb-4"> |
| 201 | + <Title level={4} className="m-0!"> |
| 202 | + 动态配置管理 |
| 203 | + </Title> |
| 204 | + <Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}> |
| 205 | + 添加配置 |
| 206 | + </Button> |
| 207 | + </div> |
| 208 | + |
| 209 | + <Spin spinning={isLoading}> |
| 210 | + <Table |
| 211 | + dataSource={configList} |
| 212 | + columns={columns} |
| 213 | + rowKey="key" |
| 214 | + pagination={false} |
| 215 | + /> |
| 216 | + </Spin> |
| 217 | + </Card> |
| 218 | + |
| 219 | + <Modal |
| 220 | + title={editingItem ? '编辑配置' : '添加配置'} |
| 221 | + open={isModalOpen} |
| 222 | + onCancel={() => setIsModalOpen(false)} |
| 223 | + footer={[ |
| 224 | + <Button key="cancel" onClick={() => setIsModalOpen(false)}> |
| 225 | + 取消 |
| 226 | + </Button>, |
| 227 | + <Button |
| 228 | + key="save" |
| 229 | + type="primary" |
| 230 | + icon={<SaveOutlined />} |
| 231 | + onClick={handleSave} |
| 232 | + > |
| 233 | + 保存 |
| 234 | + </Button>, |
| 235 | + ]} |
| 236 | + width={700} |
| 237 | + > |
| 238 | + <Form form={form} layout="vertical"> |
| 239 | + <Form.Item |
| 240 | + name="key" |
| 241 | + label="Key" |
| 242 | + rules={[{ required: true, message: '请输入配置键名' }]} |
| 243 | + > |
| 244 | + <Input disabled={!!editingItem} placeholder="配置键名" /> |
| 245 | + </Form.Item> |
| 246 | + <Form.Item label="Value (JSON)"> |
| 247 | + <JsonEditorWrapper value={jsonValue} onChange={setJsonValue} /> |
| 248 | + </Form.Item> |
| 249 | + </Form> |
| 250 | + </Modal> |
| 251 | + </div> |
| 252 | + ); |
| 253 | +}; |
0 commit comments