e70e0d2224
验证:868 项单测、ESLint、Prettier、UI 依赖审计和差异检查通过。
477 lines
15 KiB
React
477 lines
15 KiB
React
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import { TableRowActions } from '@/components/common/table/TableToolbar';
|
||
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 (
|
||
<TableRowActions
|
||
items={[
|
||
{
|
||
key: 'up',
|
||
label: t('上移'),
|
||
title: sortable ? undefined : '请在全部分类且无搜索时调整顺序',
|
||
disabled: disabled || !sortable || index <= 0,
|
||
onClick: () => move(-1),
|
||
},
|
||
{
|
||
key: 'down',
|
||
label: t('下移'),
|
||
title: sortable ? undefined : '请在全部分类且无搜索时调整顺序',
|
||
disabled:
|
||
disabled || !sortable || index >= value.articles.length - 1,
|
||
onClick: () => move(1),
|
||
},
|
||
{
|
||
key: 'delete',
|
||
label: t('删除'),
|
||
danger: true,
|
||
disabled,
|
||
onClick: () => deleteArticle(item),
|
||
},
|
||
]}
|
||
>
|
||
<Button
|
||
type='link'
|
||
size='small'
|
||
disabled={disabled}
|
||
onClick={() =>
|
||
setArticle({ ...item, related: [...item.related] })
|
||
}
|
||
>
|
||
{t('编辑')}
|
||
</Button>
|
||
</TableRowActions>
|
||
);
|
||
},
|
||
},
|
||
];
|
||
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>
|
||
);
|
||
}
|