Skip to content

Commit

Permalink
refactor(botonic-react): split in diferents files Header component an…
Browse files Browse the repository at this point in the history
… use typescript
  • Loading branch information
Iru89 committed Dec 18, 2024
1 parent d11ba35 commit 32c3a71
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 149 deletions.
149 changes: 0 additions & 149 deletions packages/botonic-react/src/webchat/header.jsx

This file was deleted.

75 changes: 75 additions & 0 deletions packages/botonic-react/src/webchat/header/default-header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, { useContext } from 'react'

import { COLORS, ROLES, WEBCHAT } from '../../constants'
import { WebchatContext } from '../../contexts'
import { Scale } from '../../shared/styles'
import { resolveImage } from '../../util/environment'
import { ConditionalWrapper } from '../../util/react'
import {
CloseHeader,
HeaderContainer,
ImageContainer,
Subtitle,
TextContainer,
Title,
} from './styles'

export const DefaultHeader = () => {
const { getThemeProperty, toggleWebchat } = useContext(WebchatContext)

const animationsEnabled = getThemeProperty(
WEBCHAT.CUSTOM_PROPERTIES.enableAnimations,
true
)

const headerImage = getThemeProperty(
WEBCHAT.CUSTOM_PROPERTIES.headerImage,
getThemeProperty(
WEBCHAT.CUSTOM_PROPERTIES.brandImage,
WEBCHAT.DEFAULTS.LOGO
)
)

const headerTitle = getThemeProperty(
WEBCHAT.CUSTOM_PROPERTIES.headerTitle,
WEBCHAT.DEFAULTS.TITLE
)

const headerSubtitle = getThemeProperty(
WEBCHAT.CUSTOM_PROPERTIES.headerSubtitle,
''
)

const handleCloseWebchat = () => {
toggleWebchat(false)

Check warning on line 44 in packages/botonic-react/src/webchat/header/default-header.tsx

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/header/default-header.tsx#L44

Added line #L44 was not covered by tests
}

const color = getThemeProperty(
WEBCHAT.CUSTOM_PROPERTIES.brandColor,
COLORS.BOTONIC_BLUE
)

return (
<HeaderContainer
role={ROLES.HEADER}
color={color}
style={{ ...getThemeProperty(WEBCHAT.CUSTOM_PROPERTIES.headerStyle) }}
>
{headerImage && (
<ImageContainer>
<img src={resolveImage(headerImage)} />
</ImageContainer>
)}
<TextContainer>
<Title>{headerTitle}</Title>
{headerSubtitle ? <Subtitle>{headerSubtitle}</Subtitle> : null}
</TextContainer>
<ConditionalWrapper
condition={animationsEnabled}
wrapper={children => <Scale>{children}</Scale>}
>
<CloseHeader onClick={handleCloseWebchat}></CloseHeader>
</ConditionalWrapper>
</HeaderContainer>
)
}
37 changes: 37 additions & 0 deletions packages/botonic-react/src/webchat/header/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { ForwardedRef, forwardRef, useContext } from 'react'

import { WEBCHAT } from '../../constants'
import { WebchatContext } from '../../contexts'
import { BotonicContainerId } from '../constants'
import { DefaultHeader } from './default-header'
import { StyledWebchatHeader } from './styles'

export const WebchatHeader = forwardRef(
(_, ref: ForwardedRef<HTMLDivElement>) => {
const { getThemeProperty, toggleWebchat } = useContext(WebchatContext)

const handleCloseWebchat = () => {
toggleWebchat(false)

Check warning on line 14 in packages/botonic-react/src/webchat/header/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/header/index.tsx#L14

Added line #L14 was not covered by tests
}

const CustomHeader = getThemeProperty(
WEBCHAT.CUSTOM_PROPERTIES.customHeader
)

if (CustomHeader) {
return (

Check warning on line 22 in packages/botonic-react/src/webchat/header/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/header/index.tsx#L22

Added line #L22 was not covered by tests
<div id={BotonicContainerId.Header} ref={ref}>
<CustomHeader onCloseClick={handleCloseWebchat} />
</div>
)
}

return (
<StyledWebchatHeader id={BotonicContainerId.Header} ref={ref}>
<DefaultHeader />
</StyledWebchatHeader>
)
}
)

WebchatHeader.displayName = 'WebchatHeader'
62 changes: 62 additions & 0 deletions packages/botonic-react/src/webchat/header/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import styled from 'styled-components'

import { COLORS, WEBCHAT } from '../../constants'

export const HeaderContainer = styled.div`
display: flex;
background: linear-gradient(
90deg,
${COLORS.BLEACHED_CEDAR_PURPLE} 0%,
${props => props.color} 100%
);
border-radius: ${WEBCHAT.DEFAULTS.BORDER_RADIUS_TOP_CORNERS};
z-index: 2;
height: inherit;
`

export const ImageContainer = styled.div`
padding: 10px;
align-items: center;
img {
width: 32px;
border-radius: 50%;
}
`

export const TextContainer = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
flex: 1 1 auto;
`

export const Title = styled.div`
display: flex;
font-family: inherit;
font-size: 15px;
font-weight: bold;
color: ${COLORS.SOLID_WHITE};
`

export const Subtitle = styled.div`
display: flex;
font-family: inherit;
font-size: 11px;
color: ${COLORS.SOLID_WHITE};
`

export const CloseHeader = styled.div`
padding: 0px 16px;
cursor: pointer;
color: ${COLORS.SOLID_WHITE};
font-family: inherit;
font-size: 36px;
`

export const StyledWebchatHeader = styled.div`
border-radius: 8px 8px 0px 0px;
box-shadow: ${COLORS.PIGEON_POST_BLUE_ALPHA_0_5} 0px 2px 5px;
height: 55px;
flex: none;
`

0 comments on commit 32c3a71

Please sign in to comment.