Skip to content

Commit

Permalink
refactor: simplify use-webchat-resizer
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Sep 19, 2024
1 parent 4dbb311 commit b77cc69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useDeviceAdapter = (host, isWebchatOpen) => {
window.innerHeight
)

const { handleKeyboardShown, handleKeyboardHidden } = useWebchatResizer(host)
const { handleKeyboardShown, handleKeyboardHidden } = useWebchatResizer()

const { handleOnTouchMoveEvents, handleScrollEvents } =
useScrollbarController(currentDevice, host)
Expand Down
48 changes: 27 additions & 21 deletions packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@ import { useContext } from 'react'
import { WebchatContext } from '../../contexts'
import { useScrollToBottom } from './use-scroll-to-bottom'

export const useWebchatResizer = host => {
const calculateResizedPercentualWebchatHeight = webchatElement => {
const webchatHeight = webchatElement.clientHeight || 0
const keyboardOffset =
(window.visualViewport && window.visualViewport.height) ||
window.innerHeight
let newWebchatPercentualHeight = keyboardOffset / webchatHeight
newWebchatPercentualHeight =

Check warning on line 12 in packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts#L11-L12

Added lines #L11 - L12 were not covered by tests
Math.round(newWebchatPercentualHeight * 100 * 100) / 100 // Two decimal places
return newWebchatPercentualHeight

Check warning on line 14 in packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts#L14

Added line #L14 was not covered by tests
}

const calculateResizedPxChatAreaHeight = (
webchatElement,
headerElement,
inputPanelElement
) => {
return (

Check warning on line 22 in packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts#L22

Added line #L22 was not covered by tests
webchatElement.clientHeight -
headerElement.clientHeight -
inputPanelElement.clientHeight
)
}

export const useWebchatResizer = () => {
const { webchatRef, chatAreaRef, inputPanelRef, headerRef } =
useContext(WebchatContext)
const { scrollToBottom } = useScrollToBottom({ host })

const handleKeyboardShown = () => {
const calculateNewWebchatElementHeight = () => {
const webchatHeight = webchatRef.current?.clientHeight || 0
const keyboardOffset =
(window.visualViewport && window.visualViewport.height) ||
window.innerHeight
let newWebchatPercentualHeight = keyboardOffset / webchatHeight
newWebchatPercentualHeight =
Math.round(newWebchatPercentualHeight * 100 * 100) / 100 // Two decimal places
return newWebchatPercentualHeight
}

if (
webchatRef.current &&
chatAreaRef.current &&
headerRef.current &&
inputPanelRef.current
) {
const newWebchatPercentualHeight = `${calculateNewWebchatElementHeight()}%`
webchatRef.current.style.height = newWebchatPercentualHeight

const newChatAreaHeight = `${webchatRef.current.clientHeight - headerRef.current.clientHeight - inputPanelRef.current.clientHeight}px`

chatAreaRef.current.style.height = newChatAreaHeight
scrollToBottom()
webchatRef.current.style.height = `${calculateResizedPercentualWebchatHeight(webchatRef.current)}%`
chatAreaRef.current.style.height = `${calculateResizedPxChatAreaHeight(webchatRef.current, headerRef.current, inputPanelRef.current)}px`

Check warning on line 41 in packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts#L40-L41

Added lines #L40 - L41 were not covered by tests
}
}

Expand All @@ -44,7 +50,7 @@ export const useWebchatResizer = host => {
headerRef.current
) {
webchatRef.current.style.height = '100%'
chatAreaRef.current.style.height = `${webchatRef.current.clientHeight - headerRef.current.clientHeight - inputPanelRef.current.clientHeight}px`
chatAreaRef.current.style.height = `${calculateResizedPxChatAreaHeight(webchatRef.current, headerRef.current, inputPanelRef.current)}px`

Check warning on line 53 in packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-react/src/webchat/hooks/use-webchat-resizer.ts#L52-L53

Added lines #L52 - L53 were not covered by tests
}
}

Expand Down

0 comments on commit b77cc69

Please sign in to comment.