ToolButtonPr.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { useEffect, useState } from "react";
  2. import { Button, Tag, Tree } from "antd";
  3. import { HandOutlinedIcon } from "../../assets/icon";
  4. import ToolButton from "./ToolButton";
  5. import { post } from "../../request";
  6. import { IUser } from "../auth/User";
  7. interface IPrTreeData {
  8. book: number;
  9. paragraph: number;
  10. word_start: number;
  11. word_end: number;
  12. channel_id: string;
  13. content: string;
  14. pr_count: number;
  15. }
  16. interface IPrTreeRequestData {
  17. book: number;
  18. paragraph: number;
  19. word_start: number;
  20. word_end: number;
  21. channel_id: string;
  22. }
  23. interface IPrData {
  24. content: string;
  25. editor?: IUser;
  26. }
  27. interface IPrTreeRequest {
  28. data: IPrTreeRequestData[];
  29. }
  30. interface IPrTreeResponseData {
  31. sentence: IPrTreeData;
  32. pr: IPrData[];
  33. }
  34. interface IPrTreeResponse {
  35. ok: boolean;
  36. message: string;
  37. data: { rows: IPrTreeResponseData[]; count: number };
  38. }
  39. interface DataNode {
  40. title: string;
  41. key: string;
  42. isLeaf?: boolean;
  43. children?: DataNode[];
  44. }
  45. interface IWidget {
  46. type?: string;
  47. articleId?: string;
  48. }
  49. const Widget = ({ type, articleId }: IWidget) => {
  50. const [treeData, setTreeData] = useState<DataNode[]>([]);
  51. const refresh = () => {
  52. const pr = document.querySelectorAll("div.pr_icon[has-pr='true']");
  53. let prRequestData: IPrTreeRequestData[] = [];
  54. for (let index = 0; index < pr.length; index++) {
  55. const element = pr[index];
  56. const id = element.id.split("_");
  57. prRequestData.push({
  58. book: parseInt(id[0]),
  59. paragraph: parseInt(id[1]),
  60. word_start: parseInt(id[2]),
  61. word_end: parseInt(id[3]),
  62. channel_id: id[4],
  63. });
  64. }
  65. console.log("request pr tree", prRequestData);
  66. post<IPrTreeRequest, IPrTreeResponse>("/v2/sent-pr-tree", {
  67. data: prRequestData,
  68. }).then((json) => {
  69. console.log("pr tree", json);
  70. if (json.ok) {
  71. const newTree: DataNode[] = json.data.rows.map((item) => {
  72. const children = item.pr.map((pr) => {
  73. return { title: pr.content, key: pr.content };
  74. });
  75. return {
  76. title: item.sentence.content,
  77. key: `${item.sentence.book}_${item.sentence.paragraph}_${item.sentence.word_start}_${item.sentence.word_end}_${item.sentence.channel_id}`,
  78. children: children,
  79. };
  80. });
  81. setTreeData(newTree);
  82. }
  83. });
  84. };
  85. useEffect(() => {
  86. refresh();
  87. }, []);
  88. return (
  89. <ToolButton
  90. title="修改建议"
  91. icon={<HandOutlinedIcon />}
  92. content={
  93. <>
  94. <Button
  95. onClick={() => {
  96. refresh();
  97. }}
  98. >
  99. refresh
  100. </Button>
  101. <Tree
  102. treeData={treeData}
  103. titleRender={(node) => {
  104. const ele = document.getElementById(node.key);
  105. const count = node.children?.length;
  106. return (
  107. <div
  108. onClick={() => {
  109. ele?.scrollIntoView();
  110. }}
  111. >
  112. {node.title}
  113. <Tag style={{ borderRadius: 5 }}>{count}</Tag>
  114. </div>
  115. );
  116. }}
  117. />
  118. </>
  119. }
  120. />
  121. );
  122. };
  123. export default Widget;