WebhookTpl.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { Button, Space, Tag, Tree } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { useAppSelector } from "../../hooks";
  4. import { currentUser } from "../../reducers/current-user";
  5. import ArticleListModal from "../article/ArticleListModal";
  6. interface DataNode {
  7. title: string;
  8. key: string;
  9. isLeaf?: boolean;
  10. children?: DataNode[];
  11. }
  12. interface IWidget {
  13. value?: IWebhookEvent[];
  14. onChange?: Function;
  15. }
  16. const WebhookTplWidget = ({ value = [], onChange }: IWidget) => {
  17. const [event, setEvent] = useState(value);
  18. const user = useAppSelector(currentUser);
  19. useEffect(() => {
  20. if (typeof onChange !== "undefined") {
  21. onChange(event);
  22. }
  23. }, [event]);
  24. const treeData: DataNode[] = [
  25. {
  26. title: "事件",
  27. key: "event",
  28. children: [
  29. {
  30. title: "discussion",
  31. key: "discussion",
  32. children: [
  33. {
  34. title: "create",
  35. key: "discussion-create",
  36. isLeaf: true,
  37. },
  38. {
  39. title: "replay",
  40. key: "discussion-replay",
  41. isLeaf: true,
  42. },
  43. {
  44. title: "edit",
  45. key: "discussion-edit",
  46. isLeaf: true,
  47. },
  48. ],
  49. },
  50. {
  51. title: "pr",
  52. key: "pr",
  53. children: [
  54. {
  55. title: "create",
  56. key: "pr-create",
  57. isLeaf: true,
  58. },
  59. {
  60. title: "edit",
  61. key: "pr-edit",
  62. isLeaf: true,
  63. },
  64. ],
  65. },
  66. {
  67. title: "content",
  68. key: "content",
  69. children: [
  70. {
  71. title: "create",
  72. key: "content-create",
  73. isLeaf: true,
  74. },
  75. {
  76. title: "edit",
  77. key: "content-edit",
  78. isLeaf: true,
  79. },
  80. ],
  81. },
  82. ],
  83. },
  84. ];
  85. return (
  86. <Tree
  87. treeData={treeData}
  88. checkable
  89. titleRender={(node) => {
  90. const tpl = event?.find((value) => value.key === node.key);
  91. return (
  92. <Space>
  93. {node.title}
  94. {node.key === "event" ? `自定义模版${event.length}` : ""}
  95. {node.isLeaf ? (
  96. <ArticleListModal
  97. studioName={user?.realName}
  98. trigger={
  99. <Button type="text">
  100. {tpl ? (
  101. <Tag
  102. closable
  103. onClose={() => {
  104. setEvent((origin) => {
  105. const index = origin.findIndex(
  106. (value) => value.key === node.key
  107. );
  108. if (index >= 0) {
  109. origin.splice(index, 1);
  110. console.log("origin", origin);
  111. }
  112. return origin;
  113. });
  114. }}
  115. >
  116. {tpl.tplTitle}
  117. </Tag>
  118. ) : (
  119. "默认模版"
  120. )}
  121. </Button>
  122. }
  123. multiple={false}
  124. onSelect={(id: string, title: string) => {
  125. setEvent((origin) => {
  126. const index = origin.findIndex(
  127. (value) => value.key === node.key
  128. );
  129. if (index >= 0) {
  130. origin[index].tpl = id;
  131. origin[index].tplTitle = title;
  132. return origin;
  133. } else {
  134. return [
  135. ...origin,
  136. { key: node.key, tpl: id, tplTitle: title },
  137. ];
  138. }
  139. });
  140. }}
  141. />
  142. ) : (
  143. ""
  144. )}
  145. </Space>
  146. );
  147. }}
  148. />
  149. );
  150. };
  151. export default WebhookTplWidget;