/* 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 . 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) => (
{item.summary}
), }, { 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 ( 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), }, ]} > ); }, }, ]; return (
{error && ( {error} )} 内容分类 } headerExtraContent={ } >
{[{ id: 'all', name: '全部内容' }, ...value.categories].map((c) => (
{c.id !== 'all' && (
)}
))}
} aria-label='搜索文章标题或摘要' placeholder='搜索文章标题或摘要' value={keyword} showClear onEnterPress={(event) => event.preventDefault()} onChange={(text) => { setKeyword(text); }} />
{ setCategoryDraft((previous) => ({ ...previous, name })); setCategoryMessage(''); }} onEnterPress={saveCategory} /> {categoryMessage && ( {categoryMessage} )}
{article && ( setArticle(null)} /> )}
); }