OfficialSiteAgentDisplayConfigPage.tsx
12.5 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { useCallback, useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { Button, StatePlaceholder, Table } from '../../../packages/ui/src';
import {
getConfiguredAgents,
deleteAgentDisplayConfig,
type OfficialSiteConfiguredAgent,
type AgentStatus,
type DisplayStatus,
} from '../../api/officialSiteAgentDisplay';
import { AgentDisplayConfigEditModal } from './AgentDisplayConfigEditModal';
function mapAgentStatusToText(status: AgentStatus): string {
return status === 'enable' ? '已上架' : '已停用';
}
function getDisplayStatusTextForList(agentStatus: AgentStatus, displayStatus: DisplayStatus): string {
if (agentStatus === 'disable') return '-';
return displayStatus === 'enable' ? '展示中' : '已下架';
}
export function OfficialSiteAgentDisplayConfigPage() {
const navigate = useNavigate();
const params = useParams();
const tabId = Number(params.tabId);
const [loading, setLoading] = useState(true);
const [loadError, setLoadError] = useState(false);
const [tabContext, setTabContext] = useState<{
position_name: string;
qrcode_name: string;
channel_code: string;
employee_name: string;
employee_role_text: string;
} | null>(null);
const [items, setItems] = useState<OfficialSiteConfiguredAgent[]>([]);
const [editingAgent, setEditingAgent] = useState<OfficialSiteConfiguredAgent | null>(null);
const [notice, setNotice] = useState<{ type: 'success' | 'error'; message: string } | null>(null);
const fetchConfiguredAgents = useCallback(() => {
setLoading(true);
setLoadError(false);
getConfiguredAgents(tabId)
.then((response) => {
setTabContext({
position_name: response.position_name,
qrcode_name: response.qrcode_name,
channel_code: response.channel_code,
employee_name: response.employee_name,
employee_role_text: response.employee_role_text,
});
setItems(response.configured_agents);
setLoading(false);
})
.catch(() => {
setLoadError(true);
setLoading(false);
});
}, [tabId]);
useEffect(() => {
fetchConfiguredAgents();
}, [fetchConfiguredAgents]);
const goBackToList = () => {
navigate('/official-site/job-tabs');
};
const handleEditAgent = (agent: OfficialSiteConfiguredAgent) => {
setEditingAgent(agent);
};
const handleDeleteAgent = async (agent: OfficialSiteConfiguredAgent) => {
try {
await deleteAgentDisplayConfig(tabId, Number(agent.agent_id));
setNotice({ type: 'success', message: '删除成功' });
fetchConfiguredAgents();
} catch (error) {
const message = error instanceof Error ? error.message : '删除失败,请重试';
setNotice({ type: 'error', message });
}
};
// Loading state
if (loading) {
return (
<section className="grid gap-6">
<header className="flex items-center justify-between gap-6 rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] px-8 py-6">
<div className="flex items-center gap-4">
<Button variant="outline" onClick={goBackToList}>返回</Button>
<div>
<h2 className="m-0 text-2xl leading-[var(--gl-line-height-xl)]">Agent展示配置</h2>
</div>
</div>
</header>
<div className="grid gap-4 rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] p-6">
<p className="text-sm text-[var(--gl-color-text-secondary)]">加载中...</p>
</div>
</section>
);
}
// Error state
if (loadError) {
return (
<section className="grid gap-6">
<header className="flex items-center justify-between gap-6 rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] px-8 py-6">
<div className="flex items-center gap-4">
<Button variant="outline" onClick={goBackToList}>返回</Button>
<div>
<h2 className="m-0 text-2xl leading-[var(--gl-line-height-xl)]">Agent展示配置</h2>
</div>
</div>
</header>
<div className="grid gap-4 rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] p-6">
<StatePlaceholder
state="error"
title="加载失败"
description="Agent展示配置列表加载失败,请重试"
primaryAction={<Button size="small" onClick={fetchConfiguredAgents}>重试</Button>}
/>
</div>
</section>
);
}
const columns = [
{
title: 'Agent名称',
dataIndex: 'speech_title' as const,
render: (value: unknown) => <span className="font-medium">{value as string}</span>,
},
{
title: 'Agent概述',
dataIndex: 'speech_content' as const,
render: (value: unknown) => (
<span className="max-w-[200px] truncate">{(value as string) || '-'}</span>
),
},
{
title: '图标',
dataIndex: 'icon_url' as const,
render: (value: unknown, item: OfficialSiteConfiguredAgent) => {
// Use media_url as icon if icon_url not available
const url = (value as string) || item.media_url;
if (!url) {
return <span className="text-[var(--gl-color-text-secondary)]">-</span>;
}
return (
<img
data-testid={`agent-icon-${item.agent_id}`}
src={url}
alt={item.speech_title}
className="w-8 h-8 rounded object-cover"
/>
);
},
},
{
title: '展示素材',
dataIndex: 'media_url' as const,
render: (value: unknown, item: OfficialSiteConfiguredAgent) => {
const url = value as string;
if (!url) {
return <span className="text-[var(--gl-color-text-secondary)]">-</span>;
}
const typeLabels: Record<string, string> = {
'image': '图片',
'video': '视频',
'gif': 'GIF',
'png': '图片',
'jpg': '图片',
'jpeg': '图片',
'mp4': '视频',
};
return (
<div className="flex items-center gap-2">
{item.media_type === 'mp4' ? (
<video
src={url}
className="w-8 h-8 rounded object-cover"
muted
/>
) : (
<img
src={url}
alt={item.speech_title}
className="w-8 h-8 rounded object-cover"
/>
)}
<span className="text-xs text-[var(--gl-color-text-secondary)]">
{typeLabels[item.media_type] || '素材'}
</span>
</div>
);
},
},
{
title: '排序',
dataIndex: 'sort_order' as const,
render: (value: unknown) => <span>{value as number}</span>,
},
{
title: '状态',
dataIndex: 'agent_status' as const,
render: (value: unknown) => {
const status = value as AgentStatus;
const text = mapAgentStatusToText(status);
return status === 'enable'
? <span className="inline-flex items-center rounded-full bg-[var(--gl-color-success-bg)] px-2 py-1 text-xs text-[var(--gl-color-success-text)]">{text}</span>
: <span className="inline-flex items-center rounded-full bg-[var(--gl-color-warning-bg)] px-2 py-1 text-xs text-[var(--gl-color-warning-text)]">{text}</span>;
},
},
{
title: '展示状态',
key: 'display_status_display',
render: (_: unknown, item: OfficialSiteConfiguredAgent) => {
const text = getDisplayStatusTextForList(item.agent_status, item.display_status);
if (text === '-') {
return <span className="text-[var(--gl-color-text-secondary)]">-</span>;
}
return item.display_status === 'enable'
? <span className="inline-flex items-center rounded-full bg-[var(--gl-color-success-bg)] px-2 py-1 text-xs text-[var(--gl-color-success-text)]">{text}</span>
: <span className="inline-flex items-center rounded-full bg-[var(--gl-color-warning-bg)] px-2 py-1 text-xs text-[var(--gl-color-warning-text)]">{text}</span>;
},
},
{
title: '操作',
key: 'actions',
render: (_: unknown, item: OfficialSiteConfiguredAgent) => {
if (item.agent_status !== 'enable') {
return null;
}
return (
<span className="flex flex-wrap gap-1">
<Button
aria-label={`编辑配置-${item.speech_title}`}
size="small"
variant="text"
onClick={() => handleEditAgent(item)}
>
编辑配置
</Button>
{item.display_status === 'enable' ? (
<Button
aria-label={`删除配置-${item.speech_title}`}
size="small"
variant="danger"
onClick={() => handleDeleteAgent(item)}
>
删除配置
</Button>
) : null}
</span>
);
},
},
];
return (
<section className="grid gap-6">
<header className="flex items-center justify-between gap-6 rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] px-8 py-6">
<div className="flex items-center gap-4">
<Button variant="outline" onClick={goBackToList}>返回</Button>
<div>
<h2 className="m-0 text-2xl leading-[var(--gl-line-height-xl)]">
{tabContext?.position_name} - {tabContext?.qrcode_name} - Agent展示配置
</h2>
<p className="m-0 mt-1 text-sm text-[var(--gl-color-text-secondary)]">
配置该岗位下 Agent 在官网的展示素材和状态
</p>
</div>
</div>
</header>
{notice ? (
<div
className={`rounded-[var(--gl-radius-sm)] border px-4 py-3 text-sm ${
notice.type === 'error'
? 'border-[var(--gl-color-danger-border)] bg-[var(--gl-color-danger-bg)] text-[var(--gl-color-danger-text)]'
: 'border-[var(--gl-color-success-border)] bg-[var(--gl-color-success-bg)] text-[var(--gl-color-success-text)]'
}`}
role={notice.type === 'error' ? 'alert' : 'status'}
aria-label={notice.message}
>
{notice.message}
</div>
) : null}
{/* Tab context info */}
{tabContext ? (
<div className="grid gap-4 rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] px-8 py-6">
<div className="grid gap-4 text-sm">
<div className="flex items-center gap-2">
<span className="shrink-0 min-w-[80px] text-[var(--gl-color-text-secondary)]">关联渠道活码:</span>
<span>{tabContext.qrcode_name}</span>
</div>
<div className="flex items-center gap-2">
<span className="shrink-0 min-w-[80px] text-[var(--gl-color-text-secondary)]">渠道编码:</span>
<span>{tabContext.channel_code}</span>
</div>
<div className="flex items-center gap-2">
<span className="shrink-0 min-w-[80px] text-[var(--gl-color-text-secondary)]">承接员工:</span>
<span>{tabContext.employee_name}({tabContext.employee_role_text})</span>
</div>
</div>
</div>
) : null}
{/* Agent list table */}
<div className="grid gap-4 rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] p-6">
<p className="text-sm text-[var(--gl-color-text-secondary)]">
Agent 数据源来自该岗位关联的渠道活码。管家 Agent 不参与官网展示。
Agent 列表不可增删,仅可编辑展示配置。
</p>
<Table
rowKey="agent_id"
columns={columns}
dataSource={items}
empty={{
description: '当前无可配置 Agent',
}}
/>
</div>
{/* Edit modal */}
<AgentDisplayConfigEditModal
open={Boolean(editingAgent)}
agent={editingAgent}
tabId={tabId}
onCancel={() => setEditingAgent(null)}
onSaveSuccess={() => {
setEditingAgent(null);
setNotice({ type: 'success', message: '保存成功' });
fetchConfiguredAgents();
}}
/>
</section>
);
}