Skip to content

Commit

Permalink
fix(engine): 💄 Scroll when payment form appears
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 1, 2022
1 parent 4f208a2 commit caa6015
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/bot-engine/src/components/ChatBlock/ChatBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import {
import { HostBubble } from './ChatStep/bubbles/HostBubble'
import { InputChatStep } from './ChatStep/InputChatStep'
import { getLastChatStepType } from '../../services/chat'
import { useChat } from 'contexts/ChatContext'

type ChatBlockProps = {
steps: Step[]
startStepIndex: number
blockTitle: string
keepShowingHostAvatar: boolean
onScroll: () => void
onBlockEnd: (
edgeId?: string,
updatedTypebot?: PublicTypebot | LinkedTypebot
Expand All @@ -45,7 +45,6 @@ export const ChatBlock = ({
steps,
startStepIndex,
blockTitle,
onScroll,
onBlockEnd,
keepShowingHostAvatar,
}: ChatBlockProps) => {
Expand All @@ -63,6 +62,7 @@ export const ChatBlock = ({
pushEdgeIdInLinkedTypebotQueue,
} = useTypebot()
const { resultValues, updateVariables, resultId } = useAnswers()
const { scroll } = useChat()
const [processedSteps, setProcessedSteps] = useState<Step[]>([])
const [displayedChunks, setDisplayedChunks] = useState<ChatDisplayChunk[]>([])

Expand Down Expand Up @@ -102,7 +102,7 @@ export const ChatBlock = ({
}, [])

useEffect(() => {
onScroll()
scroll()
onNewStepDisplayed()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [processedSteps])
Expand Down Expand Up @@ -155,7 +155,7 @@ export const ChatBlock = ({
}

const displayNextStep = (answerContent?: string, isRetry?: boolean) => {
onScroll()
scroll()
const currentStep = [...processedSteps].pop()
if (currentStep) {
if (isRetry && stepCanBeRetried(currentStep))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SendButton, Spinner } from '../SendButton'
import { useFrame } from 'react-frame-component'
import { initStripe } from '../../../../../../lib/stripe'
import { parseVariables } from 'services/variable'
import { useChat } from 'contexts/ChatContext'

type Props = {
options: PaymentInputOptions
Expand Down Expand Up @@ -80,6 +81,7 @@ const CheckoutForm = ({
variables: Variable[]
viewerHost: string
}) => {
const { scroll } = useChat()
const [ignoreFirstPaymentIntentCall, setIgnoreFirstPaymentIntentCall] =
useState(true)

Expand Down Expand Up @@ -154,7 +156,10 @@ const CheckoutForm = ({
setMessage('An unexpected error occured.')
}

const showPayButton = () => setIsPayButtonVisible(true)
const showPayButton = () => {
setIsPayButtonVisible(true)
scroll()
}

return (
<form
Expand Down
25 changes: 14 additions & 11 deletions packages/bot-engine/src/components/ConversationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Block, Edge, PublicTypebot, Theme, VariableWithValue } from 'models'
import { byId, isNotDefined } from 'utils'
import { animateScroll as scroll } from 'react-scroll'
import { LinkedTypebot, useTypebot } from 'contexts/TypebotContext'
import { ChatContext } from 'contexts/ChatContext'

type Props = {
theme: Theme
Expand Down Expand Up @@ -104,17 +105,19 @@ export const ConversationContainer = ({
ref={scrollableContainer}
className="overflow-y-scroll w-full lg:w-3/4 min-h-full rounded lg:px-5 px-3 pt-10 relative scrollable-container typebot-chat-view"
>
{displayedBlocks.map((displayedBlock, idx) => (
<ChatBlock
key={displayedBlock.block.id + idx}
steps={displayedBlock.block.steps}
startStepIndex={displayedBlock.startStepIndex}
onScroll={autoScrollToBottom}
onBlockEnd={displayNextBlock}
blockTitle={displayedBlock.block.title}
keepShowingHostAvatar={idx === displayedBlocks.length - 1}
/>
))}
<ChatContext onScroll={autoScrollToBottom}>
{displayedBlocks.map((displayedBlock, idx) => (
<ChatBlock
key={displayedBlock.block.id + idx}
steps={displayedBlock.block.steps}
startStepIndex={displayedBlock.startStepIndex}
onBlockEnd={displayNextBlock}
blockTitle={displayedBlock.block.title}
keepShowingHostAvatar={idx === displayedBlocks.length - 1}
/>
))}
</ChatContext>

{/* We use a block to simulate padding because it makes iOS scroll flicker */}
<div className="w-full h-32" ref={bottomAnchor} />
</div>
Expand Down
28 changes: 28 additions & 0 deletions packages/bot-engine/src/contexts/ChatContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { createContext, ReactNode, useContext } from 'react'

const chatContext = createContext<{
scroll: () => void
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
}>({})

export const ChatContext = ({
children,
onScroll,
}: {
children: ReactNode
onScroll: () => void
}) => {
const scroll = onScroll
return (
<chatContext.Provider
value={{
scroll,
}}
>
{children}
</chatContext.Provider>
)
}

export const useChat = () => useContext(chatContext)

4 comments on commit caa6015

@vercel
Copy link

@vercel vercel bot commented on caa6015 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on caa6015 Jun 1, 2022

@vercel
Copy link

@vercel vercel bot commented on caa6015 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on caa6015 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app

Please sign in to comment.