Skip to content

Commit

Permalink
feat: Refactor AddFlowVariableModal to use buildVariableString from u…
Browse files Browse the repository at this point in the history
…tils/flow
  • Loading branch information
谨欣 committed Sep 4, 2024
1 parent 9a4e6b6 commit 25944c7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 55 deletions.
55 changes: 1 addition & 54 deletions web/components/flow/canvas-modal/add-flow-variable-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { apiInterceptors, getKeys, getVariablesByKey } from '@/client/api';
import { IGetKeysResponseData, IVariableItem } from '@/types/flow';
import { buildVariableString } from '@/utils/flow';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { Button, Cascader, Form, Input, Modal, Select, Space } from 'antd';
import { uniqBy } from 'lodash';
Expand All @@ -16,60 +17,6 @@ interface Option {
isLeaf?: boolean;
}

function escapeVariable(value: string, enableEscape: boolean): string {
if (!enableEscape) {
return value;
}
return value.replace(/@/g, '\\@').replace(/#/g, '\\#').replace(/%/g, '\\%').replace(/:/g, '\\:');
}

function buildVariableString(variableDict: IVariableItem): string {
const scopeSig = '@';
const sysCodeSig = '#';
const userSig = '%';
const kvSig = ':';
const enableEscape = true;

const specialChars = new Set([scopeSig, sysCodeSig, userSig, kvSig]);

const newVariableDict: Partial<IVariableItem> = {
key: variableDict.key || '',
name: variableDict.name || '',
scope: variableDict.scope || '',
scope_key: variableDict.scope_key || '',
sys_code: variableDict.sys_code || '',
user_name: variableDict.user_name || '',
};

// Check for special characters in values
for (const [key, value] of Object.entries(newVariableDict)) {
if (value && [...specialChars].some(char => (value as string).includes(char))) {
if (enableEscape) {
newVariableDict[key] = escapeVariable(value as string, enableEscape);
} else {
throw new Error(
`${key} contains special characters, error value: ${value}, special characters: ${[...specialChars].join(', ')}`,
);
}
}
}

const { key, name, scope, scope_key, sys_code, user_name } = newVariableDict;

let variableStr = `${key}`;

if (name) variableStr += `${kvSig}${name}`;
if (scope || scope_key) {
variableStr += `${scopeSig}${scope}`;
if (scope_key) {
variableStr += `${kvSig}${scope_key}`;
}
}
if (sys_code) variableStr += `${sysCodeSig}${sys_code}`;
if (user_name) variableStr += `${userSig}${user_name}`;
return `\${${variableStr}}`;
}

export const AddFlowVariableModal: React.FC = () => {
const { t } = useTranslation();
const [isModalOpen, setIsModalOpen] = useState(false);
Expand Down
56 changes: 55 additions & 1 deletion web/utils/flow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFlowData, IFlowDataNode, IFlowNode } from '@/types/flow';
import { IFlowData, IFlowDataNode, IFlowNode, IVariableItem } from '@/types/flow';
import { Node } from 'reactflow';

export const getUniqueNodeId = (nodeData: IFlowNode, nodes: Node[]) => {
Expand Down Expand Up @@ -140,3 +140,57 @@ export const convertKeysToCamelCase = (obj: Record<string, any>): Record<string,

return convert(obj);
};

function escapeVariable(value: string, enableEscape: boolean): string {
if (!enableEscape) {
return value;
}
return value.replace(/@/g, '\\@').replace(/#/g, '\\#').replace(/%/g, '\\%').replace(/:/g, '\\:');
}

export function buildVariableString(variableDict: IVariableItem): string {
const scopeSig = '@';
const sysCodeSig = '#';
const userSig = '%';
const kvSig = ':';
const enableEscape = true;

const specialChars = new Set([scopeSig, sysCodeSig, userSig, kvSig]);

const newVariableDict: Partial<IVariableItem> = {
key: variableDict.key || '',
name: variableDict.name || '',
scope: variableDict.scope || '',
scope_key: variableDict.scope_key || '',
sys_code: variableDict.sys_code || '',
user_name: variableDict.user_name || '',
};

// Check for special characters in values
for (const [key, value] of Object.entries(newVariableDict)) {
if (value && [...specialChars].some(char => (value as string).includes(char))) {
if (enableEscape) {
newVariableDict[key] = escapeVariable(value as string, enableEscape);
} else {
throw new Error(
`${key} contains special characters, error value: ${value}, special characters: ${[...specialChars].join(', ')}`,
);
}
}
}

const { key, name, scope, scope_key, sys_code, user_name } = newVariableDict;

let variableStr = `${key}`;

if (name) variableStr += `${kvSig}${name}`;
if (scope || scope_key) {
variableStr += `${scopeSig}${scope}`;
if (scope_key) {
variableStr += `${kvSig}${scope_key}`;
}
}
if (sys_code) variableStr += `${sysCodeSig}${sys_code}`;
if (user_name) variableStr += `${userSig}${user_name}`;
return `\${${variableStr}}`;
}

0 comments on commit 25944c7

Please sign in to comment.