2
0

♻️ Rename Code block to Script block

Closes #245
This commit is contained in:
Baptiste Arnaud
2023-01-27 15:58:05 +01:00
parent 068f9bbd17
commit a842f57297
35 changed files with 113 additions and 103 deletions

View File

@@ -1,7 +0,0 @@
import { CodeIcon as CodeIco } from '@/components/icons'
import { IconProps } from '@chakra-ui/react'
import React from 'react'
export const CodeIcon = (props: IconProps) => (
<CodeIco color="purple.500" {...props} />
)

View File

@@ -1,3 +0,0 @@
export { CodeSettings } from './components/CodeSettings'
export { CodeNodeContent } from './components/CodeNodeContent'
export { CodeIcon } from './components/CodeIcon'

View File

@@ -0,0 +1,15 @@
import { featherIconsBaseProps } from '@/components/icons'
import { Icon, IconProps, useColorModeValue } from '@chakra-ui/react'
import React from 'react'
export const ScriptIcon = (props: IconProps) => (
<Icon
viewBox="0 0 24 24"
color={useColorModeValue('purple.500', 'purple.300')}
{...featherIconsBaseProps}
{...props}
>
<polyline points="4 17 10 11 4 5"></polyline>
<line x1="12" y1="19" x2="20" y2="19"></line>
</Icon>
)

View File

@@ -1,10 +1,10 @@
import React from 'react' import React from 'react'
import { Text } from '@chakra-ui/react' import { Text } from '@chakra-ui/react'
import { CodeOptions } from 'models' import { ScriptOptions } from 'models'
type Props = CodeOptions type Props = ScriptOptions
export const CodeNodeContent = ({ name, content }: Props) => ( export const ScriptNodeContent = ({ name, content }: Props) => (
<Text color={content ? 'currentcolor' : 'gray.500'} noOfLines={1}> <Text color={content ? 'currentcolor' : 'gray.500'} noOfLines={1}>
{content ? `Run ${name}` : 'Configure...'} {content ? `Run ${name}` : 'Configure...'}
</Text> </Text>

View File

@@ -1,16 +1,16 @@
import { FormLabel, Stack, Text } from '@chakra-ui/react' import { FormLabel, Stack, Text } from '@chakra-ui/react'
import { CodeEditor } from '@/components/CodeEditor' import { CodeEditor } from '@/components/CodeEditor'
import { CodeOptions } from 'models'
import React from 'react' import React from 'react'
import { SwitchWithLabel } from '@/components/SwitchWithLabel' import { SwitchWithLabel } from '@/components/SwitchWithLabel'
import { Input } from '@/components/inputs' import { Input } from '@/components/inputs'
import { ScriptOptions } from 'models'
type Props = { type Props = {
options: CodeOptions options: ScriptOptions
onOptionsChange: (options: CodeOptions) => void onOptionsChange: (options: ScriptOptions) => void
} }
export const CodeSettings = ({ options, onOptionsChange }: Props) => { export const ScriptSettings = ({ options, onOptionsChange }: Props) => {
const handleNameChange = (name: string) => const handleNameChange = (name: string) =>
onOptionsChange({ ...options, name }) onOptionsChange({ ...options, name })
const handleCodeChange = (content: string) => const handleCodeChange = (content: string) =>

View File

@@ -0,0 +1,3 @@
export { ScriptSettings } from './components/ScriptSettings'
export { ScriptNodeContent } from './components/ScriptNodeContent'
export { ScriptIcon } from './components/ScriptIcon'

View File

@@ -6,9 +6,9 @@ import { getTestAsset } from '@/test/utils/playwright'
const typebotId = cuid() const typebotId = cuid()
test.describe('Code block', () => { test.describe('Script block', () => {
test('code should trigger', async ({ page }) => { test('script should trigger', async ({ page }) => {
await importTypebotInDatabase(getTestAsset('typebots/logic/code.json'), { await importTypebotInDatabase(getTestAsset('typebots/logic/script.json'), {
id: typebotId, id: typebotId,
}) })

View File

@@ -19,7 +19,6 @@ import { ZapierLogo } from '@/features/blocks/integrations/zapier'
import { WebhookIcon } from '@/features/blocks/integrations/webhook' import { WebhookIcon } from '@/features/blocks/integrations/webhook'
import { GoogleSheetsLogo } from '@/features/blocks/integrations/googleSheets' import { GoogleSheetsLogo } from '@/features/blocks/integrations/googleSheets'
import { TypebotLinkIcon } from '@/features/blocks/logic/typebotLink' import { TypebotLinkIcon } from '@/features/blocks/logic/typebotLink'
import { CodeIcon } from '@/features/blocks/logic/code'
import { RedirectIcon } from '@/features/blocks/logic/redirect' import { RedirectIcon } from '@/features/blocks/logic/redirect'
import { ConditionIcon } from '@/features/blocks/logic/condition' import { ConditionIcon } from '@/features/blocks/logic/condition'
import { SetVariableIcon } from '@/features/blocks/logic/setVariable' import { SetVariableIcon } from '@/features/blocks/logic/setVariable'
@@ -37,6 +36,7 @@ import { EmbedBubbleIcon } from '@/features/blocks/bubbles/embed'
import { GoogleAnalyticsLogo } from '@/features/blocks/integrations/googleAnalytics' import { GoogleAnalyticsLogo } from '@/features/blocks/integrations/googleAnalytics'
import { AudioBubbleIcon } from '@/features/blocks/bubbles/audio' import { AudioBubbleIcon } from '@/features/blocks/bubbles/audio'
import { WaitIcon } from '@/features/blocks/logic/wait/components/WaitIcon' import { WaitIcon } from '@/features/blocks/logic/wait/components/WaitIcon'
import { ScriptIcon } from '@/features/blocks/logic/script/components/ScriptIcon'
type BlockIconProps = { type: BlockType } & IconProps type BlockIconProps = { type: BlockType } & IconProps
@@ -81,8 +81,8 @@ export const BlockIcon = ({ type, ...props }: BlockIconProps) => {
return <ConditionIcon color={purple} {...props} /> return <ConditionIcon color={purple} {...props} />
case LogicBlockType.REDIRECT: case LogicBlockType.REDIRECT:
return <RedirectIcon color={purple} {...props} /> return <RedirectIcon color={purple} {...props} />
case LogicBlockType.CODE: case LogicBlockType.SCRIPT:
return <CodeIcon color={purple} {...props} /> return <ScriptIcon {...props} />
case LogicBlockType.WAIT: case LogicBlockType.WAIT:
return <WaitIcon color={purple} {...props} /> return <WaitIcon color={purple} {...props} />
case LogicBlockType.TYPEBOT_LINK: case LogicBlockType.TYPEBOT_LINK:

View File

@@ -65,10 +65,10 @@ export const BlockTypeLabel = ({ type }: Props): JSX.Element => {
return <Text>Condition</Text> return <Text>Condition</Text>
case LogicBlockType.REDIRECT: case LogicBlockType.REDIRECT:
return <Text>Redirect</Text> return <Text>Redirect</Text>
case LogicBlockType.CODE: case LogicBlockType.SCRIPT:
return ( return (
<Tooltip label="Run Javascript code"> <Tooltip label="Run Javascript code">
<Text>Code</Text> <Text>Script</Text>
</Tooltip> </Tooltip>
) )
case LogicBlockType.TYPEBOT_LINK: case LogicBlockType.TYPEBOT_LINK:

View File

@@ -23,7 +23,6 @@ import { SetVariableContent } from '@/features/blocks/logic/setVariable'
import { WebhookContent } from '@/features/blocks/integrations/webhook' import { WebhookContent } from '@/features/blocks/integrations/webhook'
import { ChatwootBlockNodeLabel } from '@/features/blocks/integrations/chatwoot' import { ChatwootBlockNodeLabel } from '@/features/blocks/integrations/chatwoot'
import { RedirectNodeContent } from '@/features/blocks/logic/redirect' import { RedirectNodeContent } from '@/features/blocks/logic/redirect'
import { CodeNodeContent } from '@/features/blocks/logic/code'
import { PabblyConnectContent } from '@/features/blocks/integrations/pabbly' import { PabblyConnectContent } from '@/features/blocks/integrations/pabbly'
import { WithVariableContent } from './WithVariableContent' import { WithVariableContent } from './WithVariableContent'
import { PaymentInputContent } from '@/features/blocks/inputs/payment' import { PaymentInputContent } from '@/features/blocks/inputs/payment'
@@ -38,6 +37,7 @@ import { isInputBlock, isChoiceInput, blockHasItems } from 'utils'
import { MakeComContent } from '@/features/blocks/integrations/makeCom' import { MakeComContent } from '@/features/blocks/integrations/makeCom'
import { AudioBubbleNode } from '@/features/blocks/bubbles/audio' import { AudioBubbleNode } from '@/features/blocks/bubbles/audio'
import { WaitNodeContent } from '@/features/blocks/logic/wait/components/WaitNodeContent' import { WaitNodeContent } from '@/features/blocks/logic/wait/components/WaitNodeContent'
import { ScriptNodeContent } from '@/features/blocks/logic/script/components/ScriptNodeContent'
type Props = { type Props = {
block: Block | StartBlock block: Block | StartBlock
@@ -113,9 +113,9 @@ export const BlockNodeContent = ({ block, indices }: Props): JSX.Element => {
case LogicBlockType.REDIRECT: { case LogicBlockType.REDIRECT: {
return <RedirectNodeContent url={block.options.url} /> return <RedirectNodeContent url={block.options.url} />
} }
case LogicBlockType.CODE: { case LogicBlockType.SCRIPT: {
return ( return (
<CodeNodeContent <ScriptNodeContent
name={block.options.name} name={block.options.name}
content={block.options.content} content={block.options.content}
/> />

View File

@@ -38,8 +38,8 @@ const getHelpDocUrl = (blockType: BlockWithOptions['type']): string | null => {
return 'https://docs.typebot.io/editor/blocks/logic/set-variable' return 'https://docs.typebot.io/editor/blocks/logic/set-variable'
case LogicBlockType.REDIRECT: case LogicBlockType.REDIRECT:
return 'https://docs.typebot.io/editor/blocks/logic/redirect' return 'https://docs.typebot.io/editor/blocks/logic/redirect'
case LogicBlockType.CODE: case LogicBlockType.SCRIPT:
return 'https://docs.typebot.io/editor/blocks/logic/code' return 'https://docs.typebot.io/editor/blocks/logic/script'
case LogicBlockType.WAIT: case LogicBlockType.WAIT:
return 'https://docs.typebot.io/editor/blocks/logic/wait' return 'https://docs.typebot.io/editor/blocks/logic/wait'
case InputBlockType.TEXT: case InputBlockType.TEXT:

View File

@@ -33,7 +33,6 @@ import { GoogleSheetsSettingsBody } from '@/features/blocks/integrations/googleS
import { SendEmailSettings } from '@/features/blocks/integrations/sendEmail' import { SendEmailSettings } from '@/features/blocks/integrations/sendEmail'
import { WebhookSettings } from '@/features/blocks/integrations/webhook' import { WebhookSettings } from '@/features/blocks/integrations/webhook'
import { ZapierSettings } from '@/features/blocks/integrations/zapier' import { ZapierSettings } from '@/features/blocks/integrations/zapier'
import { CodeSettings } from '@/features/blocks/logic/code'
import { RedirectSettings } from '@/features/blocks/logic/redirect' import { RedirectSettings } from '@/features/blocks/logic/redirect'
import { SetVariableSettings } from '@/features/blocks/logic/setVariable' import { SetVariableSettings } from '@/features/blocks/logic/setVariable'
import { TypebotLinkForm } from '@/features/blocks/logic/typebotLink' import { TypebotLinkForm } from '@/features/blocks/logic/typebotLink'
@@ -42,6 +41,7 @@ import { ChatwootSettingsForm } from '@/features/blocks/integrations/chatwoot'
import { MakeComSettings } from '@/features/blocks/integrations/makeCom' import { MakeComSettings } from '@/features/blocks/integrations/makeCom'
import { HelpDocButton } from './HelpDocButton' import { HelpDocButton } from './HelpDocButton'
import { WaitSettings } from '@/features/blocks/logic/wait/components/WaitSettings' import { WaitSettings } from '@/features/blocks/logic/wait/components/WaitSettings'
import { ScriptSettings } from '@/features/blocks/logic/script/components/ScriptSettings'
type Props = { type Props = {
block: BlockWithOptions block: BlockWithOptions
@@ -197,9 +197,9 @@ export const BlockSettings = ({
/> />
) )
} }
case LogicBlockType.CODE: { case LogicBlockType.SCRIPT: {
return ( return (
<CodeSettings <ScriptSettings
options={block.options} options={block.options}
onOptionsChange={handleOptionsChange} onOptionsChange={handleOptionsChange}
/> />

View File

@@ -6,7 +6,6 @@ import {
BubbleBlockType, BubbleBlockType,
defaultChatwootOptions, defaultChatwootOptions,
defaultChoiceInputOptions, defaultChoiceInputOptions,
defaultCodeOptions,
defaultConditionContent, defaultConditionContent,
defaultDateInputOptions, defaultDateInputOptions,
defaultEmailInputOptions, defaultEmailInputOptions,
@@ -38,6 +37,7 @@ import {
ItemType, ItemType,
LogicBlockType, LogicBlockType,
defaultWaitOptions, defaultWaitOptions,
defaultScriptOptions,
} from 'models' } from 'models'
import { import {
stubLength, stubLength,
@@ -433,8 +433,8 @@ const parseDefaultBlockOptions = (type: BlockWithOptionsType): BlockOptions => {
return defaultSetVariablesOptions return defaultSetVariablesOptions
case LogicBlockType.REDIRECT: case LogicBlockType.REDIRECT:
return defaultRedirectOptions return defaultRedirectOptions
case LogicBlockType.CODE: case LogicBlockType.SCRIPT:
return defaultCodeOptions return defaultScriptOptions
case LogicBlockType.WAIT: case LogicBlockType.WAIT:
return defaultWaitOptions return defaultWaitOptions
case LogicBlockType.TYPEBOT_LINK: case LogicBlockType.TYPEBOT_LINK:

View File

@@ -1,6 +1,6 @@
# Code # Script
The "Code" block allows you to execute Javascript code. If you want to set a variable value with Javascript, use the [Set variable block](./set-variable) instead. The "Script" block allows you to execute Javascript code. If you want to set a variable value with Javascript, use the [Set variable block](./set-variable) instead.
**It doesn't allow you to create a custom visual block** **It doesn't allow you to create a custom visual block**

View File

@@ -54,7 +54,8 @@
"PRO", "PRO",
"LIFETIME", "LIFETIME",
"OFFERED", "OFFERED",
"CUSTOM" "CUSTOM",
"UNLIMITED"
] ]
} }
}, },
@@ -145,7 +146,8 @@
"PRO", "PRO",
"LIFETIME", "LIFETIME",
"OFFERED", "OFFERED",
"CUSTOM" "CUSTOM",
"UNLIMITED"
] ]
}, },
"stripeId": { "stripeId": {
@@ -280,7 +282,8 @@
"PRO", "PRO",
"LIFETIME", "LIFETIME",
"OFFERED", "OFFERED",
"CUSTOM" "CUSTOM",
"UNLIMITED"
] ]
}, },
"stripeId": { "stripeId": {
@@ -432,7 +435,8 @@
"PRO", "PRO",
"LIFETIME", "LIFETIME",
"OFFERED", "OFFERED",
"CUSTOM" "CUSTOM",
"UNLIMITED"
] ]
}, },
"stripeId": { "stripeId": {

View File

@@ -4498,7 +4498,7 @@
{ {
"type": "object", "type": "object",
"properties": { "properties": {
"codeToExecute": { "scriptToExecute": {
"type": "object", "type": "object",
"properties": { "properties": {
"content": { "content": {
@@ -4553,7 +4553,7 @@
} }
}, },
"required": [ "required": [
"codeToExecute" "scriptToExecute"
], ],
"additionalProperties": false "additionalProperties": false
}, },
@@ -4589,7 +4589,7 @@
"chatwoot": { "chatwoot": {
"type": "object", "type": "object",
"properties": { "properties": {
"codeToExecute": { "scriptToExecute": {
"type": "object", "type": "object",
"properties": { "properties": {
"content": { "content": {
@@ -4644,7 +4644,7 @@
} }
}, },
"required": [ "required": [
"codeToExecute" "scriptToExecute"
], ],
"additionalProperties": false "additionalProperties": false
} }

View File

@@ -61,7 +61,7 @@ export const executeChatwootBlock = (
{ {
lastBubbleBlockId, lastBubbleBlockId,
chatwoot: { chatwoot: {
codeToExecute: { scriptToExecute: {
content: parseVariables(variables, { fieldToParse: 'id' })( content: parseVariables(variables, { fieldToParse: 'id' })(
chatwootCode chatwootCode
), ),

View File

@@ -1 +0,0 @@
export { executeCode } from './utils/executeCode'

View File

@@ -4,11 +4,11 @@ import {
parseCorrectValueType, parseCorrectValueType,
extractVariablesFromText, extractVariablesFromText,
} from '@/features/variables' } from '@/features/variables'
import { CodeBlock, SessionState } from 'models' import { ScriptBlock, SessionState } from 'models'
export const executeCode = ( export const executeScript = (
{ typebot: { variables } }: SessionState, { typebot: { variables } }: SessionState,
block: CodeBlock, block: ScriptBlock,
lastBubbleBlockId?: string lastBubbleBlockId?: string
): ExecuteLogicResponse => { ): ExecuteLogicResponse => {
if (!block.options.content) return { outgoingEdgeId: block.outgoingEdgeId } if (!block.options.content) return { outgoingEdgeId: block.outgoingEdgeId }
@@ -27,7 +27,7 @@ export const executeCode = (
outgoingEdgeId: block.outgoingEdgeId, outgoingEdgeId: block.outgoingEdgeId,
clientSideActions: [ clientSideActions: [
{ {
codeToExecute: { scriptToExecute: {
content, content,
args, args,
}, },

View File

@@ -1,4 +1,3 @@
import { executeCode } from '@/features/blocks/logic/code/api'
import { executeCondition } from '@/features/blocks/logic/condition/api' import { executeCondition } from '@/features/blocks/logic/condition/api'
import { executeRedirect } from '@/features/blocks/logic/redirect/api' import { executeRedirect } from '@/features/blocks/logic/redirect/api'
import { executeSetVariable } from '@/features/blocks/logic/setVariable/api' import { executeSetVariable } from '@/features/blocks/logic/setVariable/api'
@@ -6,6 +5,7 @@ import { executeTypebotLink } from '@/features/blocks/logic/typebotLink/api'
import { executeWait } from '@/features/blocks/logic/wait/api/utils/executeWait' import { executeWait } from '@/features/blocks/logic/wait/api/utils/executeWait'
import { LogicBlock, LogicBlockType, SessionState } from 'models' import { LogicBlock, LogicBlockType, SessionState } from 'models'
import { ExecuteLogicResponse } from '../../types' import { ExecuteLogicResponse } from '../../types'
import { executeScript } from '@/features/blocks/logic/script/executeScript'
export const executeLogic = export const executeLogic =
(state: SessionState, lastBubbleBlockId?: string) => (state: SessionState, lastBubbleBlockId?: string) =>
@@ -17,8 +17,8 @@ export const executeLogic =
return executeCondition(state, block) return executeCondition(state, block)
case LogicBlockType.REDIRECT: case LogicBlockType.REDIRECT:
return executeRedirect(state, block, lastBubbleBlockId) return executeRedirect(state, block, lastBubbleBlockId)
case LogicBlockType.CODE: case LogicBlockType.SCRIPT:
return executeCode(state, block, lastBubbleBlockId) return executeScript(state, block, lastBubbleBlockId)
case LogicBlockType.TYPEBOT_LINK: case LogicBlockType.TYPEBOT_LINK:
return executeTypebotLink(state, block) return executeTypebotLink(state, block)
case LogicBlockType.WAIT: case LogicBlockType.WAIT:

View File

@@ -1 +0,0 @@
export { executeCode } from './utils/executeCode'

View File

@@ -2,10 +2,10 @@ import { parseVariables, parseCorrectValueType } from '@/features/variables'
import { LogicState } from '@/types' import { LogicState } from '@/types'
import { sendEventToParent } from '@/utils/chat' import { sendEventToParent } from '@/utils/chat'
import { isEmbedded } from '@/utils/helpers' import { isEmbedded } from '@/utils/helpers'
import { CodeBlock } from 'models' import { ScriptBlock } from 'models'
export const executeCode = async ( export const executeScript = async (
block: CodeBlock, block: ScriptBlock,
{ typebot: { variables } }: LogicState { typebot: { variables } }: LogicState
) => { ) => {
if (!block.options.content) return if (!block.options.content) return

View File

@@ -1,5 +1,4 @@
import { TypebotViewerProps } from '@/components/TypebotViewer' import { TypebotViewerProps } from '@/components/TypebotViewer'
import { executeCode } from '@/features/blocks/logic/code'
import { executeCondition } from '@/features/blocks/logic/condition' import { executeCondition } from '@/features/blocks/logic/condition'
import { executeRedirect } from '@/features/blocks/logic/redirect' import { executeRedirect } from '@/features/blocks/logic/redirect'
import { executeSetVariable } from '@/features/blocks/logic/setVariable' import { executeSetVariable } from '@/features/blocks/logic/setVariable'
@@ -8,6 +7,7 @@ import { executeWait } from '@/features/blocks/logic/wait'
import { LinkedTypebot } from '@/providers/TypebotProvider' import { LinkedTypebot } from '@/providers/TypebotProvider'
import { EdgeId, LogicState } from '@/types' import { EdgeId, LogicState } from '@/types'
import { LogicBlock, LogicBlockType } from 'models' import { LogicBlock, LogicBlockType } from 'models'
import { executeScript } from '@/features/blocks/logic/script/executeScript'
export const executeLogic = async ( export const executeLogic = async (
block: LogicBlock, block: LogicBlock,
@@ -23,8 +23,8 @@ export const executeLogic = async (
return { nextEdgeId: executeCondition(block, context) } return { nextEdgeId: executeCondition(block, context) }
case LogicBlockType.REDIRECT: case LogicBlockType.REDIRECT:
return { nextEdgeId: executeRedirect(block, context) } return { nextEdgeId: executeRedirect(block, context) }
case LogicBlockType.CODE: case LogicBlockType.SCRIPT:
return { nextEdgeId: await executeCode(block, context) } return { nextEdgeId: await executeScript(block, context) }
case LogicBlockType.TYPEBOT_LINK: case LogicBlockType.TYPEBOT_LINK:
return executeTypebotLink(block, context) return executeTypebotLink(block, context)
case LogicBlockType.WAIT: case LogicBlockType.WAIT:

View File

@@ -1,6 +1,8 @@
import { executeCode } from '@/features/blocks/logic/code' import { executeScript } from '@/features/blocks/logic/script/executeScript'
import type { CodeToExecute } from 'models' import type { ScriptToExecute } from 'models'
export const executeChatwoot = (chatwoot: { codeToExecute: CodeToExecute }) => { export const executeChatwoot = (chatwoot: {
executeCode(chatwoot.codeToExecute) scriptToExecute: ScriptToExecute
}) => {
executeScript(chatwoot.scriptToExecute)
} }

View File

@@ -1 +0,0 @@
export * from './utils'

View File

@@ -1 +0,0 @@
export * from './executeCode'

View File

@@ -1,6 +1,6 @@
import type { CodeToExecute } from 'models' import type { ScriptToExecute } from 'models'
export const executeCode = async ({ content, args }: CodeToExecute) => { export const executeScript = async ({ content, args }: ScriptToExecute) => {
const func = Function(...args.map((arg) => arg.id), content) const func = Function(...args.map((arg) => arg.id), content)
try { try {
await func(...args.map((arg) => arg.value)) await func(...args.map((arg) => arg.value))

View File

@@ -1,7 +1,7 @@
import { executeChatwoot } from '@/features/blocks/integrations/chatwoot' import { executeChatwoot } from '@/features/blocks/integrations/chatwoot'
import { executeGoogleAnalyticsBlock } from '@/features/blocks/integrations/googleAnalytics/utils/executeGoogleAnalytics' import { executeGoogleAnalyticsBlock } from '@/features/blocks/integrations/googleAnalytics/utils/executeGoogleAnalytics'
import { executeCode } from '@/features/blocks/logic/code'
import { executeRedirect } from '@/features/blocks/logic/redirect' import { executeRedirect } from '@/features/blocks/logic/redirect'
import { executeScript } from '@/features/blocks/logic/script/executeScript'
import { executeWait } from '@/features/blocks/logic/wait/utils/executeWait' import { executeWait } from '@/features/blocks/logic/wait/utils/executeWait'
import type { ChatReply } from 'models' import type { ChatReply } from 'models'
@@ -14,8 +14,8 @@ export const executeClientSideAction = async (
if ('googleAnalytics' in clientSideAction) { if ('googleAnalytics' in clientSideAction) {
executeGoogleAnalyticsBlock(clientSideAction.googleAnalytics) executeGoogleAnalyticsBlock(clientSideAction.googleAnalytics)
} }
if ('codeToExecute' in clientSideAction) { if ('scriptToExecute' in clientSideAction) {
await executeCode(clientSideAction.codeToExecute) await executeScript(clientSideAction.scriptToExecute)
} }
if ('redirect' in clientSideAction) { if ('redirect' in clientSideAction) {
executeRedirect(clientSideAction.redirect) executeRedirect(clientSideAction.redirect)

View File

@@ -1,21 +0,0 @@
import { z } from 'zod'
import { blockBaseSchema } from '../baseSchemas'
import { LogicBlockType } from './enums'
export const codeOptionsSchema = z.object({
name: z.string(),
content: z.string().optional(),
shouldExecuteInParentContext: z.boolean().optional(),
})
export const codeBlockSchema = blockBaseSchema.and(
z.object({
type: z.enum([LogicBlockType.CODE]),
options: codeOptionsSchema,
})
)
export const defaultCodeOptions: CodeOptions = { name: 'Code snippet' }
export type CodeBlock = z.infer<typeof codeBlockSchema>
export type CodeOptions = z.infer<typeof codeOptionsSchema>

View File

@@ -2,7 +2,7 @@ export enum LogicBlockType {
SET_VARIABLE = 'Set variable', SET_VARIABLE = 'Set variable',
CONDITION = 'Condition', CONDITION = 'Condition',
REDIRECT = 'Redirect', REDIRECT = 'Redirect',
CODE = 'Code', SCRIPT = 'Code',
TYPEBOT_LINK = 'Typebot link', TYPEBOT_LINK = 'Typebot link',
WAIT = 'Wait', WAIT = 'Wait',
} }

View File

@@ -1,4 +1,4 @@
export * from './code' export * from './script'
export * from './condition' export * from './condition'
export * from './enums' export * from './enums'
export * from './logicBlock' export * from './logicBlock'

View File

@@ -1,18 +1,18 @@
import { z } from 'zod' import { z } from 'zod'
import { codeOptionsSchema, codeBlockSchema } from './code' import { scriptOptionsSchema, scriptBlockSchema } from './script'
import { conditionBlockSchema } from './condition' import { conditionBlockSchema } from './condition'
import { redirectOptionsSchema, redirectBlockSchema } from './redirect' import { redirectOptionsSchema, redirectBlockSchema } from './redirect'
import { setVariableOptionsSchema, setVariableBlockSchema } from './setVariable' import { setVariableOptionsSchema, setVariableBlockSchema } from './setVariable'
import { typebotLinkOptionsSchema, typebotLinkBlockSchema } from './typebotLink' import { typebotLinkOptionsSchema, typebotLinkBlockSchema } from './typebotLink'
import { waitBlockSchema, waitOptionsSchema } from './wait' import { waitBlockSchema, waitOptionsSchema } from './wait'
const logicBlockOptionsSchema = codeOptionsSchema const logicBlockOptionsSchema = scriptOptionsSchema
.or(redirectOptionsSchema) .or(redirectOptionsSchema)
.or(setVariableOptionsSchema) .or(setVariableOptionsSchema)
.or(typebotLinkOptionsSchema) .or(typebotLinkOptionsSchema)
.or(waitOptionsSchema) .or(waitOptionsSchema)
export const logicBlockSchema = codeBlockSchema export const logicBlockSchema = scriptBlockSchema
.or(conditionBlockSchema) .or(conditionBlockSchema)
.or(redirectBlockSchema) .or(redirectBlockSchema)
.or(typebotLinkBlockSchema) .or(typebotLinkBlockSchema)

View File

@@ -0,0 +1,21 @@
import { z } from 'zod'
import { blockBaseSchema } from '../baseSchemas'
import { LogicBlockType } from './enums'
export const scriptOptionsSchema = z.object({
name: z.string(),
content: z.string().optional(),
shouldExecuteInParentContext: z.boolean().optional(),
})
export const scriptBlockSchema = blockBaseSchema.and(
z.object({
type: z.enum([LogicBlockType.SCRIPT]),
options: scriptOptionsSchema,
})
)
export const defaultScriptOptions: ScriptOptions = { name: 'Script' }
export type ScriptBlock = z.infer<typeof scriptBlockSchema>
export type ScriptOptions = z.infer<typeof scriptOptionsSchema>

View File

@@ -97,7 +97,7 @@ const chatMessageSchema = z
.or(embedMessageSchema) .or(embedMessageSchema)
) )
const codeToExecuteSchema = z.object({ const scriptToExecuteSchema = z.object({
content: z.string(), content: z.string(),
args: z.array( args: z.array(
z.object({ z.object({
@@ -176,7 +176,7 @@ const clientSideActionSchema = z
.and( .and(
z z
.object({ .object({
codeToExecute: codeToExecuteSchema, scriptToExecute: scriptToExecuteSchema,
}) })
.or( .or(
z.object({ z.object({
@@ -185,7 +185,7 @@ const clientSideActionSchema = z
) )
.or( .or(
z.object({ z.object({
chatwoot: z.object({ codeToExecute: codeToExecuteSchema }), chatwoot: z.object({ scriptToExecute: scriptToExecuteSchema }),
}) })
) )
.or( .or(
@@ -228,7 +228,7 @@ export type TypebotInSession = z.infer<typeof typebotInSessionStateSchema>
export type ChatReply = z.infer<typeof chatReplySchema> export type ChatReply = z.infer<typeof chatReplySchema>
export type ChatMessage = z.infer<typeof chatMessageSchema> export type ChatMessage = z.infer<typeof chatMessageSchema>
export type SendMessageInput = z.infer<typeof sendMessageInputSchema> export type SendMessageInput = z.infer<typeof sendMessageInputSchema>
export type CodeToExecute = z.infer<typeof codeToExecuteSchema> export type ScriptToExecute = z.infer<typeof scriptToExecuteSchema>
export type StartParams = z.infer<typeof startParamsSchema> export type StartParams = z.infer<typeof startParamsSchema>
export type RuntimeOptions = z.infer<typeof runtimeOptionsSchema> export type RuntimeOptions = z.infer<typeof runtimeOptionsSchema>
export type StartTypebot = z.infer<typeof startTypebotSchema> export type StartTypebot = z.infer<typeof startTypebotSchema>