🚸 Automatically create variables when pasting groups to a new typebot
Closes #1587
This commit is contained in:
@ -14,6 +14,7 @@ import {
|
||||
IntegrationBlock,
|
||||
HttpRequestBlock,
|
||||
BlockWithOptionsType,
|
||||
BlockWithOptions,
|
||||
} from './features/blocks'
|
||||
import { BubbleBlockType } from './features/blocks/bubbles/constants'
|
||||
import { defaultChoiceInputOptions } from './features/blocks/inputs/choice/constants'
|
||||
@ -78,13 +79,8 @@ export const isBubbleBlockType = (
|
||||
): type is BubbleBlockType =>
|
||||
(Object.values(BubbleBlockType) as string[]).includes(type)
|
||||
|
||||
export const blockTypeHasOption = (
|
||||
type: Block['type']
|
||||
): type is BlockWithOptionsType =>
|
||||
(Object.values(InputBlockType) as string[])
|
||||
.concat(Object.values(LogicBlockType))
|
||||
.concat(Object.values(IntegrationBlockType))
|
||||
.includes(type)
|
||||
export const blockHasOptions = (block: Block): block is BlockWithOptions =>
|
||||
'options' in block
|
||||
|
||||
export const blockTypeHasItems = (
|
||||
type: Block['type']
|
||||
|
29
packages/variables/extractVariablesFromObject.ts
Normal file
29
packages/variables/extractVariablesFromObject.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { Variable } from './types'
|
||||
|
||||
const variableNameRegex = /\{\{([^{}]+)\}\}/g
|
||||
|
||||
export const extractVariableIdReferencesInObject = (
|
||||
obj: any,
|
||||
existingVariables: Variable[]
|
||||
): string[] =>
|
||||
[...(JSON.stringify(obj).match(variableNameRegex) ?? [])].reduce<string[]>(
|
||||
(acc, match) => {
|
||||
const varName = match.slice(2, -2)
|
||||
const id = existingVariables.find((v) => v.name === varName)?.id
|
||||
if (!id || acc.find((accId) => accId === id)) return acc
|
||||
return acc.concat(id)
|
||||
},
|
||||
[]
|
||||
)
|
||||
|
||||
const variableIdRegex = /"\w*variableid":"([^"]+)"/gi
|
||||
|
||||
export const extractVariableIdsFromObject = (obj: any): string[] =>
|
||||
[...(JSON.stringify(obj).match(variableIdRegex) ?? [])].reduce<string[]>(
|
||||
(acc, match) => {
|
||||
const id = variableIdRegex.exec(match)?.[1]
|
||||
if (!id || acc.find((accId) => accId === id)) return acc
|
||||
return acc.concat(id)
|
||||
},
|
||||
[]
|
||||
)
|
Reference in New Issue
Block a user