File size: 8,375 Bytes
adc1e1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Copyright (c) 2025 Tethys Plex

This file is part of Veloera.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import React, { useState, useEffect } from 'react';
import { API, isMobile, showError, showSuccess } from '../../helpers';
import Title from '@douyinfe/semi-ui/lib/es/typography/title';
import { 
  Button, 
  Input, 
  SideSheet, 
  Space, 
  Spin, 
  TextArea,
  RadioGroup,
  Radio,
  Table,
  Tag,
  Descriptions
} from '@douyinfe/semi-ui';
import { useTranslation } from 'react-i18next';

const EditMessage = (props) => {
  const { t } = useTranslation();
  
  const [inputs, setInputs] = useState({
    title: '',
    content: '',
    format: 'markdown',
  });
  
  const [loading, setLoading] = useState(false);
  const [messageDetails, setMessageDetails] = useState(null);
  const [recipients, setRecipients] = useState([]);
  const [recipientLoading, setRecipientLoading] = useState(false);
  
  const { title, content, format } = inputs;

  const handleInputChange = (name, value) => {
    setInputs((inputs) => ({ ...inputs, [name]: value }));
  };

  const loadMessageDetails = async (messageId) => {
    if (!messageId) return;
    
    setLoading(true);
    try {
      const res = await API.get(`/api/admin/messages/${messageId}`);
      const { success, message, data } = res.data;
      if (success) {
        setMessageDetails(data);
        setInputs({
          title: data.title || '',
          content: data.content || '',
          format: data.format || 'markdown',
        });
        loadRecipients(messageId);
      } else {
        showError(message);
      }
    } catch (error) {
      showError(error.message);
    }
    setLoading(false);
  };

  const loadRecipients = async (messageId) => {
    setRecipientLoading(true);
    try {
      const res = await API.get(`/api/admin/messages/${messageId}/recipients`);
      const { success, message, data } = res.data;
      if (success) {
        setRecipients(data || []);
      } else {
        showError(message);
      }
    } catch (error) {
      showError(error.message);
    }
    setRecipientLoading(false);
  };

  useEffect(() => {
    if (props.visible && props.editingMessage?.id) {
      loadMessageDetails(props.editingMessage.id);
    }
  }, [props.visible, props.editingMessage?.id]);

  const recipientColumns = [
    {
      title: 'ID',
      dataIndex: 'user_id',
      width: 80,
    },
    {
      title: t('用户名'),
      dataIndex: 'username',
    },
    {
      title: t('显示名称'),
      dataIndex: 'display_name',
      render: (text) => text || '-',
    },
    {
      title: t('阅读状态'),
      dataIndex: 'read_at',
      width: 120,
      render: (text) => (
        <Tag color={text ? 'green' : 'orange'} size='small'>
          {text ? t('已读') : t('未读')}
        </Tag>
      ),
    },
    {
      title: t('阅读时间'),
      dataIndex: 'read_at',
      width: 180,
      render: (text) => {
        return text ? new Date(text).toLocaleString() : '-';
      },
    },
  ];

  const submit = async () => {
    setLoading(true);
    
    if (!inputs.title.trim()) {
      showError(t('请输入消息标题'));
      setLoading(false);
      return;
    }
    
    if (!inputs.content.trim()) {
      showError(t('请输入消息内容'));
      setLoading(false);
      return;
    }

    const messageData = {
      title: inputs.title.trim(),
      content: inputs.content.trim(),
      format: inputs.format,
    };

    try {
      const res = await API.put(`/api/admin/messages/${props.editingMessage.id}`, messageData);
      const { success, message } = res.data;
      if (success) {
        showSuccess(t('消息更新成功'));
        props.refresh();
        props.handleClose();
      } else {
        showError(message);
      }
    } catch (error) {
      showError(error.message);
    }
    setLoading(false);
  };

  const handleCancel = () => {
    setInputs({
      title: '',
      content: '',
      format: 'markdown',
    });
    setMessageDetails(null);
    setRecipients([]);
    props.handleClose();
  };

  return (
    <>
      <SideSheet
        placement={'right'}
        title={<Title level={3}>{t('编辑消息')}</Title>}
        headerStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
        bodyStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
        visible={props.visible}
        footer={
          <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
            <Space>
              <Button theme='solid' size={'large'} onClick={submit} loading={loading}>
                {t('更新消息')}
              </Button>
              <Button
                theme='solid'
                size={'large'}
                type={'tertiary'}
                onClick={handleCancel}
              >
                {t('取消')}
              </Button>
            </Space>
          </div>
        }
        closeIcon={null}
        onCancel={() => handleCancel()}
        width={isMobile() ? '100%' : 900}
      >
        <Spin spinning={loading}>
          <div style={{ padding: '20px 0' }}>
            {messageDetails && (
              <div style={{ marginBottom: 24 }}>
                <Descriptions
                  data={[
                    { key: t('消息ID'), value: messageDetails.id },
                    { key: t('创建时间'), value: new Date(messageDetails.created_at).toLocaleString() },
                    { key: t('收件人数'), value: recipients.length },
                    { key: t('已读人数'), value: recipients.filter(r => r.read_at).length },
                  ]}
                  row
                  size="small"
                />
              </div>
            )}

            <Input
              label={t('消息标题')}
              placeholder={t('请输入消息标题')}
              value={title}
              onChange={(value) => handleInputChange('title', value)}
              style={{ marginBottom: 20 }}
            />

            <div style={{ marginBottom: 20 }}>
              <label style={{ display: 'block', marginBottom: 8, fontWeight: 600 }}>
                {t('内容格式')}
              </label>
              <RadioGroup
                type="button"
                value={format}
                onChange={(e) => handleInputChange('format', e.target.value)}
              >
                <Radio value="markdown">Markdown</Radio>
                <Radio value="html">HTML</Radio>
              </RadioGroup>
            </div>

            <div style={{ marginBottom: 20 }}>
              <label style={{ display: 'block', marginBottom: 8, fontWeight: 600 }}>
                {t('消息内容')}
              </label>
              <TextArea
                placeholder={format === 'html' ? t('请输入HTML格式的消息内容') : t('请输入Markdown格式的消息内容')}
                value={content}
                onChange={(value) => handleInputChange('content', value)}
                rows={8}
                style={{ fontFamily: 'monospace' }}
              />
            </div>

            <div>
              <label style={{ display: 'block', marginBottom: 8, fontWeight: 600 }}>
                {t('收件人列表')}
              </label>
              
              <Table
                columns={recipientColumns}
                dataSource={recipients}
                loading={recipientLoading}
                pagination={{
                  pageSize: 10,
                  showSizeChanger: false,
                  size: 'small',
                }}
                size="small"
                style={{ maxHeight: 400, overflow: 'auto' }}
              />
            </div>
          </div>
        </Spin>
      </SideSheet>
    </>
  );
};

export default EditMessage;