mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: implement PatternType enum and refactor pattern type handling in PatternInput
This commit is contained in:
parent
c7d5406f9e
commit
e1555268b2
@ -14,47 +14,14 @@ import { Input } from "@/app-components/inputs/Input";
|
|||||||
import NlpPatternSelect from "@/app-components/inputs/NlpPatternSelect";
|
import NlpPatternSelect from "@/app-components/inputs/NlpPatternSelect";
|
||||||
import { RegexInput } from "@/app-components/inputs/RegexInput";
|
import { RegexInput } from "@/app-components/inputs/RegexInput";
|
||||||
import { useTranslate } from "@/hooks/useTranslate";
|
import { useTranslate } from "@/hooks/useTranslate";
|
||||||
import {
|
import { NlpPattern, Pattern, PayloadPattern } from "@/types/block.types";
|
||||||
NlpPattern,
|
import { PatternType } from "@/types/pattern.types";
|
||||||
Pattern,
|
import { getPatternType } from "@/utils/pattern";
|
||||||
PatternType,
|
import { extractRegexBody, formatWithSlashes, isRegex } from "@/utils/string";
|
||||||
PayloadPattern,
|
|
||||||
} from "@/types/block.types";
|
|
||||||
import {
|
|
||||||
extractRegexBody,
|
|
||||||
formatWithSlashes,
|
|
||||||
isRegex,
|
|
||||||
isRegexString,
|
|
||||||
} from "@/utils/string";
|
|
||||||
|
|
||||||
import { OutcomeInput } from "./OutcomeInput";
|
import { OutcomeInput } from "./OutcomeInput";
|
||||||
import { PostbackInput } from "./PostbackInput";
|
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 = {
|
type PatternInputProps = {
|
||||||
control: Control<any>;
|
control: Control<any>;
|
||||||
basePath: string;
|
basePath: string;
|
||||||
@ -71,7 +38,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
|
|||||||
validate: (currentPatternValue: Pattern) => {
|
validate: (currentPatternValue: Pattern) => {
|
||||||
const type = getPatternType(currentPatternValue);
|
const type = getPatternType(currentPatternValue);
|
||||||
|
|
||||||
if (type === "regex") {
|
if (type === PatternType.REGEX) {
|
||||||
const regexString = currentPatternValue as string;
|
const regexString = currentPatternValue as string;
|
||||||
|
|
||||||
if (!regexString || extractRegexBody(regexString).trim() === "") {
|
if (!regexString || extractRegexBody(regexString).trim() === "") {
|
||||||
@ -80,7 +47,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
|
|||||||
if (!isRegex(extractRegexBody(regexString))) {
|
if (!isRegex(extractRegexBody(regexString))) {
|
||||||
return t("message.regex_is_invalid");
|
return t("message.regex_is_invalid");
|
||||||
}
|
}
|
||||||
} else if (type === "text") {
|
} else if (type === PatternType.TEXT) {
|
||||||
const textString = currentPatternValue as string;
|
const textString = currentPatternValue as string;
|
||||||
|
|
||||||
if (!textString || textString.trim() === "") {
|
if (!textString || textString.trim() === "") {
|
||||||
@ -97,13 +64,17 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Box display="flex" flexGrow={1}>
|
<Box display="flex" flexGrow={1}>
|
||||||
{currentPatternType === "nlp" && (
|
{currentPatternType === PatternType.NLP && (
|
||||||
<NlpPatternSelect
|
<NlpPatternSelect
|
||||||
patterns={patternForPath as NlpPattern[]}
|
patterns={patternForPath as NlpPattern[]}
|
||||||
onChange={field.onChange}
|
onChange={field.onChange}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{["payload", "content", "menu"].includes(currentPatternType) ? (
|
{[
|
||||||
|
PatternType.PAYLOAD,
|
||||||
|
PatternType.CONTENT,
|
||||||
|
PatternType.MENU,
|
||||||
|
].includes(currentPatternType) ? (
|
||||||
<PostbackInput
|
<PostbackInput
|
||||||
onChange={(payload) => {
|
onChange={(payload) => {
|
||||||
payload && field.onChange(payload);
|
payload && field.onChange(payload);
|
||||||
@ -111,7 +82,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
|
|||||||
defaultValue={patternForPath as PayloadPattern}
|
defaultValue={patternForPath as PayloadPattern}
|
||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
{currentPatternType === "outcome" ? (
|
{currentPatternType === PatternType.OUTCOME ? (
|
||||||
<OutcomeInput
|
<OutcomeInput
|
||||||
onChange={(payload) => {
|
onChange={(payload) => {
|
||||||
payload && field.onChange(payload);
|
payload && field.onChange(payload);
|
||||||
@ -120,7 +91,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
|
|||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
{typeof patternForPath === "string" &&
|
{typeof patternForPath === "string" &&
|
||||||
currentPatternType === "regex" ? (
|
currentPatternType === PatternType.REGEX ? (
|
||||||
<RegexInput
|
<RegexInput
|
||||||
value={extractRegexBody(patternForPath as string)}
|
value={extractRegexBody(patternForPath as string)}
|
||||||
label={t("label.regex")}
|
label={t("label.regex")}
|
||||||
@ -133,7 +104,7 @@ const PatternInput: FC<PatternInputProps> = ({ control, basePath }) => {
|
|||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
{typeof patternForPath === "string" &&
|
{typeof patternForPath === "string" &&
|
||||||
currentPatternType === "text" ? (
|
currentPatternType === PatternType.TEXT ? (
|
||||||
<Input
|
<Input
|
||||||
label={t("label.text")}
|
label={t("label.text")}
|
||||||
value={patternForPath as string}
|
value={patternForPath as string}
|
||||||
|
@ -20,6 +20,7 @@ import {
|
|||||||
StdOutgoingTextMessage,
|
StdOutgoingTextMessage,
|
||||||
StdPluginMessage,
|
StdPluginMessage,
|
||||||
} from "./message.types";
|
} from "./message.types";
|
||||||
|
import { PatternType } from "./pattern.types";
|
||||||
import { IUser } from "./user.types";
|
import { IUser } from "./user.types";
|
||||||
|
|
||||||
export type Position = {
|
export type Position = {
|
||||||
@ -76,14 +77,7 @@ export type NlpPattern = {
|
|||||||
|
|
||||||
export type Pattern = null | string | PayloadPattern | NlpPattern[];
|
export type Pattern = null | string | PayloadPattern | NlpPattern[];
|
||||||
|
|
||||||
export type PatternType =
|
export type { PatternType };
|
||||||
| "regex"
|
|
||||||
| "nlp"
|
|
||||||
| "menu"
|
|
||||||
| "content"
|
|
||||||
| "outcome"
|
|
||||||
| "payload"
|
|
||||||
| "text";
|
|
||||||
|
|
||||||
export interface IBlockAttributes {
|
export interface IBlockAttributes {
|
||||||
name: string;
|
name: string;
|
||||||
|
17
frontend/src/types/pattern.types.ts
Normal file
17
frontend/src/types/pattern.types.ts
Normal 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",
|
||||||
|
}
|
44
frontend/src/utils/pattern.ts
Normal file
44
frontend/src/utils/pattern.ts
Normal 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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user