tachybase_todo/packages/plugins/workflow/src/client/variable.tsx

114 lines
3.5 KiB
TypeScript
Raw Normal View History

Feat(plugin-workflow) manual instruction (#1339) * feat(plugin-workflow): add prompt node * feat(plugin-workflow): useValueGetter for all instructions and triggers * feat(plugin-workflow): add workflow block initializer * refactor(plugin-workflow): change prompt node type to manual * feat(plugin-workflow): add ModeConfig component for mode * feat(plugin-workflow): add todo drawer * feat(plugin-workflow): add block value provider * feat(plugin-workflow): improve todo block and drawer * fix(plugin-workflow): fix instruction name in test cases * fix(plugin-workflow): fix test cases * refactor(plugin-workflow): change param type of collection field initializer * feat(plugin-workflow): add filter types for getters * fix(plugin-workflow): fix assignees variable * fix(plugin-workflow): filter todo by exist workflow * fix(plugin-workflow): fix duplicated save action in manual config * fix(plugin-workflow): fix transaction * feat(plugin-workflow): destroy workflow will be cascaded * fix(plugin-workflow): fix merge * fix(plugin-workflow): fix locale * fix(plugin-workflow): allow open ui view when executed * fix(plugin-workflow): change todo table filter * feat(plugin-workflow): use formula for calculation * fix(plugin-workflow): fix variable template regexp * fix(plugin-workflow): fix sub-options logic with types * refactor(plugin-workflow): drop useless component * fix(plugin-workflow): fix manual node action button * feat(plugin-workflow): add new variable input component * refactor(plugin-workflow): change all variable to new component * fix(plugin-workflow): fix type * fix(plugin-workflow): fix functions init * fix(plugin-workflow): change jsonb to json for stable order * fix(plugin-workflow): fix duplicated field name when initialize * fix(plugin-workflow): fix manual result in manual block * test(plugin-workflow): log field initializer props * fix(plugin-workflow): fix nullable arguments * test(plugin-workflow): test initializer fields schema * fix: observer * fix(plugin-workflow): adjust hints * fix(plugin-workflow): fix locale and cursor in variable input * refactor(plugin-workflow): change status keys * fix(plugin-workflow): fix parallel instruction * fix(plugin-workflow): fix calculation migration * fix(plugin-workflow): move tasks native filter to server * fix(plugin-workflow): fix manual options for variable * fix(plugin-workflow): fix conflict * fix(plugin-workflow): fix some bugs * fix(plugin-workflow): fix todo list filter and locale * fix(plugin-workflow): fix update action of workflow * refactor(plugin-workflow): add legacy condition calculation as basic engine * fix(plugin-workflow): fix type * fix(plugin-workflow): fix condition basic calculation * fix(plugin-workflow): fix type * fix(plugin-workflow): fix migration * fix(plugin-workflow): fix evaluators and scope * fix(plugin-workflow): remove disabled type select in schema config * fix(plugin-workflow): fix manual form schema designer --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-02-20 11:52:06 +08:00
import { useCollectionManager, useCompile } from "@nocobase/client";
import { instructions, useAvailableUpstreams, useNodeContext } from "./nodes";
import { useFlowContext } from "./FlowContext";
import { triggers } from "./triggers";
import { NAMESPACE } from "./locale";
export type VariableOption = { key: string, value: string; label: string; children?: VariableOption[] };
const VariableTypes = [
{
title: `{{t("Node result", { ns: "${NAMESPACE}" })}}`,
value: '$jobsMapByNodeId',
options(types) {
const current = useNodeContext();
const upstreams = useAvailableUpstreams(current);
const options: VariableOption[] = [];
upstreams.forEach((node) => {
const instruction = instructions.get(node.type);
const subOptions = instruction.getOptions?.(node.config, types);
if (subOptions) {
options.push({
key: node.id.toString(),
value: node.id.toString(),
label: node.title ?? `#${node.id}`,
children: subOptions,
});
}
});
return options;
},
},
{
title: `{{t("Trigger variables", { ns: "${NAMESPACE}" })}}`,
value: '$context',
options(types) {
const { workflow } = useFlowContext();
const trigger = triggers.get(workflow.type);
return trigger?.getOptions?.(workflow.config, types) ?? null;
},
},
{
title: `{{t("System variables", { ns: "${NAMESPACE}" })}}`,
value: '$system',
options: [
{
key: 'now',
value: 'now',
label: `{{t("Current time", { ns: "${NAMESPACE}" })}}`,
}
]
}
];
export const TypeSets = {
boolean: new Set(['boolean']),
number: new Set(['integer', 'bigInt', 'float', 'double', 'real', 'decimal']),
string: new Set(['string', 'text', 'password']),
date: new Set(['date', 'time'])
}
function matchFieldType(field, type): Boolean {
if (typeof type === 'string') {
return Boolean(TypeSets[type]?.has(field.type));
}
if (typeof type === 'object' && type.type === 'reference') {
return (field.collectionName === type.options?.collection && field.name === 'id')
|| (field.type === 'belongsTo' && field.target === type.options?.collection);
}
return false;
}
export function filterTypedFields(fields, types) {
return types
? fields.filter(field => types.some(type => matchFieldType(field, type)))
: fields;
}
export function useWorkflowVariableOptions() {
const compile = useCompile();
const options = VariableTypes.map((item: any) => {
const options = typeof item.options === 'function' ? item.options().filter(Boolean) : item.options;
return {
label: compile(item.title),
value: item.value,
key: item.value,
children: compile(options),
disabled: options && !options.length
};
});
return options;
}
export function useCollectionFieldOptions(props) {
const { fields, collection, types } = props;
const compile = useCompile();
const { getCollectionFields } = useCollectionManager();
return filterTypedFields((fields ?? getCollectionFields(collection)), types)
.filter(field => field.interface && (!field.target || field.type === 'belongsTo'))
.map(field => field.type === 'belongsTo'
? {
label: `${compile(field.uiSchema?.title || field.name)} ID`,
key: field.foreignKey,
value: field.foreignKey,
}
: {
label: compile(field.uiSchema?.title || field.name),
key: field.name,
value: field.name,
});
}