From dc91d44ce658839951c0947bb3cc565db21ea8d8 Mon Sep 17 00:00:00 2001 From: Junyi Date: Thu, 6 Jul 2023 11:27:34 +0700 Subject: [PATCH] refactor(plugin-workflow): refactor nodes variable option api (#2192) --- .../workflow/src/client/nodes/aggregate.tsx | 10 ++++----- .../workflow/src/client/nodes/calculation.tsx | 17 +++++++++----- .../workflow/src/client/nodes/create.tsx | 22 ++++++++++++++----- .../workflow/src/client/nodes/index.tsx | 4 ++-- .../src/client/nodes/manual/index.tsx | 22 ++++++++++++------- .../workflow/src/client/nodes/query.tsx | 22 ++++++++++++++----- .../plugins/workflow/src/client/variable.tsx | 11 +++------- 7 files changed, 69 insertions(+), 39 deletions(-) diff --git a/packages/plugins/workflow/src/client/nodes/aggregate.tsx b/packages/plugins/workflow/src/client/nodes/aggregate.tsx index 8dcd4f086..31c094d6e 100644 --- a/packages/plugins/workflow/src/client/nodes/aggregate.tsx +++ b/packages/plugins/workflow/src/client/nodes/aggregate.tsx @@ -30,7 +30,6 @@ function AssociatedConfig({ value, onChange, ...props }): JSX.Element { const { setValuesIn } = useForm(); const compile = useCompile(); const { getCollection } = useCollectionManager(); - const current = useNodeContext(); const options = [nodesOptions, triggerOptions].map((item) => { const children = item.useOptions({ types: [matchToManyField] })?.filter(Boolean); return { @@ -300,16 +299,17 @@ export default { ValueBlock, AssociatedConfig, }, - useVariables(current, { types }) { + useVariables({ id, title }, { types }) { if ( types && !types.some((type) => type in BaseTypeSets || Object.values(BaseTypeSets).some((set) => set.has(type))) ) { return null; } - return [ - // { key: '', value: '', label: lang('Calculation result') } - ]; + return { + value: `${id}`, + label: title, + }; }, useInitializers(node): SchemaInitializerItemOptions | null { if (!node.config.collection) { diff --git a/packages/plugins/workflow/src/client/nodes/calculation.tsx b/packages/plugins/workflow/src/client/nodes/calculation.tsx index c1937c411..c5c68a92f 100644 --- a/packages/plugins/workflow/src/client/nodes/calculation.tsx +++ b/packages/plugins/workflow/src/client/nodes/calculation.tsx @@ -18,15 +18,19 @@ function useDynamicExpressionCollectionFieldMatcher(field): boolean { return false; } + if (this.getCollection(field.collectionName)?.template === 'expression') { + return true; + } + const fields = this.getCollectionFields(field.target); return fields.some((f) => f.interface === 'expression'); } const DynamicConfig = ({ value, onChange }) => { const { t } = useTranslation(); - const { getCollectionFields } = useCollectionManager(); + const { getCollectionFields, getCollection } = useCollectionManager(); const scope = useWorkflowVariableOptions({ - types: [useDynamicExpressionCollectionFieldMatcher.bind({ getCollectionFields })], + types: [useDynamicExpressionCollectionFieldMatcher.bind({ getCollectionFields, getCollection })], }); return ( @@ -189,7 +193,7 @@ export default { RadioWithTooltip, DynamicConfig, }, - useVariables(current, options) { + useVariables({ id, title }, options) { const { types } = options ?? {}; if ( types && @@ -197,9 +201,10 @@ export default { ) { return null; } - return [ - // { key: '', value: '', label: lang('Calculation result') } - ]; + return { + value: id, + label: title, + }; }, useInitializers(node): SchemaInitializerItemOptions { return { diff --git a/packages/plugins/workflow/src/client/nodes/create.tsx b/packages/plugins/workflow/src/client/nodes/create.tsx index a0036a9b9..9a45f3abc 100644 --- a/packages/plugins/workflow/src/client/nodes/create.tsx +++ b/packages/plugins/workflow/src/client/nodes/create.tsx @@ -40,22 +40,34 @@ export default { CollectionFieldset, FieldsSelect, }, - useVariables({ config }, options) { + useVariables({ id, title, config }, options) { const compile = useCompile(); const { getCollectionFields } = useCollectionManager(); // const depth = config?.params?.appends?.length // ? config?.params?.appends.reduce((max, item) => Math.max(max, item.split('.').length), 1) // : 0; - const result = getCollectionFieldOptions({ - collection: config.collection, + const name = `${id}`; + const [result] = getCollectionFieldOptions({ + // collection: config.collection, ...options, + fields: [ + { + collectionName: config.collection, + name, + type: 'hasOne', + target: config.collection, + uiSchema: { + title, + }, + }, + ], // depth: options?.depth ?? depth, - appends: config.params?.appends, + appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])], compile, getCollectionFields, }); - return result?.length ? result : null; + return result; }, useInitializers(node): SchemaInitializerItemOptions | null { if (!node.config.collection) { diff --git a/packages/plugins/workflow/src/client/nodes/index.tsx b/packages/plugins/workflow/src/client/nodes/index.tsx index b374d5cd9..4acf51411 100644 --- a/packages/plugins/workflow/src/client/nodes/index.tsx +++ b/packages/plugins/workflow/src/client/nodes/index.tsx @@ -38,7 +38,7 @@ import aggregate from './aggregate'; import { JobStatusOptionsMap } from '../constants'; import { NAMESPACE, lang } from '../locale'; import request from './request'; -import { VariableOptions } from '../variable'; +import { VariableOption, VariableOptions } from '../variable'; import { NodeDescription } from '../components/NodeDescription'; export interface Instruction { @@ -53,7 +53,7 @@ export interface Instruction { components?: { [key: string]: any }; component?(props): JSX.Element; endding?: boolean; - useVariables?(node, options?): VariableOptions; + useVariables?(node, options?): VariableOption; useScopeVariables?(node, options?): VariableOptions; useInitializers?(node): SchemaInitializerItemOptions | null; initializers?: { [key: string]: any }; diff --git a/packages/plugins/workflow/src/client/nodes/manual/index.tsx b/packages/plugins/workflow/src/client/nodes/manual/index.tsx index e087ec2cf..3038d727d 100644 --- a/packages/plugins/workflow/src/client/nodes/manual/index.tsx +++ b/packages/plugins/workflow/src/client/nodes/manual/index.tsx @@ -85,7 +85,7 @@ export default { ModeConfig, AssigneesSelect, }, - useVariables({ config }, { types }) { + useVariables({ id, title, config }, { types }) { const compile = useCompile(); const { getCollectionFields } = useCollectionManager(); const formKeys = Object.keys(config.forms ?? {}); @@ -97,27 +97,33 @@ export default { .map((formKey) => { const form = config.forms[formKey]; - // eslint-disable-next-line react-hooks/rules-of-hooks - const options = getCollectionFieldOptions({ + const fieldsOptions = getCollectionFieldOptions({ fields: form.collection?.fields, collection: form.collection, types, compile, getCollectionFields, }); - return options.length + const label = compile(form.title) || formKey; + return fieldsOptions.length ? { key: formKey, value: formKey, - label: form.title || formKey, - title: form.title || formKey, - children: options, + label, + title: label, + children: fieldsOptions, } : null; }) .filter(Boolean); - return options.length ? options : null; + return options.length + ? { + value: `${id}`, + label: title, + children: options, + } + : null; }, useInitializers(node): SchemaInitializerItemOptions | null { const { getCollection } = useCollectionManager(); diff --git a/packages/plugins/workflow/src/client/nodes/query.tsx b/packages/plugins/workflow/src/client/nodes/query.tsx index e52a0a7e5..f84ff42e6 100644 --- a/packages/plugins/workflow/src/client/nodes/query.tsx +++ b/packages/plugins/workflow/src/client/nodes/query.tsx @@ -43,22 +43,34 @@ export default { FilterDynamicComponent, FieldsSelect, }, - useVariables({ config }, options) { + useVariables({ id, title, config }, options) { const compile = useCompile(); const { getCollectionFields } = useCollectionManager(); // const depth = config?.params?.appends?.length // ? config?.params?.appends.reduce((max, item) => Math.max(max, item.split('.').length), 1) // : 0; - const result = getCollectionFieldOptions({ - collection: config.collection, + const name = `${id}`; + const [result] = getCollectionFieldOptions({ + // collection: config.collection, ...options, + fields: [ + { + collectionName: config.collection, + name, + type: 'hasOne', + target: config.collection, + uiSchema: { + title, + }, + }, + ], // depth: options?.depth ?? depth, - appends: config.params?.appends, + appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])], compile, getCollectionFields, }); - return result?.length ? result : null; + return result; }, useInitializers(node): SchemaInitializerItemOptions | null { if (!node.config.collection || node.config.multiple) { diff --git a/packages/plugins/workflow/src/client/variable.tsx b/packages/plugins/workflow/src/client/variable.tsx index fd7d0d4de..984d15aed 100644 --- a/packages/plugins/workflow/src/client/variable.tsx +++ b/packages/plugins/workflow/src/client/variable.tsx @@ -22,14 +22,9 @@ export const nodesOptions = { const result: VariableOption[] = []; upstreams.forEach((node) => { const instruction = instructions.get(node.type); - const subOptions = instruction.useVariables?.(node, options); - if (subOptions) { - result.push({ - key: node.id.toString(), - value: node.id.toString(), - label: node.title ?? `#${node.id}`, - children: subOptions, - }); + const subOption = instruction.useVariables?.(node, options); + if (subOption) { + result.push(subOption); } }); return result;