feat: implement PatternType enum and refactor pattern type handling in PatternInput

This commit is contained in:
medchedli 2025-06-09 17:08:20 +01:00
parent c7d5406f9e
commit e1555268b2
4 changed files with 78 additions and 52 deletions

View File

@ -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<any>;
basePath: string;
@ -71,7 +38,7 @@ const PatternInput: FC<PatternInputProps> = ({ 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<PatternInputProps> = ({ 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<PatternInputProps> = ({ control, basePath }) => {
return (
<Box display="flex" flexGrow={1}>
{currentPatternType === "nlp" && (
{currentPatternType === PatternType.NLP && (
<NlpPatternSelect
patterns={patternForPath as NlpPattern[]}
onChange={field.onChange}
/>
)}
{["payload", "content", "menu"].includes(currentPatternType) ? (
{[
PatternType.PAYLOAD,
PatternType.CONTENT,
PatternType.MENU,
].includes(currentPatternType) ? (
<PostbackInput
onChange={(payload) => {
payload && field.onChange(payload);
@ -111,7 +82,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
defaultValue={patternForPath as PayloadPattern}
/>
) : null}
{currentPatternType === "outcome" ? (
{currentPatternType === PatternType.OUTCOME ? (
<OutcomeInput
onChange={(payload) => {
payload && field.onChange(payload);
@ -120,7 +91,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
/>
) : null}
{typeof patternForPath === "string" &&
currentPatternType === "regex" ? (
currentPatternType === PatternType.REGEX ? (
<RegexInput
value={extractRegexBody(patternForPath as string)}
label={t("label.regex")}
@ -133,7 +104,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
/>
) : null}
{typeof patternForPath === "string" &&
currentPatternType === "text" ? (
currentPatternType === PatternType.TEXT ? (
<Input
label={t("label.text")}
value={patternForPath as string}

View File

@ -20,6 +20,7 @@ import {
StdOutgoingTextMessage,
StdPluginMessage,
} from "./message.types";
import { PatternType } from "./pattern.types";
import { IUser } from "./user.types";
export type Position = {
@ -76,14 +77,7 @@ export type NlpPattern = {
export type Pattern = null | string | PayloadPattern | NlpPattern[];
export type PatternType =
| "regex"
| "nlp"
| "menu"
| "content"
| "outcome"
| "payload"
| "text";
export type { PatternType };
export interface IBlockAttributes {
name: string;

View File

@ -0,0 +1,17 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
export enum PatternType {
TEXT = "text",
REGEX = "regex",
NLP = "nlp",
PAYLOAD = "payload",
MENU = "menu",
CONTENT = "content",
OUTCOME = "outcome",
}

View File

@ -0,0 +1,44 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { Pattern } from "@/types/block.types";
import { PatternType } from "@/types/pattern.types";
import { isRegexString } from "./string";
/**
* Determines the type of a given pattern and returns the corresponding `PatternType`.
* Defaults to returning `PatternType.TEXT` if none of the conditions are met.
*
* @param pattern - The pattern to evaluate, which can be a string, array, or object.
* @returns The determined `PatternType` for the given pattern.
*/
export const getPatternType = (pattern: Pattern): PatternType => {
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;
};