| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import { Button, Space, Tag, Tree } from "antd";
- import { useEffect, useState } from "react";
- import { useAppSelector } from "../../hooks";
- import { currentUser } from "../../reducers/current-user";
- import ArticleListModal from "../article/ArticleListModal";
- interface DataNode {
- title: string;
- key: string;
- isLeaf?: boolean;
- children?: DataNode[];
- }
- interface IWidget {
- value?: IWebhookEvent[];
- onChange?: Function;
- }
- const WebhookTplWidget = ({ value = [], onChange }: IWidget) => {
- const [event, setEvent] = useState(value);
- const user = useAppSelector(currentUser);
- useEffect(() => {
- if (typeof onChange !== "undefined") {
- onChange(event);
- }
- }, [event]);
- const treeData: DataNode[] = [
- {
- title: "事件",
- key: "event",
- children: [
- {
- title: "discussion",
- key: "discussion",
- children: [
- {
- title: "create",
- key: "discussion-create",
- isLeaf: true,
- },
- {
- title: "replay",
- key: "discussion-replay",
- isLeaf: true,
- },
- {
- title: "edit",
- key: "discussion-edit",
- isLeaf: true,
- },
- ],
- },
- {
- title: "pr",
- key: "pr",
- children: [
- {
- title: "create",
- key: "pr-create",
- isLeaf: true,
- },
- {
- title: "edit",
- key: "pr-edit",
- isLeaf: true,
- },
- ],
- },
- {
- title: "content",
- key: "content",
- children: [
- {
- title: "create",
- key: "content-create",
- isLeaf: true,
- },
- {
- title: "edit",
- key: "content-edit",
- isLeaf: true,
- },
- ],
- },
- ],
- },
- ];
- return (
- <Tree
- treeData={treeData}
- checkable
- titleRender={(node) => {
- const tpl = event?.find((value) => value.key === node.key);
- return (
- <Space>
- {node.title}
- {node.key === "event" ? `自定义模版${event.length}` : ""}
- {node.isLeaf ? (
- <ArticleListModal
- studioName={user?.realName}
- trigger={
- <Button type="text">
- {tpl ? (
- <Tag
- closable
- onClose={() => {
- setEvent((origin) => {
- const index = origin.findIndex(
- (value) => value.key === node.key
- );
- if (index >= 0) {
- origin.splice(index, 1);
- console.log("origin", origin);
- }
- return origin;
- });
- }}
- >
- {tpl.tplTitle}
- </Tag>
- ) : (
- "默认模版"
- )}
- </Button>
- }
- multiple={false}
- onSelect={(id: string, title: string) => {
- setEvent((origin) => {
- const index = origin.findIndex(
- (value) => value.key === node.key
- );
- if (index >= 0) {
- origin[index].tpl = id;
- origin[index].tplTitle = title;
- return origin;
- } else {
- return [
- ...origin,
- { key: node.key, tpl: id, tplTitle: title },
- ];
- }
- });
- }}
- />
- ) : (
- ""
- )}
- </Space>
- );
- }}
- />
- );
- };
- export default WebhookTplWidget;
|