2
0

feat(theme): Custom avatars

This commit is contained in:
Baptiste Arnaud
2022-02-16 15:08:50 +01:00
parent 1d3917f440
commit d2ac13ba5f
14 changed files with 294 additions and 81 deletions

View File

@@ -0,0 +1,38 @@
import { Icon, IconProps } from '@chakra-ui/react'
import React from 'react'
export const DefaultAvatar = (props: IconProps) => {
return (
<Icon
viewBox="0 0 75 75"
fill="none"
xmlns="http://www.w3.org/2000/svg"
boxSize="40px"
data-testid="default-avatar"
{...props}
>
<mask id="mask0" x="0" y="0" mask-type="alpha">
<circle cx="37.5" cy="37.5" r="37.5" fill="#0042DA" />
</mask>
<g mask="url(#mask0)">
<rect x="-30" y="-43" width="131" height="154" fill="#0042DA" />
<rect
x="2.50413"
y="120.333"
width="81.5597"
height="86.4577"
rx="2.5"
transform="rotate(-52.6423 2.50413 120.333)"
stroke="#FED23D"
strokeWidth="5"
/>
<circle cx="76.5" cy="-1.5" r="29" stroke="#FF8E20" strokeWidth="5" />
<path
d="M-49.8224 22L-15.5 -40.7879L18.8224 22H-49.8224Z"
stroke="#F7F8FF"
strokeWidth="5"
/>
</g>
</Icon>
)
}

View File

@@ -1,10 +1,11 @@
import { ChangeEvent, useEffect, useState } from 'react'
import { Button, Flex, HStack, Input, Stack, Text } from '@chakra-ui/react'
import { useEffect, useState } from 'react'
import { Button, Flex, HStack, Stack, Text } from '@chakra-ui/react'
import { SearchContextManager } from '@giphy/react-components'
import { UploadButton } from '../buttons/UploadButton'
import { GiphySearch } from './GiphySearch'
import { useTypebot } from 'contexts/TypebotContext'
import { useDebounce } from 'use-debounce'
import { InputWithVariableButton } from '../TextboxWithVariableButton'
type Props = {
url?: string
@@ -102,16 +103,12 @@ const EmbedLinkContent = ({ initialUrl, onNewUrl }: ContentProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedImageUrl])
const handleImageUrlChange = (e: ChangeEvent<HTMLInputElement>) =>
setImageUrl(e.target.value)
return (
<Stack py="2">
<Input
my="2"
<InputWithVariableButton
placeholder={'Paste the image link...'}
onChange={handleImageUrlChange}
value={imageUrl}
onChange={setImageUrl}
initialValue={imageUrl}
/>
</Stack>
)

View File

@@ -0,0 +1,82 @@
import React from 'react'
import { AvatarProps } from 'models'
import {
Heading,
HStack,
Popover,
PopoverContent,
PopoverTrigger,
Stack,
Switch,
Image,
Flex,
Box,
Portal,
} from '@chakra-ui/react'
import { ImageUploadContent } from 'components/shared/ImageUploadContent'
import { DefaultAvatar } from 'assets/DefaultAvatar'
type Props = {
title: string
avatarProps?: AvatarProps
isDefaultCheck?: boolean
onAvatarChange: (avatarProps: AvatarProps) => void
}
export const AvatarForm = ({
title,
avatarProps,
isDefaultCheck = false,
onAvatarChange,
}: Props) => {
const isChecked = avatarProps ? avatarProps.isEnabled : isDefaultCheck
const handleOnCheck = () =>
onAvatarChange({ ...avatarProps, isEnabled: !isChecked })
const handleImageUrl = (url: string) =>
onAvatarChange({ isEnabled: isChecked, url })
return (
<Stack borderWidth={1} rounded="md" p="4" spacing={4}>
<Flex justifyContent="space-between">
<HStack>
<Heading as="label" fontSize="lg" htmlFor={title} mb="1">
{title}
</Heading>
<Switch isChecked={isChecked} id={title} onChange={handleOnCheck} />
</HStack>
{isChecked && (
<Popover isLazy>
<PopoverTrigger>
{avatarProps?.url ? (
<Image
src={avatarProps.url}
alt="Website image"
cursor="pointer"
_hover={{ filter: 'brightness(.9)' }}
transition="filter 200ms"
rounded="full"
boxSize="40px"
objectFit="cover"
/>
) : (
<Box>
<DefaultAvatar
cursor="pointer"
_hover={{ filter: 'brightness(.9)' }}
/>
</Box>
)}
</PopoverTrigger>
<Portal>
<PopoverContent p="4">
<ImageUploadContent
url={avatarProps?.url}
onSubmit={handleImageUrl}
/>
</PopoverContent>
</Portal>
</Popover>
)}
</Flex>
</Stack>
)
}

View File

@@ -1,6 +1,7 @@
import { Heading, Stack } from '@chakra-ui/react'
import { ChatTheme, ContainerColors, InputColors } from 'models'
import { AvatarProps, ChatTheme, ContainerColors, InputColors } from 'models'
import React from 'react'
import { AvatarForm } from './AvatarForm'
import { ButtonsTheme } from './ButtonsTheme'
import { GuestBubbles } from './GuestBubbles'
import { HostBubbles } from './HostBubbles'
@@ -21,8 +22,24 @@ export const ChatThemeSettings = ({ chatTheme, onChatThemeChange }: Props) => {
const handleInputsChange = (inputs: InputColors) =>
onChatThemeChange({ ...chatTheme, inputs })
const handleHostAvatarChange = (hostAvatar: AvatarProps) =>
onChatThemeChange({ ...chatTheme, hostAvatar })
const handleGuestAvatarChange = (guestAvatar: AvatarProps) =>
onChatThemeChange({ ...chatTheme, guestAvatar })
return (
<Stack spacing={6}>
<AvatarForm
title="Bot avatar"
avatarProps={chatTheme.hostAvatar}
isDefaultCheck
onAvatarChange={handleHostAvatarChange}
/>
<AvatarForm
title="User avatar"
avatarProps={chatTheme.guestAvatar}
onAvatarChange={handleGuestAvatarChange}
/>
<Stack borderWidth={1} rounded="md" p="4" spacing={4}>
<Heading fontSize="lg">Bot bubbles</Heading>
<HostBubbles

View File

@@ -4,6 +4,11 @@ import { generate } from 'short-uuid'
import { importTypebotInDatabase } from '../services/database'
import { typebotViewer } from '../services/selectorUtils'
const hostAvatarUrl =
'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1760&q=80'
const guestAvatarUrl =
'https://images.unsplash.com/photo-1494790108377-be9c29b29330?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1287&q=80'
test.describe.parallel('Theme page', () => {
test.describe('General', () => {
test('should reflect change in real-time', async ({ page }) => {
@@ -40,7 +45,7 @@ test.describe.parallel('Theme page', () => {
})
test.describe('Chat', () => {
test('should reflect change in real-time', async ({ page }) => {
test.only('should reflect change in real-time', async ({ page }) => {
const typebotId = 'chat-theme-typebot'
try {
await importTypebotInDatabase(
@@ -53,7 +58,23 @@ test.describe.parallel('Theme page', () => {
await page.goto(`/typebots/${typebotId}/theme`)
await page.click('button:has-text("Chat")')
await page.waitForTimeout(300)
// Host avatar
await expect(
typebotViewer(page).locator('[data-testid="default-avatar"]')
).toBeVisible()
await page.click('[data-testid="default-avatar"]')
await page.click('button:has-text("Embed link")')
await page.fill(
'input[placeholder="Paste the image link..."]',
hostAvatarUrl
)
await expect(typebotViewer(page).locator('img')).toHaveAttribute(
'src',
hostAvatarUrl
)
await page.click('text=Bot avatar')
await expect(typebotViewer(page).locator('img')).toBeHidden()
// Host bubbles
await page.click(
@@ -105,6 +126,22 @@ test.describe.parallel('Theme page', () => {
)
await expect(guestBubble).toHaveCSS('color', 'rgb(38, 70, 83)')
// Guest avatar
await page.click('text=User avatar')
await expect(
typebotViewer(page).locator('[data-testid="default-avatar"]')
).toBeVisible()
await page.click('[data-testid="default-avatar"]')
await page.click('button:has-text("Embed link")')
await page.fill(
'input[placeholder="Paste the image link..."]',
guestAvatarUrl
)
await expect(typebotViewer(page).locator('img')).toHaveAttribute(
'src',
guestAvatarUrl
)
// Input
await page.click(
'[data-testid="inputs-theme"] >> [aria-label="Pick a color"] >> nth=0'