import React from 'react'; import { cx } from '@emotion/css'; import { Dropdown, Menu, Button } from 'antd'; import { PlusOutlined } from '@ant-design/icons'; import { useAPIClient, useCompile } from '@nocobase/client'; import { useFlowContext } from './FlowContext'; import { Instruction, instructions, Node } from './nodes'; import { addButtonClass } from './style'; interface AddButtonProps { upstream; branchIndex?: number; }; export function AddButton({ upstream, branchIndex = null }: AddButtonProps) { const compile = useCompile(); const api = useAPIClient(); const { workflow, onNodeAdded } = useFlowContext() ?? {}; if (!workflow) { return null; } const resource = api.resource('workflows.nodes', workflow.id); async function onCreate({ keyPath }) { const type = keyPath.pop(); const config = {}; const [optionKey] = keyPath; if (optionKey) { const { value } = instructions.get(type).options.find(item => item.key === optionKey); Object.assign(config, value); } const { data: { data: node } } = await resource.create({ values: { type, upstreamId: upstream?.id ?? null, branchIndex, config } }); onNodeAdded(node); } const groups = [ { value: 'control', name: '{{t("Control")}}' }, { value: 'collection', name: '{{t("Collection operations")}}' }, { value: 'extended', name: '{{t("Extended")}}' }, ]; const instructionList = (Array.from(instructions.getValues()) as Instruction[]); return (
onCreate(ev)}> {groups.map(group => { const groupInstructions = instructionList.filter(item => item.group === group.value); return groupInstructions.length ? ( {groupInstructions.map(item => item.options ? ( {item.options.map(option => ( {compile(option.label)} ))} ) : ( {compile(item.title)} ))} ) : null; })} } disabled={workflow.executed} >
); };