/*
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) => (