Skip to content

Commit

Permalink
fix(engine): 🐛 Properly handle variable avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 25, 2022
1 parent 74b3464 commit d21b172
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 33 deletions.
18 changes: 10 additions & 8 deletions apps/builder/components/theme/ChatSettings/AvatarForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const AvatarForm = ({
onAvatarChange({ ...avatarProps, isEnabled: !isChecked })
const handleImageUrl = (url: string) =>
onAvatarChange({ isEnabled: isChecked, url })

const isDefaultAvatar = !avatarProps?.url || avatarProps.url.includes('{{')
return (
<Stack borderWidth={1} rounded="md" p="4" spacing={4}>
<Flex justifyContent="space-between">
Expand All @@ -46,7 +48,14 @@ export const AvatarForm = ({
{isChecked && (
<Popover isLazy>
<PopoverTrigger>
{avatarProps?.url ? (
{isDefaultAvatar ? (
<Box>
<DefaultAvatar
cursor="pointer"
_hover={{ filter: 'brightness(.9)' }}
/>
</Box>
) : (
<Image
src={avatarProps.url}
alt="Website image"
Expand All @@ -57,13 +66,6 @@ export const AvatarForm = ({
boxSize="40px"
objectFit="cover"
/>
) : (
<Box>
<DefaultAvatar
cursor="pointer"
_hover={{ filter: 'brightness(.9)' }}
/>
</Box>
)}
</PopoverTrigger>
<Portal>
Expand Down
1 change: 1 addition & 0 deletions packages/bot-engine/src/assets/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const SendIcon = (props: React.SVGProps<SVGSVGElement>) => (
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"
width="19px"
color="white"
{...props}
>
<path d="M476.59 227.05l-.16-.07L49.35 49.84A23.56 23.56 0 0027.14 52 24.65 24.65 0 0016 72.59v113.29a24 24 0 0019.52 23.57l232.93 43.07a4 4 0 010 7.86L35.53 303.45A24 24 0 0016 327v113.31A23.57 23.57 0 0026.59 460a23.94 23.94 0 0013.22 4 24.55 24.55 0 009.52-1.93L476.4 285.94l.19-.09a32 32 0 000-58.8z" />
Expand Down
2 changes: 1 addition & 1 deletion packages/bot-engine/src/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ textarea {
}

.typebot-button > .send-icon {
fill: var(--typebot-button-active-color);
fill: var(--typebot-button-color);
}

.typebot-chat-view {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useHostAvatars } from '../../contexts/HostAvatarsContext'
export const AvatarSideContainer = ({
hostAvatarSrc,
}: {
hostAvatarSrc: string
hostAvatarSrc?: string
}) => {
const { lastBubblesTopOffset } = useHostAvatars()
const { window, document } = useFrame()
Expand Down
11 changes: 6 additions & 5 deletions packages/bot-engine/src/components/ChatBlock/ChatBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,18 @@ export const ChatBlock = ({
if (nextStep) setDisplayedSteps([...displayedSteps, nextStep])
}

const avatarSrc = typebot.theme.chat.hostAvatar?.url
return (
<div className="flex">
<div className="flex w-full">
<HostAvatarsContext>
{(typebot.theme.chat.hostAvatar?.isEnabled ?? true) && (
<AvatarSideContainer
hostAvatarSrc={parseVariables(typebot.variables)(
typebot.theme.chat.hostAvatar?.url
)}
hostAvatarSrc={
avatarSrc && parseVariables(typebot.variables)(avatarSrc)
}
/>
)}
<div className="flex flex-col w-full">
<div className="flex flex-col w-full min-w-0">
<TransitionGroup>
{displayedSteps
.filter((step) => isInputStep(step) || isBubbleStep(step))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,12 @@ const InputChatStep = ({
}

if (answer) {
const avatarUrl = typebot.theme.chat.guestAvatar?.url
return (
<GuestBubble
message={answer}
showAvatar={typebot.theme.chat.guestAvatar?.isEnabled ?? false}
avatarSrc={parseVariables(typebot.variables)(
typebot.theme.chat.guestAvatar?.url
)}
avatarSrc={avatarUrl && parseVariables(typebot.variables)(avatarUrl)}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CSSTransition } from 'react-transition-group'
interface Props {
message: string
showAvatar: boolean
avatarSrc: string
avatarSrc?: string
}

export const GuestBubble = ({
Expand All @@ -15,16 +15,14 @@ export const GuestBubble = ({
}: Props): JSX.Element => {
return (
<CSSTransition classNames="bubble" timeout={1000}>
<div className="flex justify-end mb-2 items-center">
<div className="flex items-end w-11/12 lg:w-4/6 justify-end">
<div
className="inline-flex px-4 py-2 rounded-lg mr-2 whitespace-pre-wrap max-w-full typebot-guest-bubble"
data-testid="guest-bubble"
>
{message}
</div>
{showAvatar && <Avatar avatarSrc={avatarSrc} />}
</div>
<div className="flex justify-end mb-2 items-end">
<span
className="px-4 py-2 rounded-lg mr-2 whitespace-pre-wrap max-w-full typebot-guest-bubble"
data-testid="guest-bubble"
>
{message}
</span>
{showAvatar && <Avatar avatarSrc={avatarSrc} />}
</div>
</CSSTransition>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const TextBubble = ({ step, onTransitionEnd }: Props) => {

return (
<div className="flex flex-col" ref={messageContainer}>
<div className="flex mb-2 w-full lg:w-11/12 items-center">
<div className="flex mb-2 w-full items-center">
<div className={'flex relative z-10 items-start typebot-host-bubble'}>
<div
className="flex items-center absolute px-4 py-2 rounded-lg bubble-typing z-10 "
Expand Down
8 changes: 5 additions & 3 deletions packages/bot-engine/src/components/avatars/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react'
import { isDefined } from 'utils'
import { DefaultAvatar } from './DefaultAvatar'

export const Avatar = ({ avatarSrc }: { avatarSrc: string }): JSX.Element => {
export const Avatar = ({ avatarSrc }: { avatarSrc?: string }): JSX.Element => {
if (avatarSrc === '') return <></>
return (
<div className="w-full h-full rounded-full text-2xl md:text-4xl text-center xs:w-10 xs:h-10">
{avatarSrc !== '' ? (
<div className="w-full h-full rounded-full text-2xl md:text-4xl text-center xs:w-10 xs:h-10 flex-shrink-0">
{isDefined(avatarSrc) ? (
<figure
className={
'flex justify-center items-center rounded-full text-white w-6 h-6 text-sm relative xs:w-full xs:h-full xs:text-xl'
Expand Down

0 comments on commit d21b172

Please sign in to comment.