2
0

feat(viewer): Add variables in URL support

This commit is contained in:
Baptiste Arnaud
2022-02-17 10:46:04 +01:00
parent f7d6f0b766
commit 6e0ab67502
15 changed files with 550 additions and 58 deletions

View File

@ -22,7 +22,7 @@ export const ConversationContainer = ({
onNewAnswer,
onCompleted,
}: Props) => {
const { typebot } = useTypebot()
const { typebot, updateVariableValue } = useTypebot()
const { document: frameDocument } = useFrame()
const [displayedBlocks, setDisplayedBlocks] = useState<
{ block: PublicBlock; startStepIndex: number }[]
@ -48,10 +48,21 @@ export const ConversationContainer = ({
}
useEffect(() => {
injectUrlParamsIntoVariables()
displayNextBlock(typebot.blocks[0].steps[0].outgoingEdgeId)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const injectUrlParamsIntoVariables = () => {
const urlParams = new URLSearchParams(location.search)
urlParams.forEach((value, key) => {
const matchingVariable = typebot.variables.find(
(v) => v.name.toLowerCase() === key.toLowerCase()
)
if (matchingVariable) updateVariableValue(matchingVariable?.id, value)
})
}
useEffect(() => {
setCssVariablesValue(theme, frameDocument.body.style)
}, [theme, frameDocument])