OfficialSiteMethodologyPage.tsx
22.4 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import { useEffect, useState } from 'react';
import { Button, FormItem, Input, MediaWall, StatePlaceholder, Switch, Textarea } from '../../../packages/ui/src';
import {
getMethodologyConfig,
saveMethodologyConfig,
uploadMethodologyAsset,
type MethodologyConfig,
type MethodologyStep,
type MethodologyStatus,
type MethodologyStep as ApiMethodologyStep,
} from '../../api/methodology';
interface MethodologyStepFormState {
step_id: string;
step_icon?: { id: string; name: string; url: string };
step_name: string;
step_order: number;
step_image?: { id: string; name: string; url: string; mime_type?: string; size_bytes?: number };
iconError?: string;
nameError?: string;
imageError?: string;
}
interface MethodologyFormState {
display_status: MethodologyStatus;
main_title: string;
sub_title: string;
steps: MethodologyStepFormState[];
}
interface SaveNotice {
type: 'success' | 'error';
message: string;
}
const contentImageHelperText = '支持 PNG/JPG/JPEG,最大 10MB';
const stepIconHelperText = '支持 PNG/JPG/JPEG,最大 2MB';
function normalizeSteps(steps: MethodologyStepFormState[]) {
return steps.map((step, index) => ({
...step,
step_order: index + 1,
}));
}
function buildFormState(record: MethodologyConfig): MethodologyFormState {
return {
display_status: record.display_status,
main_title: record.main_title,
sub_title: record.sub_title,
steps: normalizeSteps(record.steps.map((step) => ({
step_id: step.step_id,
step_icon: step.step_icon ? { id: step.step_id, name: '', url: step.step_icon } : undefined,
step_name: step.step_name,
step_order: step.step_order,
step_image: step.step_image ? { id: step.step_id, name: '', url: step.step_image } : undefined,
iconError: undefined,
imageError: undefined,
nameError: undefined,
}))),
};
}
function createEmptyStep(nextOrder: number): MethodologyStepFormState {
return {
step_id: `new-step-${nextOrder}-${Date.now()}`,
step_name: '',
step_order: nextOrder,
};
}
function buildDefaultIcon(stepOrder: number) {
const svgContent = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><circle cx="24" cy="24" r="22" fill="%232B6CB0"/><text x="24" y="30" text-anchor="middle" fill="white" font-size="18" font-weight="bold" font-family="system-ui">${stepOrder}</text></svg>`;
const dataUrl = `data:image/svg+xml,${encodeURIComponent(svgContent)}`;
return {
id: `default-icon-${stepOrder}`,
name: `默认图标(步骤${stepOrder})`,
url: dataUrl,
};
}
export function OfficialSiteMethodologyPage() {
const [loading, setLoading] = useState(true);
const [form, setForm] = useState<MethodologyFormState | null>(null);
const [loadError, setLoadError] = useState(false);
const [saving, setSaving] = useState(false);
const [notice, setNotice] = useState<SaveNotice | null>(null);
const [mainTitleError, setMainTitleError] = useState<string>();
const [subTitleError, setSubTitleError] = useState<string>();
const [stepListError, setStepListError] = useState<string>();
useEffect(() => {
setLoading(true);
setLoadError(false);
setNotice(null);
getMethodologyConfig()
.then((record) => {
setForm(buildFormState(record));
setMainTitleError(undefined);
setSubTitleError(undefined);
setStepListError(undefined);
setLoading(false);
})
.catch(() => {
setLoadError(true);
setLoading(false);
});
}, []);
const updateForm = (updater: (current: MethodologyFormState) => MethodologyFormState) => {
setForm((current) => (current ? updater(current) : current));
};
const updateStep = (index: number, updater: (step: MethodologyStepFormState) => MethodologyStepFormState) => {
updateForm((current) => ({
...current,
steps: normalizeSteps(current.steps.map((step, stepIndex) => (stepIndex === index ? updater(step) : step))),
}));
};
const validate = () => {
if (!form) return false;
let valid = true;
const trimmedMainTitle = form.main_title.trim();
const trimmedSubTitle = form.sub_title.trim();
setMainTitleError(undefined);
setSubTitleError(undefined);
setStepListError(undefined);
if (!trimmedMainTitle) {
setMainTitleError('请输入主标题');
valid = false;
} else if (trimmedMainTitle.length > 30) {
setMainTitleError('主标题不能超过 30 字');
valid = false;
}
if (!trimmedSubTitle) {
setSubTitleError('请输入副标题');
valid = false;
} else if (trimmedSubTitle.length > 100) {
setSubTitleError('副标题不能超过 100 字');
valid = false;
}
if (form.display_status === 'enable' && form.steps.length === 0) {
setStepListError('至少添加一个方法论步骤');
valid = false;
}
const nextSteps = form.steps.map((step) => {
let iconError: string | undefined;
let nameError: string | undefined;
let imageError: string | undefined;
if (!step.step_icon) {
iconError = '请配置步骤图标';
valid = false;
}
if (!step.step_name.trim()) {
nameError = '请输入步骤名称';
valid = false;
} else if (step.step_name.trim().length > 20) {
nameError = '步骤名称不能超过 20 字';
valid = false;
}
if (!step.step_image) {
imageError = '请上传内容图';
valid = false;
}
return {
...step,
iconError,
imageError,
nameError,
};
});
setForm((current) => (current ? { ...current, steps: nextSteps } : current));
return valid;
};
const handleSave = async () => {
if (!form || saving) {
return;
}
setNotice(null);
if (!validate()) {
return;
}
setSaving(true);
try {
await saveMethodologyConfig({
display_status: form.display_status,
main_title: form.main_title,
sub_title: form.sub_title,
steps: form.steps.map((step) => ({
step_icon: step.step_icon?.url ?? '',
step_name: step.step_name,
step_order: step.step_order,
step_image: step.step_image?.url ?? '',
})),
});
setNotice({ type: 'success', message: '保存成功' });
} catch {
setNotice({ type: 'error', message: '保存失败,请重试' });
}
setSaving(false);
};
const renderContent = () => {
if (loading) {
return <StatePlaceholder state="empty" title="加载中" description="正在加载方法论配置" />;
}
if (loadError || !form) {
return (
<StatePlaceholder
state="error"
title="加载失败"
description="方法论配置加载失败,请重试"
primaryAction={<Button size="small" onClick={() => {
setLoading(true);
setLoadError(false);
getMethodologyConfig()
.then((record) => {
setForm(buildFormState(record));
setLoading(false);
})
.catch(() => {
setLoadError(true);
setLoading(false);
});
}}>重试</Button>}
/>
);
}
return (
<>
{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}
<div className="rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] px-8 py-8">
<div className="grid gap-6">
<FormItem label="展示状态" layout="horizontal" required error={undefined}>
<Switch
aria-label="展示状态"
checked={form.display_status === 'enable'}
onChange={(checked) => {
setNotice(null);
updateForm((current) => ({
...current,
display_status: checked ? 'enable' : 'disable',
}));
}}
/>
</FormItem>
<FormItem label="主标题" layout="horizontal" required error={mainTitleError}>
<Input
aria-label="主标题"
className="w-full max-w-[520px]"
maxLength={30}
showCount
value={form.main_title}
onChange={(value) => {
setMainTitleError(undefined);
updateForm((current) => ({
...current,
main_title: value,
}));
}}
/>
</FormItem>
<FormItem label="副标题" layout="horizontal" required error={subTitleError}>
<Textarea
aria-label="副标题"
className="w-full max-w-[520px]"
maxLength={100}
rows={4}
showCount
value={form.sub_title}
onChange={(value) => {
setSubTitleError(undefined);
updateForm((current) => ({
...current,
sub_title: value,
}));
}}
/>
</FormItem>
</div>
</div>
<div className="rounded-[var(--gl-radius-md)] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-container)] px-8 py-8">
<div className="flex items-center justify-between gap-4">
<div className="grid gap-1">
<h3 className="m-0 text-lg leading-[var(--gl-line-height-lg)]">方法论步骤</h3>
<p className="m-0 text-sm text-[var(--gl-color-text-secondary)]">配置步骤图标、步骤名称与内容图。</p>
</div>
<Button
variant="primary"
onClick={() => {
setStepListError(undefined);
updateForm((current) => ({
...current,
steps: normalizeSteps([...current.steps, createEmptyStep(current.steps.length + 1)]),
}));
}}
>
添加步骤
</Button>
</div>
{stepListError ? <p className="mb-0 mt-4 text-sm text-[var(--gl-color-danger-text)]">{stepListError}</p> : null}
{form.steps.length === 0 ? (
<div className="mt-6">
<StatePlaceholder state="empty" title="当前还没有方法论步骤" description="请先添加步骤,再配置图标、名称和内容图。" />
</div>
) : (
<div className="mt-6 grid gap-4">
{form.steps.map((step, index) => (
<section
key={step.step_id}
aria-label={`方法论步骤 ${index + 1}`}
className="grid gap-5 rounded-[8px] border border-[var(--gl-color-border-secondary)] bg-[var(--gl-color-bg-page)] p-5"
data-testid="methodology-step-card"
>
<div className="flex flex-wrap items-center justify-between gap-3">
<div className="grid gap-1">
<strong>步骤 {index + 1}</strong>
<span className="text-sm text-[var(--gl-color-text-secondary)]">{`sort_order = ${step.step_order}`}</span>
</div>
<div className="flex flex-wrap items-center gap-2">
<Button
size="small"
variant="outline"
disabled={index === 0}
aria-label={`上移步骤 ${index + 1}`}
onClick={() => {
if (index === 0) return;
updateForm((current) => {
const nextSteps = [...current.steps];
[nextSteps[index - 1], nextSteps[index]] = [nextSteps[index], nextSteps[index - 1]];
return { ...current, steps: normalizeSteps(nextSteps) };
});
}}
>
上移
</Button>
<Button
size="small"
variant="outline"
disabled={index === form.steps.length - 1}
aria-label={`下移步骤 ${index + 1}`}
onClick={() => {
if (index === form.steps.length - 1) return;
updateForm((current) => {
const nextSteps = [...current.steps];
[nextSteps[index], nextSteps[index + 1]] = [nextSteps[index + 1], nextSteps[index]];
return { ...current, steps: normalizeSteps(nextSteps) };
});
}}
>
下移
</Button>
<Button
size="small"
variant="danger"
aria-label={`删除步骤 ${index + 1}`}
onClick={() => {
updateForm((current) => ({
...current,
steps: normalizeSteps(current.steps.filter((_, currentIndex) => currentIndex !== index)),
}));
}}
>
删除
</Button>
</div>
</div>
<FormItem label="步骤图标" required error={step.iconError} help={stepIconHelperText}>
<div className="grid gap-3">
<MediaWall
accept="image/png,image/jpg,image/jpeg"
items={step.step_icon ? [{
id: step.step_icon.id,
type: 'image' as const,
src: step.step_icon.url,
alt: step.step_icon.name,
}] : []}
maxCount={1}
multiple={false}
onRemove={() => {
updateStep(index, (currentStep) => ({
...currentStep,
step_icon: undefined,
}));
}}
onSelectFiles={(files) => {
const selectedFile = files[0];
if (!selectedFile) return;
const acceptedTypes = ['image/png', 'image/jpg', 'image/jpeg'];
if (!acceptedTypes.includes(selectedFile.type)) {
updateStep(index, (currentStep) => ({
...currentStep,
step_icon: undefined,
iconError: '步骤图标仅支持 PNG/JPG/JPEG',
}));
return;
}
if (selectedFile.size > 2 * 1024 * 1024) {
updateStep(index, (currentStep) => ({
...currentStep,
step_icon: undefined,
iconError: '步骤图标大小不能超过 2MB',
}));
return;
}
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('usage_type', 'methodology_icon');
uploadMethodologyAsset(formData)
.then((result) => {
updateStep(index, (currentStep) => ({
...currentStep,
step_icon: {
id: result.file_id,
name: selectedFile.name,
url: result.file_url,
},
iconError: undefined,
}));
})
.catch(() => {
updateStep(index, (currentStep) => ({
...currentStep,
step_icon: undefined,
iconError: '步骤图标上传失败',
}));
});
}}
/>
<div className="flex flex-wrap items-center gap-2">
<Button
size="small"
variant="outline"
aria-label={`使用默认图标 步骤 ${index + 1}`}
onClick={() => {
updateStep(index, (currentStep) => ({
...currentStep,
step_icon: buildDefaultIcon(currentStep.step_order),
iconError: undefined,
}));
}}
>
使用默认图标
</Button>
</div>
</div>
</FormItem>
<FormItem label="步骤名称" required error={step.nameError}>
<Input
aria-label={`步骤${index + 1}名称`}
className="w-full max-w-[360px]"
maxLength={20}
showCount
value={step.step_name}
onChange={(value) => {
updateStep(index, (currentStep) => ({
...currentStep,
step_name: value,
nameError: undefined,
}));
}}
/>
</FormItem>
<FormItem label="内容图" required error={step.imageError} help={contentImageHelperText}>
<MediaWall
accept="image/png,image/jpg,image/jpeg"
items={step.step_image ? [{
id: step.step_image.id,
type: 'image' as const,
src: step.step_image.url,
alt: step.step_image.name,
}] : []}
maxCount={1}
multiple={false}
onRemove={() => {
updateStep(index, (currentStep) => ({
...currentStep,
step_image: undefined,
}));
}}
onSelectFiles={(files) => {
const selectedFile = files[0];
if (!selectedFile) return;
const acceptedTypes = ['image/png', 'image/jpg', 'image/jpeg'];
if (!acceptedTypes.includes(selectedFile.type)) {
updateStep(index, (currentStep) => ({
...currentStep,
step_image: undefined,
imageError: '内容图仅支持 PNG/JPG/JPEG',
}));
return;
}
if (selectedFile.size > 10 * 1024 * 1024) {
updateStep(index, (currentStep) => ({
...currentStep,
step_image: undefined,
imageError: '内容图大小不能超过 10MB',
}));
return;
}
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('usage_type', 'methodology_image');
uploadMethodologyAsset(formData)
.then((result) => {
updateStep(index, (currentStep) => ({
...currentStep,
step_image: {
id: result.file_id,
name: selectedFile.name,
url: result.file_url,
mime_type: selectedFile.type,
size_bytes: result.file_size,
},
imageError: undefined,
}));
})
.catch(() => {
updateStep(index, (currentStep) => ({
...currentStep,
step_image: undefined,
imageError: '内容图上传失败',
}));
});
}}
/>
</FormItem>
</section>
))}
</div>
)}
</div>
</>
);
};
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>
<h2 className="m-0 text-2xl leading-[var(--gl-line-height-xl)]">方法论展示配置</h2>
<p className="m-0 mt-1 text-sm text-[var(--gl-color-text-secondary)]">维护官网方法论模块的展示状态、标题和步骤内容。</p>
</div>
<Button variant="primary" loading={saving} aria-label="保存配置" onClick={handleSave}>
保存配置
</Button>
</header>
{renderContent()}
</section>
);
}