NissayaCardTable.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { Button, Space, Table, Tag } from "antd";
  2. import lodash from "lodash";
  3. import { useEffect, useState } from "react";
  4. import { ArrowRightOutlined } from "@ant-design/icons";
  5. const randomString = () =>
  6. lodash.times(20, () => lodash.random(35).toString(36)).join("");
  7. interface ICaseItem {
  8. label: string;
  9. link: string;
  10. }
  11. interface IRelationNode {
  12. case?: ICaseItem[];
  13. spell?: string;
  14. }
  15. interface DataType {
  16. key: string;
  17. relation: string;
  18. localRelation?: string;
  19. to?: IRelationNode;
  20. from?: IRelationNode;
  21. category?: { name: string; note: string; meaning: string };
  22. translation?: string;
  23. isChildren?: boolean;
  24. children?: DataType[];
  25. }
  26. export interface INissayaRelation {
  27. from?: IRelationNode;
  28. to?: IRelationNode;
  29. category?: { name: string; note: string; meaning: string };
  30. local_ending: string;
  31. relation: string;
  32. local_relation?: string;
  33. relation_link: string;
  34. }
  35. interface IWidget {
  36. data?: INissayaRelation[];
  37. }
  38. const NissayaCardTableWidget = ({ data }: IWidget) => {
  39. const [tableData, setTableData] = useState<DataType[]>();
  40. useEffect(() => {
  41. if (typeof data === "undefined") {
  42. setTableData(undefined);
  43. return;
  44. }
  45. console.log("data", data);
  46. let category: string[] = [];
  47. let newData: DataType[] = [];
  48. data.forEach((item, index) => {
  49. if (item.category && item.category.name) {
  50. const key = `${item.from?.spell}-${item.from?.case
  51. ?.map((item) => item.label)
  52. .join()}-${item.relation}-${item.category}`;
  53. if (!category.includes(key)) {
  54. category.push(key);
  55. console.log("category", category);
  56. //处理children
  57. const children = data
  58. .filter(
  59. (value) =>
  60. `${value.from?.spell}-${value.from?.case
  61. ?.map((item) => item.label)
  62. .join()}-${value.relation}-${value.category}` === key
  63. )
  64. .map((item, index) => {
  65. return {
  66. key: randomString(),
  67. relation: item.relation,
  68. localRelation: item.local_relation,
  69. from: item.from,
  70. to: item.to,
  71. category: item.category,
  72. translation: item.local_ending,
  73. isChildren: true,
  74. };
  75. });
  76. console.log("children", children);
  77. newData.push({
  78. key: randomString(),
  79. relation: item.relation,
  80. localRelation: item.local_relation,
  81. from: item.from,
  82. to: item.to,
  83. category: item.category,
  84. translation: item.local_ending,
  85. children: children.length > 1 ? [...children] : undefined,
  86. });
  87. }
  88. } else {
  89. newData.push({
  90. key: randomString(),
  91. relation: item.relation,
  92. localRelation: item.local_relation,
  93. from: item.to,
  94. to: item.to,
  95. category: item.category,
  96. translation: item.local_ending,
  97. });
  98. }
  99. });
  100. setTableData(newData);
  101. }, [data]);
  102. return (
  103. <Table
  104. columns={[
  105. {
  106. title: "本词特征",
  107. dataIndex: "from",
  108. key: "from",
  109. render: (value, record, index) => {
  110. return (
  111. <Space>
  112. {record.from?.case?.map((item, id) => {
  113. return (
  114. <Button
  115. key={id}
  116. type="link"
  117. size="small"
  118. onClick={() => window.open(item.link, "_blank")}
  119. >
  120. <Tag>{item.label}</Tag>
  121. </Button>
  122. );
  123. })}
  124. {record.from?.spell}
  125. </Space>
  126. );
  127. },
  128. },
  129. {
  130. title: "关系",
  131. dataIndex: "relation",
  132. key: "relation",
  133. width: "16%",
  134. render: (value, record, index) => {
  135. return (
  136. <Space direction="vertical">
  137. {record.relation}
  138. {record.localRelation}
  139. </Space>
  140. );
  141. },
  142. },
  143. {
  144. title: "目标词特征",
  145. dataIndex: "to",
  146. key: "to",
  147. render: (value, record, index) => {
  148. if (record.isChildren) {
  149. return (
  150. <Space>
  151. <ArrowRightOutlined />
  152. {record.to?.case?.map((item, id) => {
  153. return (
  154. <Button
  155. key={id}
  156. type="link"
  157. size="small"
  158. onClick={() => window.open(item.link, "_blank")}
  159. >
  160. <Tag key={id}>{item.label}</Tag>
  161. </Button>
  162. );
  163. })}
  164. {record.to?.spell}
  165. </Space>
  166. );
  167. } else {
  168. return (
  169. <Space>
  170. <ArrowRightOutlined />
  171. {record.category?.meaning}
  172. </Space>
  173. );
  174. }
  175. },
  176. },
  177. {
  178. title: "含义",
  179. dataIndex: "address",
  180. width: "30%",
  181. key: "address",
  182. render: (value, record, index) => {
  183. if (record.isChildren) {
  184. return undefined;
  185. } else {
  186. return <>{record.category?.note}</>;
  187. }
  188. },
  189. },
  190. {
  191. title: "翻译建议",
  192. dataIndex: "translation",
  193. width: "20%",
  194. key: "translation",
  195. },
  196. ]}
  197. dataSource={tableData}
  198. />
  199. );
  200. };
  201. export default NissayaCardTableWidget;