ToolButtonPr.tsx 3.8 KB

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