import React, { useState, useEffect } from "react"; import { Button, Table, Space, Tag, message, Tooltip, Popconfirm, Card, } from "antd"; import { PlusOutlined, EditOutlined, DeleteOutlined, EyeOutlined, } from "@ant-design/icons"; import type { ColumnsType } from "antd/es/table"; import { queryAnnotationTemplatesUsingGet, deleteAnnotationTemplateByIdUsingDelete, } from "../annotation.api"; import type { AnnotationTemplate } from "../annotation.model"; import TemplateForm from "./TemplateForm.tsx"; import TemplateDetail from "./TemplateDetail.tsx"; import {SearchControls} from "@/components/SearchControls.tsx"; import useFetchData from "@/hooks/useFetchData.ts"; import { AnnotationTypeMap, ClassificationMap, DataTypeMap, TemplateTypeMap } from "@/pages/DataAnnotation/annotation.const.tsx"; const TEMPLATE_ADMIN_KEY = "datamate_template_admin"; const TemplateList: React.FC = () => { const [isAdmin, setIsAdmin] = useState(false); useEffect(() => { // 检查 localStorage 中是否存在特殊键 const hasAdminKey = localStorage.getItem(TEMPLATE_ADMIN_KEY) !== null; setIsAdmin(hasAdminKey); }, []); const filterOptions = [ { key: "category", label: "分类", options: [...Object.values(ClassificationMap)], }, { key: "dataType", label: "数据类型", options: [...Object.values(DataTypeMap)], }, { key: "labelingType", label: "标注类型", options: [...Object.values(AnnotationTypeMap)], }, { key: "builtIn", label: "模板类型", options: [...Object.values(TemplateTypeMap)], }, ]; const BUILT_IN_FLAG = { TRUE: "true", FALSE: "false", } as const; const mapTemplateFilters = (filters: Record) => { const getFirstValue = (values?: string[]) => values && values.length > 0 ? values[0] : undefined; const builtInRaw = getFirstValue(filters.builtIn); const builtIn = builtInRaw === BUILT_IN_FLAG.TRUE ? true : builtInRaw === BUILT_IN_FLAG.FALSE ? false : undefined; return { category: getFirstValue(filters.category), dataType: getFirstValue(filters.dataType), labelingType: getFirstValue(filters.labelingType), builtIn, }; }; // Modals const [isFormVisible, setIsFormVisible] = useState(false); const [isDetailVisible, setIsDetailVisible] = useState(false); const [selectedTemplate, setSelectedTemplate] = useState(); const [formMode, setFormMode] = useState<"create" | "edit">("create"); const { loading, tableData, pagination, searchParams, setSearchParams, fetchData, handleFiltersChange, handleKeywordChange, } = useFetchData( queryAnnotationTemplatesUsingGet, undefined, undefined, undefined, undefined, 0, mapTemplateFilters ); const handleCreate = () => { setFormMode("create"); setSelectedTemplate(undefined); setIsFormVisible(true); }; const handleEdit = (template: AnnotationTemplate) => { setFormMode("edit"); setSelectedTemplate(template); setIsFormVisible(true); }; const handleView = (template: AnnotationTemplate) => { setSelectedTemplate(template); setIsDetailVisible(true); }; const handleDelete = async (templateId: string) => { try { const response = await deleteAnnotationTemplateByIdUsingDelete(templateId); if (response.code === 200) { message.success("模板删除成功"); fetchData(); } else { message.error(response.message || "删除模板失败"); } } catch (error) { message.error("删除模板失败"); console.error(error); } }; const handleFormSuccess = () => { setIsFormVisible(false); fetchData(); }; const getCategoryColor = (category: string) => { const colors: Record = { "audio-speech": "purple", "chat": "cyan", "computer-vision": "blue", "conversational-ai": "magenta", "generative-ai": "volcano", "nlp": "green", "ranking-scoring": "gold", "structured-data": "lime", "time-series": "geekblue", "video": "orange", "community": "pink", "custom": "default", }; return colors[category] || "default"; }; const columns: ColumnsType = [ { title: "模板名称", dataIndex: "name", key: "name", width: 200, ellipsis: true, onFilter: (value, record) => record.name.toLowerCase().includes(value.toString().toLowerCase()) || (record.description?.toLowerCase().includes(value.toString().toLowerCase()) ?? false), }, { title: "描述", dataIndex: "description", key: "description", ellipsis: { showTitle: false, }, render: (description: string) => (
{description}
), }, { title: "数据类型", dataIndex: "dataType", key: "dataType", width: 120, render: (dataType: string) => ( {DataTypeMap[dataType as keyof typeof DataTypeMap]?.label || dataType} ), }, { title: "标注类型", dataIndex: "labelingType", key: "labelingType", width: 150, render: (labelingType: string) => ( {AnnotationTypeMap[labelingType as keyof typeof AnnotationTypeMap]?.label || labelingType} ), }, { title: "分类", dataIndex: "category", key: "category", width: 150, render: (category: string) => ( {ClassificationMap[category as keyof typeof ClassificationMap]?.label || category} ), }, { title: "创建时间", dataIndex: "createdAt", key: "createdAt", width: 180, render: (date: string) => new Date(date).toLocaleString(), }, { title: "操作", key: "action", width: 200, fixed: "right", render: (_, record) => ( )} setIsFormVisible(false)} /> setIsDetailVisible(false)} /> ); }; export default TemplateList; export { TemplateList };