Skip to content

Commit

Permalink
fix: slots_node generate fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MXerFix committed Sep 25, 2024
1 parent 111892f commit d1fc4b0
Showing 1 changed file with 67 additions and 56 deletions.
123 changes: 67 additions & 56 deletions frontend/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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,
DefaultNodeType,
LinkNodeDataType,
LinkNodeType,
NodesTypes,
} from "./types/NodeTypes";
SlotsNodeDataType,
} from "./types/NodeTypes"

export const generateNewFlow = (flow: CreateFlowType) => {
const newFlow: FlowType = {
Expand All @@ -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 {
Expand All @@ -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<DefaultNodeType, "data"> & {
data: Partial<DefaultNodeDataType>;
data: Partial<DefaultNodeDataType>
}) &
(Omit<LinkNodeType, "data"> & { data: Partial<LinkNodeDataType> })
>,
(Omit<LinkNodeType, "data"> & { data: Partial<LinkNodeDataType> }) &
(Omit<SlotsGroupType, "data"> & { data: Partial<SlotsNodeDataType> })
>
) => {
const id = type + "_" + v4();
const id = type + "_" + v4()
switch (type) {
case "default_node":
return {
Expand All @@ -106,7 +108,7 @@ export const generateNewNode = (
global_conditions: template?.data?.global_conditions ?? [],
local_conditions: template?.data?.local_conditions ?? [],
},
};
}
case "link_node":
return {
id,
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -151,8 +164,8 @@ export const generateNewSlot = (group_id: string): SlotType => {
type: "RegexpSlot",
method: "",
value: "",
};
};
}
}

export const generateNewSlotsGroup = (): SlotsGroupType => {
return {
Expand All @@ -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<Record<string, ParsedSlot>> {
const result: Record<string, ParsedSlot> = {};
export async function parseGroups(groups: SlotsGroupType[]): Promise<Record<string, ParsedSlot>> {
const result: Record<string, ParsedSlot> = {}

function processGroup(group: SlotsGroupType): ParsedSlot {
const groupData: ParsedSlot = {
id: group.id,
type: "GroupSlot",
};
}

// Обрабатываем слоты внутри группы
group.slots.forEach((slot) => {
Expand All @@ -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
}

0 comments on commit d1fc4b0

Please sign in to comment.