diff --git a/apps/builder/src/features/blocks/inputs/buttons/components/ButtonsItemSettings.tsx b/apps/builder/src/features/blocks/inputs/buttons/components/ButtonsItemSettings.tsx index 33bffec97..a1d0812a9 100644 --- a/apps/builder/src/features/blocks/inputs/buttons/components/ButtonsItemSettings.tsx +++ b/apps/builder/src/features/blocks/inputs/buttons/components/ButtonsItemSettings.tsx @@ -32,6 +32,7 @@ export const ButtonsItemSettings = ({ item, onSettingsChange }: Props) => { diff --git a/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts b/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts index f022f785d..c72eabbcf 100644 --- a/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts +++ b/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts @@ -1,4 +1,5 @@ import { findUniqueVariableValue } from '@/features/variables/findUniqueVariableValue' +import { parseVariables } from '@/features/variables/parseVariables' import { isNotDefined, isDefined } from '@typebot.io/lib' import { Comparison, @@ -7,7 +8,6 @@ import { LogicalOperator, Variable, } from '@typebot.io/schemas' -import { parseVariables } from 'bot-engine' export const executeCondition = (variables: Variable[]) => @@ -20,8 +20,9 @@ const executeComparison = (variables: Variable[]) => (comparison: Comparison): boolean => { if (!comparison?.variableId) return false - const inputValue = - variables.find((v) => v.id === comparison.variableId)?.value ?? '' + const inputValue = variables.find( + (v) => v.id === comparison.variableId + )?.value const value = findUniqueVariableValue(variables)(comparison.value) ?? parseVariables(variables)(comparison.value) @@ -37,18 +38,19 @@ const executeComparison = } case ComparisonOperators.NOT_CONTAINS: { const notContains = (a: string | null, b: string | null) => { - if (b === '' || !b || !a) return false + if (b === '' || !b || !a) return true return !a.toLowerCase().trim().includes(b.toLowerCase().trim()) } - return compare(notContains, inputValue, value) + return compare(notContains, inputValue, value, true) } case ComparisonOperators.EQUAL: { return compare((a, b) => a === b, inputValue, value) } case ComparisonOperators.NOT_EQUAL: { - return compare((a, b) => a !== b, inputValue, value) + return compare((a, b) => a !== b, inputValue, value, true) } case ComparisonOperators.GREATER: { + if (isNotDefined(inputValue)) return false if (typeof inputValue === 'string') { if (typeof value === 'string') return parseFloat(inputValue) > parseFloat(value) @@ -59,6 +61,7 @@ const executeComparison = return inputValue.length > value.length } case ComparisonOperators.LESS: { + if (isNotDefined(inputValue)) return false if (typeof inputValue === 'string') { if (typeof value === 'string') return parseFloat(inputValue) < parseFloat(value) @@ -92,15 +95,24 @@ const executeComparison = } const compare = ( - func: (a: string | null, b: string | null) => boolean, + compareStrings: (a: string | null, b: string | null) => boolean, a: Variable['value'], - b: Variable['value'] + b: Variable['value'], + defaultReturnValue = false ): boolean => { - if (!a || !b) return false + if (!a || !b) return defaultReturnValue if (typeof a === 'string') { - if (typeof b === 'string') return func(a, b) - return b.some((b) => func(a, b)) + if (typeof b === 'string') return compareStrings(a, b) + return defaultReturnValue === true + ? b.every((b) => compareStrings(a, b)) + : b.some((b) => compareStrings(a, b)) } - if (typeof b === 'string') return a.some((a) => func(a, b)) - return a.some((a) => b.some((b) => func(a, b))) + if (typeof b === 'string') { + return defaultReturnValue === true + ? a.every((a) => compareStrings(a, b)) + : a.some((a) => compareStrings(a, b)) + } + if (defaultReturnValue === true) + return a.every((a) => b.every((b) => compareStrings(a, b))) + return a.some((a) => b.some((b) => compareStrings(a, b))) }