index.tsx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { useState, useRef, useEffect } from 'react';
  2. import { useSelector } from 'react-redux';
  3. import { createPortal } from 'react-dom';
  4. import Grid from '@mui/material/Grid'
  5. import SmallMenuBar from './SmallMenuBar'
  6. import SearchLists from './SearchLists';
  7. import SearchBar from './SearchBar'
  8. import ChatsList from './ChatsList'
  9. import MenuBar from './MenuBar'
  10. import ContactsList from './ContactsList'
  11. import AddContact from './AddContact'
  12. import SettingsBar from './SettingsBar';
  13. import EditBar from './EditBar';
  14. import { getState } from '../../../redux/authorization/selector'
  15. const LeftBar = () => {
  16. const { sort,nightMode } = useSelector(getState)
  17. const [isSearch, setIsSearch] = useState<boolean>(false)
  18. const [isMenu, setIsMenu] = useState<boolean>(false)
  19. const [isMenuSm, setIsMenuSm] = useState<boolean>(false);
  20. const [selectedIndex, setSelectedIndex] = useState<number | null>(null)
  21. const [value, setValue] = useState<string>('')
  22. const modalRoot = useRef<HTMLDivElement|null>(null);
  23. const handleFocus = (): void => setIsSearch(true)
  24. const handleClick = (): void => {
  25. setValue('')
  26. if (selectedIndex) setSelectedIndex(null)
  27. if(!isSearch) return setIsMenu(!isMenu)
  28. setIsSearch(false)
  29. }
  30. const handleSearch = (e: React.ChangeEvent<HTMLInputElement>):void => setValue(e.target.value)
  31. const handleEnterOpenMenuSm = (): void => {
  32. if(!isMenuSm) setIsMenuSm(true)
  33. }
  34. const handleLeaveCloseMenuSm = (e: any): void => {
  35. if(isMenuSm&&e.pageX > 480) setIsMenuSm(false)
  36. }
  37. const handleSelectedMenu = (i: number) => {
  38. setIsSearch(true)
  39. setIsMenu(false)
  40. setIsMenuSm(false)
  41. setValue('')
  42. setSelectedIndex(i)
  43. }
  44. useEffect(() => {
  45. const handleCloseModal = (e:any) => {
  46. if (e.target.id === 'overlay') {
  47. setIsMenu(false)
  48. setIsMenuSm(false)
  49. }
  50. }
  51. if (!modalRoot.current) {
  52. const modal = document.getElementById('modal-root') as HTMLDivElement | null
  53. if (modal) {
  54. modal.addEventListener('click',handleCloseModal)
  55. modalRoot.current = modal
  56. }
  57. }
  58. return () => {
  59. if (modalRoot.current) {
  60. modalRoot.current.removeEventListener('click',handleCloseModal)
  61. modalRoot.current = null
  62. }
  63. }
  64. }, [])
  65. return (
  66. <Grid item lg={3} onMouseEnter={handleEnterOpenMenuSm} onMouseLeave={handleLeaveCloseMenuSm}>
  67. {selectedIndex !== 2 && selectedIndex !== 4 &&
  68. <SearchBar handleClick={handleClick} handleFocus={handleFocus} sort={sort}
  69. handleSearch={handleSearch} isSearch={isSearch} selectedIndex={selectedIndex} value={value}/>}
  70. {!selectedIndex&&isSearch && <SearchLists value={value} setValue={setValue} sort={sort}/>}
  71. {!selectedIndex&&!isSearch &&<ChatsList sort={sort}/>}
  72. {!selectedIndex && isMenuSm && !isSearch && <SmallMenuBar
  73. handleSelectedMenu={handleSelectedMenu} setIsMenuSm={setIsMenuSm} />}
  74. {isMenu && modalRoot.current &&
  75. createPortal(<MenuBar handleSelectedMenu={handleSelectedMenu} nightMode={nightMode}/>, modalRoot.current)}
  76. {selectedIndex === 1 && <ContactsList handleClick={handleClick} value={value} sort={sort}/>}
  77. {selectedIndex === 2 && <SettingsBar setSelectedIndex={setSelectedIndex}/>}
  78. {selectedIndex === 3 && <AddContact setSelectedIndex={setSelectedIndex} />}
  79. {selectedIndex === 4 && <EditBar/>}
  80. </Grid>
  81. )
  82. }
  83. export default LeftBar