From e1555268b2ed9163cf5fe200c785667588c7f785 Mon Sep 17 00:00:00 2001 From: medchedli Date: Mon, 9 Jun 2025 17:08:20 +0100 Subject: [PATCH] feat: implement PatternType enum and refactor pattern type handling in PatternInput --- .../form/inputs/triggers/PatternInput.tsx | 59 +++++-------------- frontend/src/types/block.types.ts | 10 +--- frontend/src/types/pattern.types.ts | 17 ++++++ frontend/src/utils/pattern.ts | 44 ++++++++++++++ 4 files changed, 78 insertions(+), 52 deletions(-) create mode 100644 frontend/src/types/pattern.types.ts create mode 100644 frontend/src/utils/pattern.ts diff --git a/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx b/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx index 14c3230e..1c7b718d 100644 --- a/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx +++ b/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx @@ -14,47 +14,14 @@ import { Input } from "@/app-components/inputs/Input"; import NlpPatternSelect from "@/app-components/inputs/NlpPatternSelect"; import { RegexInput } from "@/app-components/inputs/RegexInput"; import { useTranslate } from "@/hooks/useTranslate"; -import { - NlpPattern, - Pattern, - PatternType, - PayloadPattern, -} from "@/types/block.types"; -import { - extractRegexBody, - formatWithSlashes, - isRegex, - isRegexString, -} from "@/utils/string"; +import { NlpPattern, Pattern, PayloadPattern } from "@/types/block.types"; +import { PatternType } from "@/types/pattern.types"; +import { getPatternType } from "@/utils/pattern"; +import { extractRegexBody, formatWithSlashes, isRegex } from "@/utils/string"; import { OutcomeInput } from "./OutcomeInput"; import { PostbackInput } from "./PostbackInput"; -const getPatternType = (pattern: Pattern): PatternType => { - if (typeof pattern === "string") { - return isRegexString(pattern) ? "regex" : "text"; - } - - if (Array.isArray(pattern)) { - return "nlp"; - } - - if (pattern && typeof pattern === "object") { - switch (pattern.type) { - case "menu": - return "menu"; - case "content": - return "content"; - case "outcome": - return "outcome"; - default: - return "payload"; - } - } - - return "text"; -}; - type PatternInputProps = { control: Control; basePath: string; @@ -71,7 +38,7 @@ const PatternInput: FC = ({ control, basePath }) => { validate: (currentPatternValue: Pattern) => { const type = getPatternType(currentPatternValue); - if (type === "regex") { + if (type === PatternType.REGEX) { const regexString = currentPatternValue as string; if (!regexString || extractRegexBody(regexString).trim() === "") { @@ -80,7 +47,7 @@ const PatternInput: FC = ({ control, basePath }) => { if (!isRegex(extractRegexBody(regexString))) { return t("message.regex_is_invalid"); } - } else if (type === "text") { + } else if (type === PatternType.TEXT) { const textString = currentPatternValue as string; if (!textString || textString.trim() === "") { @@ -97,13 +64,17 @@ const PatternInput: FC = ({ control, basePath }) => { return ( - {currentPatternType === "nlp" && ( + {currentPatternType === PatternType.NLP && ( )} - {["payload", "content", "menu"].includes(currentPatternType) ? ( + {[ + PatternType.PAYLOAD, + PatternType.CONTENT, + PatternType.MENU, + ].includes(currentPatternType) ? ( { payload && field.onChange(payload); @@ -111,7 +82,7 @@ const PatternInput: FC = ({ control, basePath }) => { defaultValue={patternForPath as PayloadPattern} /> ) : null} - {currentPatternType === "outcome" ? ( + {currentPatternType === PatternType.OUTCOME ? ( { payload && field.onChange(payload); @@ -120,7 +91,7 @@ const PatternInput: FC = ({ control, basePath }) => { /> ) : null} {typeof patternForPath === "string" && - currentPatternType === "regex" ? ( + currentPatternType === PatternType.REGEX ? ( = ({ control, basePath }) => { /> ) : null} {typeof patternForPath === "string" && - currentPatternType === "text" ? ( + currentPatternType === PatternType.TEXT ? ( { + if (typeof pattern === "string") { + return isRegexString(pattern) ? PatternType.REGEX : PatternType.TEXT; + } + + if (Array.isArray(pattern)) { + return PatternType.NLP; + } + + if (pattern && typeof pattern === "object") { + switch (pattern.type) { + case "menu": + return PatternType.MENU; + case "content": + return PatternType.CONTENT; + case "outcome": + return PatternType.OUTCOME; + default: + return PatternType.PAYLOAD; + } + } + + return PatternType.TEXT; +};