refactor code
This commit is contained in:
@@ -5,7 +5,7 @@ import { Button, Card, Popover } from "antd";
|
||||
import SharedNode, { type SharedNodeProps } from "./_SharedNode";
|
||||
import AddNode from "./AddNode";
|
||||
import ConditionNodeConfigForm, { ConditionItem, ConditionNodeConfigFormFieldValues, ConditionNodeConfigFormInstance } from "./ConditionNodeConfigForm";
|
||||
import { Expr, WorkflowNodeIoValueType, Value } from "@/domain/workflow";
|
||||
import { Expr, WorkflowNodeIoValueType, ExprType } from "@/domain/workflow";
|
||||
import { produce } from "immer";
|
||||
import { useWorkflowStore } from "@/stores/workflow";
|
||||
import { useZustandShallowSelector } from "@/hooks";
|
||||
@@ -32,7 +32,7 @@ const ConditionNode = ({ node, disabled, branchId, branchIndex }: ConditionNodeP
|
||||
const selectors = condition.leftSelector.split("#");
|
||||
const t = selectors[2] as WorkflowNodeIoValueType;
|
||||
const left: Expr = {
|
||||
type: "var",
|
||||
type: ExprType.Var,
|
||||
selector: {
|
||||
id: selectors[0],
|
||||
name: selectors[1],
|
||||
@@ -40,27 +40,10 @@ const ConditionNode = ({ node, disabled, branchId, branchIndex }: ConditionNodeP
|
||||
},
|
||||
};
|
||||
|
||||
let value: Value = condition.rightValue;
|
||||
switch (t) {
|
||||
case "boolean":
|
||||
if (value === "true") {
|
||||
value = true;
|
||||
} else if (value === "false") {
|
||||
value = false;
|
||||
}
|
||||
break;
|
||||
case "number":
|
||||
value = parseInt(value as string);
|
||||
break;
|
||||
case "string":
|
||||
value = value as string;
|
||||
break;
|
||||
}
|
||||
|
||||
const right: Expr = { type: "const", value: value, valueType: t };
|
||||
const right: Expr = { type: ExprType.Const, value: condition.rightValue, valueType: t };
|
||||
|
||||
return {
|
||||
type: "compare",
|
||||
type: ExprType.Compare,
|
||||
op: condition.operator,
|
||||
left,
|
||||
right,
|
||||
@@ -77,7 +60,7 @@ const ConditionNode = ({ node, disabled, branchId, branchIndex }: ConditionNodeP
|
||||
|
||||
for (let i = 1; i < values.conditions.length; i++) {
|
||||
expr = {
|
||||
type: "logical",
|
||||
type: ExprType.Logical,
|
||||
op: values.logicalOperator,
|
||||
left: expr,
|
||||
right: createComparisonExpr(values.conditions[i]),
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
WorkflowNode,
|
||||
workflowNodeIOOptions,
|
||||
WorkflowNodeIoValueType,
|
||||
ExprType,
|
||||
} from "@/domain/workflow";
|
||||
import { FormInstance } from "antd";
|
||||
import { useZustandShallowSelector } from "@/hooks";
|
||||
@@ -58,7 +59,7 @@ const initFormModel = (): ConditionNodeConfigFormFieldValues => {
|
||||
rightValue: "",
|
||||
},
|
||||
],
|
||||
logicalOperator: "and",
|
||||
logicalOperator: LogicalOperator.And,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -67,10 +68,10 @@ const expressionToForm = (expr?: Expr): ConditionNodeConfigFormFieldValues => {
|
||||
if (!expr) return initFormModel();
|
||||
|
||||
const conditions: ConditionItem[] = [];
|
||||
let logicalOp: LogicalOperator = "and";
|
||||
let logicalOp: LogicalOperator = LogicalOperator.And;
|
||||
|
||||
const extractComparisons = (expr: Expr): void => {
|
||||
if (expr.type === "compare") {
|
||||
if (expr.type === ExprType.Compare) {
|
||||
// 确保左侧是变量,右侧是常量
|
||||
if (isVarExpr(expr.left) && isConstExpr(expr.right)) {
|
||||
conditions.push({
|
||||
@@ -79,7 +80,7 @@ const expressionToForm = (expr?: Expr): ConditionNodeConfigFormFieldValues => {
|
||||
rightValue: String(expr.right.value),
|
||||
});
|
||||
}
|
||||
} else if (expr.type === "logical") {
|
||||
} else if (expr.type === ExprType.Logical) {
|
||||
logicalOp = expr.op;
|
||||
extractComparisons(expr.left);
|
||||
extractComparisons(expr.right);
|
||||
@@ -304,25 +305,18 @@ const formToExpression = (values: ConditionNodeConfigFormFieldValues): Expr => {
|
||||
const type = typeStr as WorkflowNodeIoValueType;
|
||||
|
||||
const left: Expr = {
|
||||
type: "var",
|
||||
type: ExprType.Var,
|
||||
selector: { id, name, type },
|
||||
};
|
||||
|
||||
let rightValue: any = condition.rightValue;
|
||||
if (type === "number") {
|
||||
rightValue = Number(condition.rightValue);
|
||||
} else if (type === "boolean") {
|
||||
rightValue = condition.rightValue === "true";
|
||||
}
|
||||
|
||||
const right: Expr = {
|
||||
type: "const",
|
||||
value: rightValue,
|
||||
type: ExprType.Const,
|
||||
value: condition.rightValue,
|
||||
valueType: type,
|
||||
};
|
||||
|
||||
return {
|
||||
type: "compare",
|
||||
type: ExprType.Compare,
|
||||
op: condition.operator,
|
||||
left,
|
||||
right,
|
||||
@@ -339,7 +333,7 @@ const formToExpression = (values: ConditionNodeConfigFormFieldValues): Expr => {
|
||||
|
||||
for (let i = 1; i < values.conditions.length; i++) {
|
||||
expr = {
|
||||
type: "logical",
|
||||
type: ExprType.Logical,
|
||||
op: values.logicalOperator,
|
||||
left: expr,
|
||||
right: createComparisonExpr(values.conditions[i]),
|
||||
|
||||
@@ -39,7 +39,7 @@ const InspectNode = ({ node, disabled }: InspectNodeProps) => {
|
||||
const config = (node.config as WorkflowNodeConfigForInspect) ?? {};
|
||||
return (
|
||||
<Flex className="size-full overflow-hidden" align="center" gap={8}>
|
||||
<Typography.Text className="truncate">{config.domain ?? ""}</Typography.Text>
|
||||
<Typography.Text className="truncate">{config.host ?? ""}</Typography.Text>
|
||||
</Flex>
|
||||
);
|
||||
}, [node]);
|
||||
|
||||
@@ -7,7 +7,7 @@ import { z } from "zod";
|
||||
import { type WorkflowNodeConfigForInspect } from "@/domain/workflow";
|
||||
import { useAntdForm } from "@/hooks";
|
||||
|
||||
import { validDomainName, validPortNumber } from "@/utils/validators";
|
||||
import { validDomainName, validIPv4Address, validPortNumber } from "@/utils/validators";
|
||||
|
||||
type InspectNodeConfigFormFieldValues = Partial<WorkflowNodeConfigForInspect>;
|
||||
|
||||
@@ -29,6 +29,8 @@ const initFormModel = (): InspectNodeConfigFormFieldValues => {
|
||||
return {
|
||||
domain: "",
|
||||
port: "443",
|
||||
path: "",
|
||||
host: "",
|
||||
};
|
||||
};
|
||||
|
||||
@@ -37,12 +39,14 @@ const InspectNodeConfigForm = forwardRef<InspectNodeConfigFormInstance, InspectN
|
||||
const { t } = useTranslation();
|
||||
|
||||
const formSchema = z.object({
|
||||
domain: z.string().refine((val) => validDomainName(val), {
|
||||
message: t("workflow_node.inspect.form.domain.placeholder"),
|
||||
host: z.string().refine((val) => validIPv4Address(val) || validDomainName(val), {
|
||||
message: t("workflow_node.inspect.form.host.placeholder"),
|
||||
}),
|
||||
domain: z.string().optional(),
|
||||
port: z.string().refine((val) => validPortNumber(val), {
|
||||
message: t("workflow_node.inspect.form.port.placeholder"),
|
||||
}),
|
||||
path: z.string().optional(),
|
||||
});
|
||||
const formRule = createSchemaFieldRule(formSchema);
|
||||
const { form: formInst, formProps } = useAntdForm({
|
||||
@@ -70,13 +74,21 @@ const InspectNodeConfigForm = forwardRef<InspectNodeConfigFormInstance, InspectN
|
||||
|
||||
return (
|
||||
<Form className={className} style={style} {...formProps} disabled={disabled} layout="vertical" scrollToFirstError onValuesChange={handleFormChange}>
|
||||
<Form.Item name="domain" label={t("workflow_node.inspect.form.domain.label")} rules={[formRule]}>
|
||||
<Input variant="filled" placeholder={t("workflow_node.inspect.form.domain.placeholder")} />
|
||||
<Form.Item name="host" label={t("workflow_node.inspect.form.host.label")} rules={[formRule]}>
|
||||
<Input variant="filled" placeholder={t("workflow_node.inspect.form.host.placeholder")} />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item name="port" label={t("workflow_node.inspect.form.port.label")} rules={[formRule]}>
|
||||
<Input variant="filled" placeholder={t("workflow_node.inspect.form.port.placeholder")} />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item name="domain" label={t("workflow_node.inspect.form.domain.label")} rules={[formRule]}>
|
||||
<Input variant="filled" placeholder={t("workflow_node.inspect.form.domain.placeholder")} />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item name="path" label={t("workflow_node.inspect.form.path.label")} rules={[formRule]}>
|
||||
<Input variant="filled" placeholder={t("workflow_node.inspect.form.path.placeholder")} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user