ToolButtonDiscussion.tsx 4.2 KB

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