Files
monkey-ai-web/src/pages/WebsiteSettings/InsightsPanel.jsx
T
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

463 lines
14 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useTranslation } from 'react-i18next';
import TableEmpty from '../../components/common/table/TableEmpty';
import TableToolbar from '../../components/common/table/TableToolbar';
import { ConsoleFilterField } from '../../components/console/ConsoleModuleFilters';
import React, { useState } from 'react';
import {
Button,
Card,
Input,
Modal,
Select,
Typography,
} from '@/components/ui';
import {
IconPlus,
IconSearch,
IconEdit,
IconClose,
} from '@/components/ui/icons';
import CardTable from '../../components/common/ui/CardTable';
import { showError, showInfo } from '../../helpers';
import {
categoryError,
createContentId,
filterArticles,
moveArticle,
newArticle,
removeArticle,
} from './insightsModel';
import ArticleEditor from './ArticleEditor';
export default function InsightsPanel({ value, onChange, disabled, error }) {
const { t } = useTranslation();
const [keyword, setKeyword] = useState('');
const [category, setCategory] = useState('all');
const [managing, setManaging] = useState(false);
const [categoryDraft, setCategoryDraft] = useState(null);
const [categoryMessage, setCategoryMessage] = useState('');
const [article, setArticle] = useState(null);
const activeCategory = value.categories.some((c) => c.id === category)
? category
: 'all';
const rows = filterArticles(value, keyword, activeCategory);
// Reordering only makes sense on the full list, which is the official site order.
const sortable = activeCategory === 'all' && !keyword.trim();
const hasCategories = value.categories.length > 0;
const count = (id) =>
id === 'all'
? value.articles.length
: value.articles.filter((a) => a.category === id).length;
const selectCategory = (id) => {
setCategory(id);
};
const editCategory = (item = { id: '', name: '' }) => {
setCategoryMessage('');
setCategoryDraft({ ...item });
};
const saveCategory = () => {
const message = categoryError(
categoryDraft.name,
value.categories,
categoryDraft.id,
);
if (message) {
setCategoryMessage(message);
return;
}
const item = {
id: categoryDraft.id || createContentId(),
name: categoryDraft.name.trim(),
};
onChange({
...value,
categories: categoryDraft.id
? value.categories.map((c) => (c.id === item.id ? item : c))
: [...value.categories, item],
});
setCategoryDraft(null);
};
const deleteCategory = (item) => {
if (count(item.id)) {
showError('该分类下仍有文章,请先将文章调整到其他分类,再删除分类。');
return;
}
Modal.confirm({
title: '删除分类',
content: `确定删除“${item.name}”分类吗?点击页面底部“保存”后统一提交。`,
okText: '确定删除',
cancelText: '取消',
onOk: () => {
onChange({
...value,
categories: value.categories.filter((c) => c.id !== item.id),
});
if (category === item.id) selectCategory('all');
},
});
};
const deleteArticle = (item) =>
Modal.confirm({
title: '删除文章',
content: `确定删除“${item.title}”吗?删除后将同步清理其他文章中的推荐引用。点击页面底部“保存”后统一提交。`,
okText: '确定删除',
cancelText: '取消',
onOk: () => onChange(removeArticle(value, item.id)),
});
const applyArticle = (draft) => {
const next = { ...draft, id: draft.id || createContentId() };
onChange({
...value,
articles: draft.id
? value.articles.map((a) => (a.id === draft.id ? next : a))
: [next, ...value.articles],
});
setArticle(null);
showInfo('文章已应用到当前配置,请点击页面底部“保存”统一提交。');
};
const columns = [
{
title: '文章标题',
dataIndex: 'title',
width: 420,
render: (_, item) => (
<div className='website-insights-article-meta'>
<button
type='button'
className='website-insights-title'
title={item.title}
disabled={disabled}
onClick={() => setArticle({ ...item, related: [...item.related] })}
>
{item.title}
</button>
<Typography.Paragraph
type='tertiary'
className='truncate'
title={item.summary}
style={{ marginBottom: 0 }}
>
{item.summary}
</Typography.Paragraph>
</div>
),
},
{
title: '所属分类',
dataIndex: 'category',
width: 140,
render: (id) => value.categories.find((c) => c.id === id)?.name,
},
{
title: '角标',
dataIndex: 'tag',
width: 100,
render: (tag) => tag || '-',
},
{ title: '发布日期', dataIndex: 'date', width: 140 },
{
title: '操作',
key: 'actions',
width: 220,
render: (_, item) => {
const index = value.articles.findIndex((a) => a.id === item.id);
const move = (offset) => onChange(moveArticle(value, item.id, offset));
return (
<div className='website-insights-row-actions'>
<Button
type='link'
size='small'
disabled={disabled}
onClick={() =>
setArticle({ ...item, related: [...item.related] })
}
>
{t('编辑')}
</Button>
<Button
type='link'
size='small'
aria-label={`上移${item.title}`}
title={sortable ? undefined : '请在全部分类且无搜索时调整顺序'}
disabled={disabled || !sortable || index <= 0}
onClick={() => move(-1)}
>
{t('上移')}
</Button>
<Button
type='link'
size='small'
aria-label={`下移${item.title}`}
title={sortable ? undefined : '请在全部分类且无搜索时调整顺序'}
disabled={
disabled || !sortable || index >= value.articles.length - 1
}
onClick={() => move(1)}
>
{t('下移')}
</Button>
<Button
type='link'
size='small'
danger
disabled={disabled}
onClick={() => deleteArticle(item)}
>
{t('删除')}
</Button>
</div>
);
},
},
];
return (
<div className='website-insights'>
{error && (
<Typography.Paragraph type='danger' role='alert'>
{error}
</Typography.Paragraph>
)}
<Card
className='website-insights-categories'
title={
<Typography.Title
heading={6}
className='website-insights-category-heading truncate'
title='内容分类'
>
内容分类
</Typography.Title>
}
headerExtraContent={
<Button
type='tertiary'
theme='borderless'
disabled={disabled}
onClick={() => setManaging(true)}
>
{t('管理分类')}
</Button>
}
>
<div className='website-insights-category-list'>
{[{ id: 'all', name: '全部内容' }, ...value.categories].map((c) => (
<div
key={c.id}
className={`website-insights-category ${activeCategory === c.id ? 'is-selected' : ''}`}
>
<Button
type='tertiary'
theme='borderless'
aria-pressed={activeCategory === c.id}
onClick={() => selectCategory(c.id)}
>
<span>{c.name}</span>
<span className='website-insights-category-count'>
{count(c.id)}
</span>
</Button>
{c.id !== 'all' && (
<div className='website-insights-category-actions'>
<Button
type='tertiary'
icon={<IconEdit />}
size='small'
theme='borderless'
aria-label={`编辑${c.name}`}
disabled={disabled}
onClick={() => editCategory(c)}
/>
<Button
icon={<IconClose />}
size='small'
theme='borderless'
type='danger'
aria-label={`删除${c.name}`}
disabled={disabled}
onClick={() => deleteCategory(c)}
/>
</div>
)}
</div>
))}
<Button
type='tertiary'
icon={<IconPlus />}
theme='borderless'
disabled={disabled}
onClick={() => editCategory()}
>
{t('新增分类')}
</Button>
</div>
</Card>
<div className='website-insights-toolbar'>
<div className='website-insights-search-field'>
<ConsoleFilterField label='文章标题'>
<Input
className='website-insights-search'
prefix={<IconSearch />}
aria-label='搜索文章标题或摘要'
placeholder='搜索文章标题或摘要'
value={keyword}
showClear
onEnterPress={(event) => event.preventDefault()}
onChange={(text) => {
setKeyword(text);
}}
/>
</ConsoleFilterField>
</div>
</div>
<TableToolbar title='文章列表'>
<div className='website-insights-toolbar-actions'>
<Select
aria-label='筛选文章分类'
value={activeCategory}
optionList={[
{ label: '全部分类', value: 'all' },
...value.categories.map((c) => ({ label: c.name, value: c.id })),
]}
onChange={selectCategory}
/>
<Button
icon={<IconPlus />}
theme='solid'
disabled={disabled || !hasCategories}
title={hasCategories ? undefined : '请先新增内容分类'}
onClick={() => {
setArticle(newArticle(activeCategory));
}}
>
{t('新增文章')}
</Button>
</div>
</TableToolbar>
{!hasCategories && (
<Typography.Paragraph type='tertiary'>
请先新增内容分类,文章需归属一个分类后才能在官网展示。
</Typography.Paragraph>
)}
<Typography.Paragraph type='tertiary'>
列表顺序即官网展示顺序,新增文章默认排在最前,可在“全部分类”下上移或下移调整。
</Typography.Paragraph>
<CardTable
rowKey='id'
columns={columns}
dataSource={rows}
scroll={{ x: 1040 }}
empty={<TableEmpty />}
hidePagination
/>
<Modal
visible={managing}
title='管理分类'
onCancel={() => setManaging(false)}
footer={
<Button
icon={<IconPlus />}
theme='solid'
disabled={disabled}
onClick={() => editCategory()}
>
{t('新增分类')}
</Button>
}
>
<Typography.Paragraph type='tertiary'>
“全部内容”为官网默认筛选项,汇总展示全部文章,不能作为文章分类;已有文章的分类需先将文章调整到其他分类再删除。
</Typography.Paragraph>
<div className='website-insights-manage-row'>
<span>{t('全部内容')}</span>
<Typography.Text type='tertiary'>
默认筛选项(不可删除)
</Typography.Text>
</div>
{value.categories.map((c) => (
<div className='website-insights-manage-row' key={c.id}>
<span>
{c.name}({count(c.id)} {t('篇)')}
</span>
<Button
type='tertiary'
theme='borderless'
disabled={disabled}
onClick={() => editCategory(c)}
>
{t('编辑')}
</Button>
<Button
theme='borderless'
type='danger'
disabled={disabled}
onClick={() => deleteCategory(c)}
>
{t('删除')}
</Button>
</div>
))}
</Modal>
<Modal
visible={Boolean(categoryDraft)}
title={categoryDraft?.id ? '编辑分类' : '新增分类'}
onCancel={() => setCategoryDraft(null)}
onOk={saveCategory}
okText='确定'
cancelText='取消'
okButtonProps={{ disabled }}
>
<Typography.Paragraph
className='website-category-dialog-description'
type='tertiary'
>
分类名称将用于官网导航与文章分类标签。
</Typography.Paragraph>
<div className='website-category-dialog-field'>
<label
htmlFor='website-category-name'
className='website-category-dialog-label'
>
{t('分类名称')} <span aria-hidden='true'>*</span>
</label>
<Input
id='website-category-name'
aria-required='true'
aria-invalid={Boolean(categoryMessage)}
aria-describedby={
categoryMessage ? 'website-category-name-error' : undefined
}
aria-label='分类名称'
value={categoryDraft?.name || ''}
maxLength={12}
placeholder='请输入分类名称,最多 12 个字符'
validateStatus={categoryMessage ? 'error' : 'default'}
onChange={(name) => {
setCategoryDraft((previous) => ({ ...previous, name }));
setCategoryMessage('');
}}
onEnterPress={saveCategory}
/>
{categoryMessage && (
<Typography.Text
id='website-category-name-error'
type='danger'
role='alert'
>
{categoryMessage}
</Typography.Text>
)}
</div>
</Modal>
{article && (
<ArticleEditor
initial={article}
insights={value}
onApply={applyArticle}
onClose={() => setArticle(null)}
/>
)}
</div>
);
}