import { useEffect, useState } from "react"; import type { IProject, ITaskData } from "../../api/task"; import "../article/article.css"; import TaskTableCell from "./TaskTableCell"; interface ITaskHeading { id: string; title: string; children: number; } interface IWidget { tasks?: ITaskData[]; onChange?: (treeData: ITaskData[]) => void; } const TaskTable = ({ tasks, onChange }: IWidget) => { const [tasksTitle, setTasksTitle] = useState(); const [dataHeading, setDataHeading] = useState(); const [projects, setProjects] = useState(); useEffect(() => { const projectsId = new Map(); const projectMap = new Map(); tasks?.forEach((task) => { if (task.project_id && task.project) { if (projectsId.has(task.project_id)) { projectsId.set(task.project_id, projectsId.get(task.project_id)! + 1); } else { projectsId.set(task.project_id, 1); projectMap.set(task.project_id, task.project); } } }); setProjects(Array.from(projectMap.values())); const getNodeChildren = (task: ITaskData): number => { const children = tasks?.filter((value) => value.parent_id === task.id); if (children && children.length > 0) { return children.reduce((acc, cur) => { return acc + getNodeChildren(cur); }, children.length); } else { return 0; } }; //列表头 const titles1: ITaskHeading[] = []; let titles2: ITaskHeading[] = []; let titles3: string[] = []; const tRoot = new Map(); tasks ?.filter((value: ITaskData) => !value.parent_id) .forEach((task) => { tRoot.set(task.title, task); }); console.debug("tasks", tasks); tRoot.forEach((task) => { const children = tasks ?.filter((value1) => value1.parent_id === task.id) .map((task1) => { const child: ITaskHeading = { id: task1.id, title: task1.title ?? "", children: 0, }; return child; }); console.debug("task children", task.title, children); if (children) { titles2 = [...titles2, ...children]; } titles1.push({ title: task.title ?? "", id: task.id, children: getNodeChildren(task), }); if (children && children.length > 0) { titles3 = [...titles3, ...children.map((item) => item.title)]; } else { titles3.push(task.title); } }); const heading = [titles1, titles2]; console.log("heading", heading); setTasksTitle(heading); setDataHeading(titles3); }, [tasks]); return (
{tasksTitle?.map((row, level) => { return ( {level === 0 ? ( <> ) : undefined} {row.map((task, index) => { return ( ); })} ); })} {projects ?.sort((a, b) => a.sn - b.sn) .map((row, index) => ( {dataHeading?.map((task, id) => { const taskData = tasks?.find( (value: ITaskData) => value.title === task && value.project_id === row.id ); return ( ); })} ))}
project weight {task.title}
{row.title} {row.weight}
); }; export default TaskTable;