Files
PingChuan 5bee9288b9 feat:替换 Dify 为自建 RAG去实现
1、修复了若干无权限时的失败提示语
2、新增了一个生成后续建议问题的功能
3、重构了知识问答部分的权限管理模块
4、修复了若干渲染不恰当的样式渲染
2026-04-10 16:20:32 +08:00

284 lines
10 KiB
TypeScript

import {
CloudUploadOutlined,
DeleteOutlined,
FileTextOutlined,
ReloadOutlined,
SearchOutlined,
UnorderedListOutlined
} from '@ant-design/icons';
import {
Button,
Empty,
Input,
Popconfirm,
Space,
Spin,
Switch,
Table,
Tag,
Tooltip,
} from 'antd';
import type { ColumnsType } from 'antd/es/table';
import type { Document, IndexingStatus } from '~/api/dify-dataset/type/documentTypes';
import { useDocumentList } from '~/hooks/dify-dataset-manager/document-list';
import type { DocumentListProps } from '~/types/dify-dataset-manager/document-list';
import { usePermission } from '~/hooks/usePermission';
import '../../styles/components/dify-dataset-manager/index.css';
import DocumentUpload from './document-upload';
/**
* 文档列表组件
*/
export default function DocumentList({
datasetId,
documents,
loading,
total,
page,
pageSize,
onPageChange,
onDocumentDeleted,
onDocumentStatusChanged,
onRefresh,
onViewDocument,
canEditDataset = true,
}: DocumentListProps) {
const {
searchValue,
setSearchValue,
deletingId,
showUploadPage,
getStatusConfig,
formatDate,
formatNumber,
handleDelete,
handleToggleStatus,
handleUploadClick,
handleUploadClose,
handleUploadSuccess,
filterDocuments,
} = useDocumentList(datasetId, onDocumentDeleted, onDocumentStatusChanged, onRefresh);
const { hasPermission } = usePermission();
const canWrite = hasPermission('dify:document:manage') && canEditDataset;
// 过滤文档
const filteredDocuments = filterDocuments(documents);
// 表格列定义
const columns: ColumnsType<Document> = [
{
title: '文档名称',
dataIndex: 'name',
key: 'name',
ellipsis: true,
render: (name: string) => (
<div className="flex items-center gap-2">
<FileTextOutlined className="text-gray-400" />
<span className="font-medium">{name}</span>
</div>
),
},
{
title: '状态',
dataIndex: 'indexing_status',
key: 'indexing_status',
width: 120,
render: (status: IndexingStatus) => {
const config = getStatusConfig(status);
return (
<Tag color={config.color} icon={config.icon}>
{config.text}
</Tag>
);
},
},
{
title: '字数',
dataIndex: 'word_count',
key: 'word_count',
width: 100,
render: (count: number) => formatNumber(count),
},
{
title: '命中次数',
dataIndex: 'hit_count',
key: 'hit_count',
width: 100,
render: (count: number) => formatNumber(count),
},
{
title: '启用',
dataIndex: 'enabled',
key: 'enabled',
width: 80,
render: (enabled: boolean, record) => (
<Switch
size="small"
checked={enabled}
disabled={!canWrite}
onChange={(checked) => handleToggleStatus(record.id, checked)}
/>
),
},
{
title: '创建时间',
dataIndex: 'created_at',
key: 'created_at',
width: 160,
render: (timestamp: number) => formatDate(timestamp),
},
{
title: '操作',
key: 'action',
width: 120,
render: (_, record) => (
<Space size="small">
<Tooltip title="分段设置">
<Button
type="text"
size="small"
icon={<UnorderedListOutlined />}
onClick={() => onViewDocument?.(record)}
/>
</Tooltip>
{canWrite && (
<Popconfirm
title="确定要删除这个文档吗?"
description="删除后无法恢复"
onConfirm={() => handleDelete(record.id)}
okText="确定"
cancelText="取消"
okButtonProps={{ danger: true }}
>
<Tooltip title="删除">
<Button
type="text"
size="small"
danger
icon={<DeleteOutlined />}
loading={deletingId === record.id}
/>
</Tooltip>
</Popconfirm>
)}
</Space>
),
},
];
return (
<>
{/* 上传页面 */}
{showUploadPage ? (
<DocumentUpload
datasetId={datasetId}
onClose={handleUploadClose}
onSuccess={handleUploadSuccess}
/>
) : (
<div className="document-list-page">
{/* 页面头部 */}
<div className="page-header">
<div className="header-left">
<h1></h1>
</div>
<div className="header-actions">
<Tooltip title="刷新">
<Button
icon={<ReloadOutlined />}
onClick={onRefresh}
loading={loading}
/>
</Tooltip>
{canWrite && (
<Button
type="primary"
icon={<CloudUploadOutlined />}
onClick={handleUploadClick}
disabled={!datasetId}
>
</Button>
)}
</div>
</div>
{/* 搜索栏 */}
<div className="document-search-bar">
<Input
placeholder="搜索文档..."
prefix={<SearchOutlined />}
value={searchValue}
onChange={(e) => setSearchValue(e.target.value)}
allowClear
style={{ width: 280 }}
/>
</div>
{/* 文档表格 */}
<div className="document-table-wrapper">
{loading && documents.length === 0 ? (
<div className="loading-state">
<Spin size="large" />
<div className="loading-text">...</div>
</div>
) : filteredDocuments.length === 0 ? (
<div className="empty-state">
<Empty description={searchValue ? '未找到匹配的文档' : '暂无文档'}>
{!searchValue && (
<Button
type="primary"
icon={<CloudUploadOutlined />}
onClick={handleUploadClick}
>
</Button>
)}
</Empty>
</div>
) : (
<Table
className="document-table"
columns={columns}
dataSource={filteredDocuments}
rowKey="id"
loading={loading}
pagination={false}
size="small"
scroll={{ x: 'max-content' }}
/>
)}
</div>
{/* 底部分页器 */}
{filteredDocuments.length > 0 && (
<div className="document-pagination">
<span className="pagination-total"> {total} </span>
<div className="pagination-controls">
<Button
size="small"
disabled={page <= 1}
onClick={() => onPageChange(page - 1)}
>
</Button>
<span className="pagination-info">
{page} / {Math.ceil(total / pageSize)}
</span>
<Button
size="small"
disabled={page >= Math.ceil(total / pageSize)}
onClick={() => onPageChange(page + 1)}
>
</Button>
</div>
</div>
)}
</div>
)}
</>
);
}