import { useState, useRef, useEffect } from 'react'; import { useSelector } from 'react-redux'; import { createPortal } from 'react-dom'; import Grid from '@mui/material/Grid' import SmallMenuBar from './SmallMenuBar' import SearchLists from './SearchLists'; import SearchBar from './SearchBar' import ChatsList from './ChatsList' import MenuBar from './MenuBar' import ContactsList from './ContactsList' import AddContact from './AddContact' import SettingsBar from './SettingsBar'; import EditBar from './EditBar'; import { getState } from '../../../redux/authorization/selector' const LeftBar = () => { const { sort,nightMode } = useSelector(getState) const [isSearch, setIsSearch] = useState(false) const [isMenu, setIsMenu] = useState(false) const [isMenuSm, setIsMenuSm] = useState(false); const [selectedIndex, setSelectedIndex] = useState(null) const [value, setValue] = useState('') const [date, setDate] = useState(''); const modalRoot = useRef(null); const handleFocus = (): void => setIsSearch(true) const handleClick = (): void => { value&&setValue('') date&&setDate('') if (selectedIndex) setSelectedIndex(null) if(!isSearch) return setIsMenu(!isMenu) setIsSearch(false) } const handleSearch = (e: React.ChangeEvent):void => setValue(e.target.value) const handleEnterOpenMenuSm = (): void => { if(!isMenuSm) setIsMenuSm(true) } const handleLeaveCloseMenuSm = (): void => { if(isMenuSm) setIsMenuSm(false) } const handleSelectedMenu = (i: number) => { setIsSearch(true) setIsMenu(false) setIsMenuSm(false) value&&setValue('') date&&setDate('') setSelectedIndex(i) } useEffect(() => { const handleCloseModal = (e:any) => { if (e.target.id === 'overlay') { setIsMenu(false) setIsMenuSm(false) } } if (!modalRoot.current) { const modal = document.getElementById('modal-root') as HTMLDivElement | null if (modal) { modal.addEventListener('click',handleCloseModal) modalRoot.current = modal } } return () => { if (modalRoot.current) { modalRoot.current.removeEventListener('click',handleCloseModal) modalRoot.current = null } } }, []) return ( {selectedIndex !== 2 && selectedIndex !== 3 && selectedIndex !== 4 && } {!selectedIndex && isSearch && } {!selectedIndex&&!isSearch &&} {!selectedIndex && isMenuSm && !isSearch && } {isMenu && modalRoot.current && createPortal(, modalRoot.current)} {selectedIndex === 1 && } {selectedIndex === 2 && } {selectedIndex === 3 && } {selectedIndex === 4 && } ) } export default LeftBar