From d1fc4b06e0c6b992fbec9aa7465d367709b32c52 Mon Sep 17 00:00:00 2001 From: MXerFix Date: Wed, 25 Sep 2024 14:11:30 +0300 Subject: [PATCH] fix: slots_node generate fix --- frontend/src/utils.ts | 123 +++++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 56 deletions(-) diff --git a/frontend/src/utils.ts b/frontend/src/utils.ts index dbac4c4..813fca6 100644 --- a/frontend/src/utils.ts +++ b/frontend/src/utils.ts @@ -1,7 +1,7 @@ -import { v4 } from "uuid"; -import { CreateFlowType } from "./modals/FlowModal/CreateFlowModal"; -import { conditionType } from "./types/ConditionTypes"; -import { FlowType, SlotsGroupType, SlotType } from "./types/FlowTypes"; +import { v4 } from "uuid" +import { CreateFlowType } from "./modals/FlowModal/CreateFlowModal" +import { conditionType } from "./types/ConditionTypes" +import { FlowType, SlotsGroupType, SlotType } from "./types/FlowTypes" import { AppNode, DefaultNodeDataType, @@ -9,7 +9,8 @@ import { LinkNodeDataType, LinkNodeType, NodesTypes, -} from "./types/NodeTypes"; + SlotsNodeDataType, +} from "./types/NodeTypes" export const generateNewFlow = (flow: CreateFlowType) => { const newFlow: FlowType = { @@ -24,32 +25,32 @@ export const generateNewFlow = (flow: CreateFlowType) => { zoom: 1, }, }, - }; - return newFlow; -}; + } + return newFlow +} export const validateFlowName = (name: string, flows: FlowType[]) => { - return !flows.some((flow) => flow.name === name) && name.length >= 2; -}; + return !flows.some((flow) => flow.name === name) && name.length >= 2 +} export function capitalizeFirstWord(str: string) { return str.replace(/\w\S*/g, function (txt) { - return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase(); - }); + return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase() + }) } export const parseSearchParams = ( - searchParams: URLSearchParams, + searchParams: URLSearchParams ): { - [key: string]: string; + [key: string]: string } => { - if (!searchParams.toString()) return {}; + if (!searchParams.toString()) return {} return searchParams .toString() .split("&") .map((s) => s.split("=")) - .reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {}); -}; + .reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {}) +} export const generateNewConditionBase = (): conditionType => { return { @@ -60,32 +61,33 @@ export const generateNewConditionBase = (): conditionType => { priority: 1, transition_type: "manual", }, - }; -}; + } +} export const isNodeDeletionValid = (nodes: AppNode[], id: string) => { - const node = nodes.find((n) => n.id === id); - if (!node) return false; - if (node.type === "link_node") return true; - if (node.type === "default_node") return !node.data.flags?.includes("start"); -}; + const node = nodes.find((n) => n.id === id) + if (!node) return false + if (node.type === "link_node") return true + if (node.type === "default_node") return !node.data.flags?.includes("start") +} export function delay(ms: number) { return new Promise((resolve) => { - setTimeout(resolve, ms); - }); + setTimeout(resolve, ms) + }) } export const generateNewNode = ( type: NodesTypes | undefined, template?: Partial< (Omit & { - data: Partial; + data: Partial }) & - (Omit & { data: Partial }) - >, + (Omit & { data: Partial }) & + (Omit & { data: Partial }) + > ) => { - const id = type + "_" + v4(); + const id = type + "_" + v4() switch (type) { case "default_node": return { @@ -106,7 +108,7 @@ export const generateNewNode = ( global_conditions: template?.data?.global_conditions ?? [], local_conditions: template?.data?.local_conditions ?? [], }, - }; + } case "link_node": return { id, @@ -120,7 +122,18 @@ export const generateNewNode = ( target_node: template?.data?.transition?.target_flow ?? "", }, }, - }; + } + case "slots_node": + return { + id, + type, + position: template?.position ?? { x: 0, y: 0 }, + data: { + id, + name: template?.data?.name ?? "Slots", + groups: template?.data?.groups ?? [], + }, + } } return { id, @@ -140,8 +153,8 @@ export const generateNewNode = ( global_conditions: template?.data?.global_conditions ?? [], local_conditions: template?.data?.local_conditions ?? [], }, - }; -}; + } +} export const generateNewSlot = (group_id: string): SlotType => { return { @@ -151,8 +164,8 @@ export const generateNewSlot = (group_id: string): SlotType => { type: "RegexpSlot", method: "", value: "", - }; -}; + } +} export const generateNewSlotsGroup = (): SlotsGroupType => { return { @@ -162,25 +175,23 @@ export const generateNewSlotsGroup = (): SlotsGroupType => { flow: "global", subgroups: [], subgroup_to: "", - }; -}; + } +} export type ParsedSlot = { - id: string; - type: "GroupSlot" | "RegexpSlot"; - [key: string]: unknown; -}; + id: string + type: "GroupSlot" | "RegexpSlot" + [key: string]: unknown +} -export async function parseGroups( - groups: SlotsGroupType[], -): Promise> { - const result: Record = {}; +export async function parseGroups(groups: SlotsGroupType[]): Promise> { + const result: Record = {} function processGroup(group: SlotsGroupType): ParsedSlot { const groupData: ParsedSlot = { id: group.id, type: "GroupSlot", - }; + } // Обрабатываем слоты внутри группы group.slots.forEach((slot) => { @@ -190,29 +201,29 @@ export async function parseGroups( type: slot.type, regexp: slot.value, // Предполагаем, что value хранит регулярное выражение match_group_idx: 1, // Здесь предполагается индекс группы захвата - }; + } } - }); + }) // Если у группы есть подгруппы, обрабатываем их рекурсивно if (group.subgroups) { group.subgroups.forEach((subgroupId) => { - const subgroup = groups.find((g) => g.id === subgroupId); + const subgroup = groups.find((g) => g.id === subgroupId) if (subgroup) { - groupData[subgroup.name] = processGroup(subgroup); + groupData[subgroup.name] = processGroup(subgroup) } - }); + }) } - return groupData; + return groupData } // Перебираем все группы верхнего уровня и строим структуру groups.forEach((group) => { if (!group.subgroup_to) { - result[group.name] = processGroup(group); + result[group.name] = processGroup(group) } - }); + }) - return result; + return result }