From 203747a2a4b3752fc27d4e32d1d1d6076d86df7f Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sat, 26 Oct 2024 07:24:59 +0100 Subject: [PATCH 1/4] fix(frontend): visual editor pattern UI representation --- .../visual-editor/form/inputs/triggers/PatternInput.tsx | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) 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 2e90c7f..1a559dd 100644 --- a/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx +++ b/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx @@ -268,19 +268,14 @@ const PatternInput: FC = ({ {...registerInput(t("message.regex_is_invalid"), idx, { validate: (pattern) => { try { - if ( - pattern.at(0) === "/" && - pattern.at(-1) === "/" && - typeof pattern === "string" - ) - new RegExp(pattern.slice(1, -1)); + if (isRegex(pattern)) new RegExp(pattern.slice(1, -1)); return true; } catch (_e) { return t("message.regex_is_invalid"); } }, - setValueAs: (v) => `/${v}/`, + setValueAs: (v) => (isRegex(v) ? v : `/${v}/`), })} label={t("label.regex")} value={value.slice(1, -1)} From 280cf7ee88070dbabb06424c76de377791771a86 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sat, 26 Oct 2024 09:57:26 +0100 Subject: [PATCH 2/4] fix(frontend): remove unused pattern check --- .../visual-editor/form/inputs/triggers/PatternInput.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1a559dd..22eed1f 100644 --- a/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx +++ b/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx @@ -268,7 +268,7 @@ const PatternInput: FC = ({ {...registerInput(t("message.regex_is_invalid"), idx, { validate: (pattern) => { try { - if (isRegex(pattern)) new RegExp(pattern.slice(1, -1)); + new RegExp(pattern.slice(1, -1)); return true; } catch (_e) { From fac05d37211e272d9ee250dfe8609e2b56be8860 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sat, 26 Oct 2024 10:03:51 +0100 Subject: [PATCH 3/4] fix(frontend): support one slash case --- .../visual-editor/form/inputs/triggers/PatternInput.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 22eed1f..769320d 100644 --- a/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx +++ b/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx @@ -32,7 +32,12 @@ import { ContentPostbackInput } from "./ContentPostbackInput"; import { PostbackInput } from "./PostbackInput"; const isRegex = (str: Pattern) => { - return typeof str === "string" && str.startsWith("/") && str.endsWith("/"); + return ( + typeof str === "string" && + str.startsWith("/") && + str.endsWith("/") && + str.length > 1 + ); }; const getType = (pattern: Pattern): PatternType => { if (isRegex(pattern)) { From 84e27094f715140e01a7dcbd7fd0a1f50de61f83 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sat, 26 Oct 2024 13:44:00 +0100 Subject: [PATCH 4/4] fix(frontend): enhance empty regex+invalid regex+UI issue --- frontend/public/locales/en/translation.json | 1 + frontend/public/locales/fr/translation.json | 1 + .../form/inputs/triggers/PatternInput.tsx | 16 ++++++++-------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/frontend/public/locales/en/translation.json b/frontend/public/locales/en/translation.json index 9811b52..7ed3d2e 100644 --- a/frontend/public/locales/en/translation.json +++ b/frontend/public/locales/en/translation.json @@ -100,6 +100,7 @@ "no_content_type": "No content type available, please create one first", "invalid_max_fallback_attempt_limit": "Max fallback attempt limit must have positive value", "regex_is_invalid": "Regex is invalid", + "regex_is_empty": "Regex cannot be empty", "attachment_not_found": "Attachment is not found", "title_length_exceeded": "You have reached the maximum length", "no_label_found": "No label found", diff --git a/frontend/public/locales/fr/translation.json b/frontend/public/locales/fr/translation.json index d06616c..f2390ea 100644 --- a/frontend/public/locales/fr/translation.json +++ b/frontend/public/locales/fr/translation.json @@ -100,6 +100,7 @@ "no_content_type": "Il n'y a aucun type de contenu pour le moment, veuillez en ajouter un.", "invalid_max_fallback_attempt_limit": "La limite des tentatives de secours doit être un nombre positif.", "regex_is_invalid": "Le regex est invalide", + "regex_is_empty": "Le regex ne peut pas être vide", "attachment_not_found": "La pièce jointe est introuvable", "title_length_exceeded": "Vous avez atteint la longueur maximale", "no_label_found": "Aucune étiquette trouvée", 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 769320d..c0152bb 100644 --- a/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx +++ b/frontend/src/components/visual-editor/form/inputs/triggers/PatternInput.tsx @@ -32,12 +32,7 @@ import { ContentPostbackInput } from "./ContentPostbackInput"; import { PostbackInput } from "./PostbackInput"; const isRegex = (str: Pattern) => { - return ( - typeof str === "string" && - str.startsWith("/") && - str.endsWith("/") && - str.length > 1 - ); + return typeof str === "string" && str.startsWith("/") && str.endsWith("/"); }; const getType = (pattern: Pattern): PatternType => { if (isRegex(pattern)) { @@ -270,10 +265,14 @@ const PatternInput: FC = ({ ) : null} {typeof value === "string" && patternType === "regex" ? ( { try { - new RegExp(pattern.slice(1, -1)); + const parsedPattern = new RegExp(pattern.slice(1, -1)); + + if (String(parsedPattern) !== pattern) { + throw t("message.regex_is_invalid"); + } return true; } catch (_e) { @@ -285,6 +284,7 @@ const PatternInput: FC = ({ label={t("label.regex")} value={value.slice(1, -1)} onChange={(v) => onChange(v)} + required /> ) : null} {typeof value === "string" && patternType === "text" ? (