WebhookTpl.tsx 4.2 KB

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