Skip to content
Snippets Groups Projects
Commit 92d8930a authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

chore: avoid useless dispatch

parent b0c29d71
No related branches found
No related tags found
2 merge requests!12413.1 Release,!1172chore: avoid useless dispatch
import FeedbackModal from 'components/Feedback/FeedbackModal'
import { ScreenType } from 'enums'
import React, { useEffect } from 'react'
import React, { useEffect, useRef } from 'react'
import { changeScreenType } from 'store/global/global.slice'
import { useAppDispatch, useAppSelector } from 'store/hooks'
import './content.scss'
......@@ -10,6 +10,7 @@ const Content = ({ children }: { children: React.ReactNode }) => {
const { screenType, headerHeight } = useAppSelector(
state => state.ecolyo.global
)
const currentScreenType = useRef(screenType)
const cozyBarHeight = 48
const cozyNavHeight = 56
......@@ -21,16 +22,24 @@ const Content = ({ children }: { children: React.ReactNode }) => {
useEffect(() => {
function handleResize() {
let newScreenType: ScreenType
if (innerWidth <= 768) {
dispatch(changeScreenType(ScreenType.MOBILE))
newScreenType = ScreenType.MOBILE
} else if (innerWidth <= 1024) {
dispatch(changeScreenType(ScreenType.TABLET))
newScreenType = ScreenType.TABLET
} else {
dispatch(changeScreenType(ScreenType.DESKTOP))
newScreenType = ScreenType.DESKTOP
}
if (currentScreenType.current !== newScreenType) {
currentScreenType.current = newScreenType
dispatch(changeScreenType(newScreenType))
}
}
handleResize()
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
......
......@@ -68,9 +68,8 @@ const CozyBar = ({
</BarRight>
</>
)
} else {
return null
}
return null
}
export default CozyBar
......@@ -29,7 +29,9 @@ const Header = ({
const navigate = useNavigate()
const header = useRef<HTMLDivElement>(null)
const dispatch = useAppDispatch()
const { screenType } = useAppSelector(state => state.ecolyo.global)
const { screenType, headerHeight } = useAppSelector(
state => state.ecolyo.global
)
const cozyBarHeight = 48
const handleClickBack = useCallback(() => {
......@@ -45,10 +47,13 @@ const Header = ({
}
useEffect(() => {
const headerHeight = header.current?.clientHeight || 0
const refHeight = header.current?.clientHeight || 0
const adjustment = screenType === ScreenType.MOBILE ? cozyBarHeight : 0
dispatch(setHeaderHeight(headerHeight - adjustment))
}, [screenType, children, dispatch])
const targetHeight = refHeight - adjustment
if (targetHeight !== headerHeight) {
dispatch(setHeaderHeight(targetHeight))
}
}, [screenType, children, dispatch, headerHeight])
return (
<header ref={header}>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment