2023-08-07 14:32:30 +02:00
|
|
|
import { Tag, 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'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { byId } 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 =
|
2023-11-08 15:34:16 +01: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}>
|
2023-11-08 15:34:16 +01:00
|
|
|
{variableName === '' ? (
|
2023-08-07 14:32:30 +02:00
|
|
|
'Click to edit...'
|
|
|
|
) : (
|
|
|
|
<Expression
|
|
|
|
options={block.options}
|
|
|
|
variables={typebot?.variables ?? []}
|
|
|
|
/>
|
|
|
|
)}
|
2022-01-22 18:24:57 +01:00
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
}
|
2023-05-11 17:17:24 -04:00
|
|
|
|
2023-08-07 14:32:30 +02:00
|
|
|
const Expression = ({
|
|
|
|
options,
|
|
|
|
variables,
|
|
|
|
}: {
|
|
|
|
options: SetVariableBlock['options']
|
|
|
|
variables: Variable[]
|
|
|
|
}): JSX.Element | null => {
|
2023-11-08 15:34:16 +01:00
|
|
|
const variableName = variables.find(byId(options?.variableId))?.name ?? ''
|
|
|
|
switch (options?.type) {
|
2023-08-07 14:32:30 +02:00
|
|
|
case 'Custom':
|
|
|
|
case undefined:
|
|
|
|
return (
|
2023-08-17 09:39:11 +02:00
|
|
|
<Text as="span">
|
2023-11-08 15:34:16 +01:00
|
|
|
{variableName} = {options?.expressionToEvaluate}
|
2023-08-07 14:32:30 +02:00
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
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 (
|
2023-08-17 09:39:11 +02:00
|
|
|
<Text as="span">
|
2023-08-07 14:32:30 +02:00
|
|
|
{variableName} = item in ${targetListVariable?.name} with same index
|
|
|
|
as ${baseItemVariable?.name} in ${baseListVariable?.name}
|
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
}
|
2023-11-08 15:34:16 +01:00
|
|
|
case 'Append value(s)': {
|
|
|
|
return (
|
|
|
|
<Text as="span">
|
|
|
|
Append {options.item} in {variableName}
|
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
}
|
2023-08-07 14:32:30 +02:00
|
|
|
case 'Empty':
|
2024-06-27 08:22:22 +02:00
|
|
|
return <Text as="span">Reset {variableName} </Text>
|
|
|
|
case 'Shift':
|
|
|
|
case 'Pop': {
|
|
|
|
const itemVariableName = variables.find(
|
|
|
|
byId(options.saveItemInVariableId)
|
|
|
|
)?.name
|
|
|
|
return (
|
|
|
|
<Text as="span">
|
|
|
|
{options.type} {variableName}
|
|
|
|
{itemVariableName ? ` into ${itemVariableName}` : ''}
|
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
}
|
2023-08-07 14:32:30 +02:00
|
|
|
case 'Random ID':
|
|
|
|
case 'Today':
|
|
|
|
case 'Now':
|
|
|
|
case 'Tomorrow':
|
|
|
|
case 'User ID':
|
2024-02-16 11:34:54 +01:00
|
|
|
case 'Result ID':
|
2023-08-07 14:32:30 +02:00
|
|
|
case 'Moment of the day':
|
2023-09-26 09:50:20 +02:00
|
|
|
case 'Environment name':
|
2024-05-15 14:24:55 +02:00
|
|
|
case 'Transcript':
|
2023-08-07 14:32:30 +02:00
|
|
|
case 'Yesterday': {
|
|
|
|
return (
|
2023-08-17 09:39:11 +02:00
|
|
|
<Text as="span">
|
2023-08-07 14:32:30 +02:00
|
|
|
{variableName} = <Tag colorScheme="purple">System.{options.type}</Tag>
|
|
|
|
</Text>
|
|
|
|
)
|
2023-05-11 17:17:24 -04:00
|
|
|
}
|
2023-08-29 10:01:28 +02:00
|
|
|
case 'Contact name':
|
|
|
|
case 'Phone number':
|
|
|
|
return (
|
|
|
|
<Text as="span">
|
|
|
|
{variableName} ={' '}
|
|
|
|
<Tag colorScheme="purple">WhatsApp.{options.type}</Tag>
|
|
|
|
</Text>
|
|
|
|
)
|
2023-05-11 17:17:24 -04:00
|
|
|
}
|
2023-08-07 14:32:30 +02:00
|
|
|
}
|