2022-01-22 18:24:57 +01:00
|
|
|
import { Text } from '@chakra-ui/react'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { useTypebot } from '@/features/editor/providers/TypebotProvider'
|
2023-05-11 17:17:24 -04:00
|
|
|
import { SetVariableBlock, Variable } from '@typebot.io/schemas'
|
|
|
|
import { byId, isEmpty } from '@typebot.io/lib'
|
2022-01-22 18:24:57 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const SetVariableContent = ({ block }: { block: SetVariableBlock }) => {
|
2022-01-22 18:24:57 +01:00
|
|
|
const { typebot } = useTypebot()
|
|
|
|
const variableName =
|
2022-06-11 07:27:38 +02:00
|
|
|
typebot?.variables.find(byId(block.options.variableId))?.name ?? ''
|
2022-01-22 18:24:57 +01:00
|
|
|
return (
|
2023-05-11 17:17:24 -04:00
|
|
|
<Text color={'gray.500'} noOfLines={4}>
|
|
|
|
{variableName === '' && isEmpty(block.options.expressionToEvaluate)
|
2022-01-22 18:24:57 +01:00
|
|
|
? 'Click to edit...'
|
2023-05-11 17:17:24 -04:00
|
|
|
: getExpression(typebot?.variables ?? [])(block.options)}
|
2022-01-22 18:24:57 +01:00
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
}
|
2023-05-11 17:17:24 -04:00
|
|
|
|
|
|
|
const getExpression =
|
|
|
|
(variables: Variable[]) =>
|
|
|
|
(options: SetVariableBlock['options']): string | null => {
|
|
|
|
const variableName = variables.find(byId(options.variableId))?.name ?? ''
|
|
|
|
switch (options.type) {
|
|
|
|
case 'Custom':
|
|
|
|
case undefined:
|
|
|
|
return `${variableName} = ${options.expressionToEvaluate}`
|
|
|
|
case 'Map item with same index': {
|
|
|
|
const baseItemVariable = variables.find(
|
|
|
|
byId(options.mapListItemParams?.baseItemVariableId)
|
|
|
|
)
|
|
|
|
const baseListVariable = variables.find(
|
|
|
|
byId(options.mapListItemParams?.baseListVariableId)
|
|
|
|
)
|
|
|
|
const targetListVariable = variables.find(
|
|
|
|
byId(options.mapListItemParams?.targetListVariableId)
|
|
|
|
)
|
|
|
|
return `${variableName} = item in ${targetListVariable?.name} with same index as ${baseItemVariable?.name} in ${baseListVariable?.name}`
|
|
|
|
}
|
|
|
|
case 'Empty':
|
|
|
|
return `Reset ${variableName}`
|
|
|
|
case 'Random ID':
|
|
|
|
case 'Today':
|
|
|
|
case 'Tomorrow':
|
|
|
|
case 'User ID':
|
|
|
|
case 'Yesterday': {
|
|
|
|
return `${variableName} = ${options.type}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|