2
0
Files
bot/packages/variables/updateVariablesInSession.ts
Baptiste Arnaud 5e019bbb22 Introducing The Forge (#1072)
The Forge allows anyone to easily create their own Typebot Block.

Closes #380
2023-12-13 10:22:02 +01:00

46 lines
1.2 KiB
TypeScript

import { safeStringify } from '@typebot.io/lib/safeStringify'
import {
SessionState,
VariableWithUnknowValue,
Variable,
} from '@typebot.io/schemas'
export const updateVariablesInSession =
(state: SessionState) =>
(newVariables: VariableWithUnknowValue[]): SessionState => ({
...state,
typebotsQueue: state.typebotsQueue.map((typebotInQueue, index) =>
index === 0
? {
...typebotInQueue,
typebot: {
...typebotInQueue.typebot,
variables: updateTypebotVariables(typebotInQueue.typebot)(
newVariables
),
},
}
: typebotInQueue
),
})
const updateTypebotVariables =
(typebot: { variables: Variable[] }) =>
(newVariables: VariableWithUnknowValue[]): Variable[] => {
const serializedNewVariables = newVariables.map((variable) => ({
...variable,
value: Array.isArray(variable.value)
? variable.value.map(safeStringify)
: safeStringify(variable.value),
}))
return [
...typebot.variables.filter((existingVariable) =>
serializedNewVariables.every(
(newVariable) => existingVariable.id !== newVariable.id
)
),
...serializedNewVariables,
]
}