2
0

(setVariable) Add Transcription system var (#1507)

Closes #1484
This commit is contained in:
Baptiste Arnaud
2024-05-15 14:24:55 +02:00
committed by GitHub
parent ec7ff8d9ca
commit 40f21203b5
102 changed files with 2911 additions and 986 deletions

View File

@ -4,6 +4,7 @@
"license": "AGPL-3.0-or-later",
"private": true,
"dependencies": {
"@typebot.io/lib": "workspace:*"
"@typebot.io/lib": "workspace:*",
"@typebot.io/tsconfig": "workspace:*"
}
}

View File

@ -95,6 +95,7 @@ type VariableToParseInformation = {
endIndex: number
textToReplace: string
value: string
variableId?: string
}
export const getVariablesToParseInfoInText = (
@ -146,6 +147,7 @@ export const getVariablesToParseInfoInText = (
? variable?.value[variable?.value.length - 1]
: variable?.value
) ?? '',
variableId: variable?.id,
})
})
return variablesToParseInfo.sort((a, b) => a.startIndex - b.startIndex)

View File

@ -2,12 +2,13 @@ export type Variable = {
id: string
name: string
value?: string | (string | null)[] | null | undefined
isSessionVariable?: boolean
}
export type VariableWithValue = Pick<Variable, 'id' | 'name'> & {
export type VariableWithValue = Omit<Variable, 'value'> & {
value: string | (string | null)[]
}
export type VariableWithUnknowValue = Pick<Variable, 'id' | 'name'> & {
export type VariableWithUnknowValue = Omit<Variable, 'value'> & {
value?: unknown
}

View File

@ -1,41 +1,102 @@
import { safeStringify } from '@typebot.io/lib/safeStringify'
import { Variable, VariableWithUnknowValue } from './types'
import { SessionState, SetVariableHistoryItem } from '../schemas'
export const updateVariablesInSession =
(state: any) => (newVariables: VariableWithUnknowValue[]) => ({
...state,
typebotsQueue: state.typebotsQueue.map(
(typebotInQueue: { typebot: { variables: Variable[] } }, index: number) =>
type Props = {
state: SessionState
newVariables: VariableWithUnknowValue[]
currentBlockId: string | undefined
}
export const updateVariablesInSession = ({
state,
newVariables,
currentBlockId,
}: Props): {
updatedState: SessionState
newSetVariableHistory: SetVariableHistoryItem[]
} => {
const { updatedVariables, newSetVariableHistory, setVariableHistoryIndex } =
updateTypebotVariables({
state,
newVariables,
currentBlockId,
})
return {
updatedState: {
...state,
currentSetVariableHistoryIndex: setVariableHistoryIndex,
typebotsQueue: state.typebotsQueue.map((typebotInQueue, index: number) =>
index === 0
? {
...typebotInQueue,
typebot: {
...typebotInQueue.typebot,
variables: updateTypebotVariables(typebotInQueue.typebot)(
newVariables
),
variables: updatedVariables,
},
}
: typebotInQueue
),
})
),
previewMetadata: state.typebotsQueue[0].resultId
? state.previewMetadata
: {
...state.previewMetadata,
setVariableHistory: (
state.previewMetadata?.setVariableHistory ?? []
).concat(newSetVariableHistory),
},
},
newSetVariableHistory,
}
}
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),
}))
const updateTypebotVariables = ({
state,
newVariables,
currentBlockId,
}: {
state: SessionState
newVariables: VariableWithUnknowValue[]
currentBlockId: string | undefined
}): {
updatedVariables: Variable[]
newSetVariableHistory: SetVariableHistoryItem[]
setVariableHistoryIndex: number
} => {
const serializedNewVariables = newVariables.map((variable) => ({
...variable,
value: Array.isArray(variable.value)
? variable.value.map(safeStringify)
: safeStringify(variable.value),
}))
return [
...typebot.variables.filter((existingVariable) =>
let setVariableHistoryIndex = state.currentSetVariableHistoryIndex ?? 0
const setVariableHistory: SetVariableHistoryItem[] = []
if (currentBlockId) {
serializedNewVariables
.filter((v) => state.setVariableIdsForHistory?.includes(v.id))
.forEach((newVariable) => {
setVariableHistory.push({
resultId: state.typebotsQueue[0].resultId as string,
index: setVariableHistoryIndex,
blockId: currentBlockId,
variableId: newVariable.id,
value: newVariable.value,
})
setVariableHistoryIndex += 1
})
}
return {
updatedVariables: [
...state.typebotsQueue[0].typebot.variables.filter((existingVariable) =>
serializedNewVariables.every(
(newVariable) => existingVariable.id !== newVariable.id
)
),
...serializedNewVariables,
]
],
newSetVariableHistory: setVariableHistory,
setVariableHistoryIndex,
}
}