@ -9,6 +9,7 @@ import {
|
|||||||
} from './blocks'
|
} from './blocks'
|
||||||
import { isEmpty } from '@typebot.io/lib'
|
import { isEmpty } from '@typebot.io/lib'
|
||||||
import { Coordinates } from '@/features/graph/types'
|
import { Coordinates } from '@/features/graph/types'
|
||||||
|
import { parseUniqueKey } from '@typebot.io/lib/parseUniqueKey'
|
||||||
|
|
||||||
export type GroupsActions = {
|
export type GroupsActions = {
|
||||||
createGroup: (
|
createGroup: (
|
||||||
@ -64,19 +65,16 @@ const groupsActions = (setTypebot: SetTypebot): GroupsActions => ({
|
|||||||
const group = typebot.groups[groupIndex]
|
const group = typebot.groups[groupIndex]
|
||||||
const id = createId()
|
const id = createId()
|
||||||
|
|
||||||
const totalGroupsWithSameTitle = typebot.groups.filter(
|
const groupTitle = isEmpty(group.title)
|
||||||
(g) => g.title === group.title
|
? ''
|
||||||
).length
|
: parseUniqueKey(
|
||||||
|
group.title,
|
||||||
|
typebot.groups.map((g) => g.title)
|
||||||
|
)
|
||||||
|
|
||||||
const newGroup: GroupV6 = {
|
const newGroup: GroupV6 = {
|
||||||
...group,
|
...group,
|
||||||
title: isEmpty(group.title)
|
title: groupTitle,
|
||||||
? ''
|
|
||||||
: `${group.title}${
|
|
||||||
totalGroupsWithSameTitle > 0
|
|
||||||
? ` (${totalGroupsWithSameTitle})`
|
|
||||||
: ''
|
|
||||||
}`,
|
|
||||||
id,
|
id,
|
||||||
blocks: group.blocks.map((block) => duplicateBlockDraft(block)),
|
blocks: group.blocks.map((block) => duplicateBlockDraft(block)),
|
||||||
graphCoordinates: {
|
graphCoordinates: {
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
export const parseUniqueKey = (
|
export const parseUniqueKey = (key: string, existingKeys: string[]): string => {
|
||||||
key: string,
|
const keyMatcher = /^(.*?)\s*\(\d+\)$/
|
||||||
existingKeys: string[],
|
const parsedKey = keyMatcher.test(key) ? key.match(keyMatcher)![1] : key
|
||||||
count = 0
|
const sameKeyCount = existingKeys.reduce((acc, existingKey) => {
|
||||||
): string => {
|
if (
|
||||||
if (!existingKeys.includes(key)) return key
|
(keyMatcher.test(existingKey) &&
|
||||||
return parseUniqueKey(`${key} (${count + 1})`, existingKeys, count + 1)
|
existingKey.match(keyMatcher)![1] === parsedKey) ||
|
||||||
|
parsedKey === existingKey
|
||||||
|
) {
|
||||||
|
return acc + 1
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, 0)
|
||||||
|
if (sameKeyCount === 0) return parsedKey
|
||||||
|
return `${parsedKey} (${sameKeyCount})`
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user