AgentDisplayConfigEditModal.tsx
8.11 KB
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
import { useState, useEffect } from 'react';
import { Button, FormItem, Input, MediaWall, Modal, Select } from '../../../packages/ui/src';
import {
saveAgentDisplayConfig,
uploadAgentMedia,
type AgentDisplayConfigMockItem,
} from '../../../packages/contracts/official-site-agent-display-config-mock';
interface EditModalProps {
open: boolean;
agent: AgentDisplayConfigMockItem | null;
tabId: number;
scenario: string;
onCancel: () => void;
onSaveSuccess: () => void;
}
interface EditFormState {
speech_title: string;
speech_content: string;
sort_order: number;
display_status: 'enable' | 'disable';
icon_url: string;
media_url: string;
media_type: 'image' | 'video' | 'gif';
}
interface EditFormErrors {
speech_content?: string;
sort_order?: string;
icon_url?: string;
speech_title?: string;
media_url?: string;
}
export function AgentDisplayConfigEditModal({
open,
agent,
tabId,
scenario,
onCancel,
onSaveSuccess,
}: EditModalProps) {
const [form, setForm] = useState<EditFormState>({
speech_title: '',
speech_content: '',
sort_order: 10,
display_status: 'disable',
icon_url: '',
media_url: '',
media_type: 'image',
});
const [errors, setErrors] = useState<EditFormErrors>({});
const [saving, setSaving] = useState(false);
// Reset form when agent changes
useEffect(() => {
if (agent) {
setForm({
speech_title: agent.speech_title ?? '',
speech_content: agent.speech_content ?? '',
sort_order: agent.sort_order ?? 10,
display_status: agent.display_status ?? 'disable',
icon_url: agent.icon_url ?? '',
media_url: agent.media_url ?? '',
media_type: agent.media_type ?? 'image',
});
setErrors({});
}
}, [agent]);
const validate = () => {
const nextErrors: EditFormErrors = {};
if (!form.icon_url.trim()) {
nextErrors.icon_url = '图标必填';
}
if (!form.speech_title.trim()) {
nextErrors.speech_title = 'Agent名称必填';
}
if (!form.speech_content.trim()) {
nextErrors.speech_content = 'Agent概述必填';
} else if (form.speech_content.length > 100) {
nextErrors.speech_content = 'Agent概述不能超过100字';
}
if (!form.media_url.trim()) {
nextErrors.media_url = '展示素材必填';
}
if (form.sort_order < 0 || form.sort_order > 99 || !Number.isInteger(form.sort_order)) {
nextErrors.sort_order = '排序请输入0-99的整数';
}
setErrors(nextErrors);
return Object.keys(nextErrors).length === 0;
};
const handleSave = async () => {
if (!agent) return;
if (!validate()) return;
setSaving(true);
const result = saveAgentDisplayConfig(tabId, {
agent_id: agent.agent_id,
speech_title: form.speech_title,
speech_content: form.speech_content,
display_status: form.display_status,
sort_order: form.sort_order,
icon_url: form.icon_url,
media_url: form.media_url,
media_type: form.media_type,
}, scenario);
if (result.blocked) {
setSaving(false);
if (result.validation_errors) {
setErrors({
speech_content: result.validation_errors.speech_content,
sort_order: result.validation_errors.sort_order,
});
}
return;
}
setSaving(false);
onSaveSuccess();
};
const handleIconUpload = (files: File[]) => {
if (files.length === 0) return;
const file = files[0];
const result = uploadAgentMedia(
{ name: file.name, size: file.size, type: file.type },
'icon',
scenario,
);
if (!result.blocked) {
setForm((current) => ({
...current,
icon_url: result.url,
}));
}
};
const handleMediaUpload = (files: File[]) => {
if (files.length === 0) return;
const file = files[0];
const result = uploadAgentMedia(
{ name: file.name, size: file.size, type: file.type },
'media',
scenario,
);
if (!result.blocked) {
setForm((current) => ({
...current,
media_url: result.url,
media_type: result.type,
}));
}
};
const handleRemoveIcon = () => {
setForm((current) => ({
...current,
icon_url: '',
}));
};
const handleRemoveMedia = () => {
setForm((current) => ({
...current,
media_url: '',
media_type: 'image',
}));
};
if (!agent) return null;
const iconItems: { id: string; type: 'image'; src: string; alt: string }[] = form.icon_url
? [{ id: 'icon', type: 'image' as const, src: form.icon_url, alt: '' }]
: [];
const mediaItems: { id: string; type: 'image' | 'video'; src: string; alt: string }[] = form.media_url
? [{ id: 'media', type: (agent.media_type === 'video' ? 'video' : 'image') as 'image' | 'video', src: form.media_url, alt: '' }]
: [];
return (
<Modal
open={open}
title={`编辑展示配置 - ${agent.speech_title}`}
onCancel={onCancel}
onOk={handleSave}
footer={
<div className="flex justify-end gap-3">
<Button variant="outline" onClick={onCancel} disabled={saving}>
取消
</Button>
<Button variant="primary" onClick={handleSave} disabled={saving}>
{saving ? '保存中...' : '确定'}
</Button>
</div>
}
>
<div className="grid gap-6 py-4">
<FormItem label="图标" layout="horizontal" required error={errors.icon_url}>
<MediaWall
accept="image/png,image/jpeg,image/jpg,image/gif"
maxCount={1}
multiple={false}
items={iconItems}
helperText="支持 PNG、JPG、GIF,单文件上限 5MB"
onSelectFiles={handleIconUpload}
onRemove={handleRemoveIcon}
/>
</FormItem>
<FormItem label="Agent 名称" layout="horizontal" required error={errors.speech_title}>
<Input
className="w-full max-w-[400px]"
placeholder="请输入Agent名称"
value={form.speech_title}
onChange={(value) => setForm((current) => ({ ...current, speech_title: value }))}
/>
</FormItem>
<FormItem label="Agent 概述" layout="horizontal" required error={errors.speech_content}>
<Input
className="w-full max-w-[400px]"
placeholder="请输入Agent概述"
value={form.speech_content}
onChange={(value) => setForm((current) => ({ ...current, speech_content: value }))}
/>
</FormItem>
<FormItem label="展示素材" layout="horizontal" required error={errors.media_url}>
<MediaWall
accept="image/png,image/jpeg,image/jpg,image/gif,video/mp4"
maxCount={1}
multiple={false}
items={mediaItems}
helperText="支持图片、视频、GIF,推荐 1:1,最大 20MB"
onSelectFiles={handleMediaUpload}
onRemove={handleRemoveMedia}
/>
</FormItem>
<FormItem label="展示排序" layout="horizontal" required error={errors.sort_order}>
<Input
className="w-full max-w-[400px]"
type="number"
min={0}
max={99}
placeholder="请输入展示排序"
value={form.sort_order === undefined || (typeof form.sort_order === 'number' && isNaN(form.sort_order)) ? '' : String(form.sort_order)}
onChange={(value) => setForm((current) => ({
...current,
sort_order: value === '' ? undefined as unknown as number : Number(value),
}))}
/>
</FormItem>
<FormItem label="展示状态" layout="horizontal" required>
<Select
className="w-full max-w-[400px]"
options={[
{ value: 'disable', label: '已下架' },
{ value: 'enable', label: '展示中' },
]}
value={form.display_status}
onChange={(value) => setForm((current) => ({
...current,
display_status: value as 'enable' | 'disable',
}))}
/>
</FormItem>
</div>
</Modal>
);
}