Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2304 | Rev 2369 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2301 stevensc 1
import { useEffect, useMemo } from 'react'
2
 
3
import useFetch from '@app/hooks/useFetch'
4
 
5
function useNavbar() {
6
  const { data: menuData } = useFetch('/helpers/menu')
7
  const { data: session, refetch } = useFetch(menuData?.routeCheckSession)
8
 
9
  const memoMenu = useMemo(() => {
2304 stevensc 10
    if (!menuData.menu) return []
2301 stevensc 11
 
2304 stevensc 12
    const menu = structuredClone(menuData?.menu)
13
    const comunicationIndex = menu.findIndex(
14
      (item) => item.label === 'Comunicación'
15
    )
16
    const comunication = menu[comunicationIndex]
2301 stevensc 17
 
2304 stevensc 18
    const newComunicationChilds = comunication.childs.map((child) => {
19
      if (child.label === 'Notificaciones') {
20
        return { ...child, count: session?.total_notifications }
21
      }
2301 stevensc 22
 
2304 stevensc 23
      if (child.label === 'Inmail') {
24
        return { ...child, count: session?.total_messages }
25
      }
2301 stevensc 26
 
2304 stevensc 27
      return child
2301 stevensc 28
    })
29
 
2304 stevensc 30
    comunication.childs = newComunicationChilds
31
    menu[comunicationIndex] = comunication
2303 stevensc 32
 
2304 stevensc 33
    return menu
2301 stevensc 34
  }, [menuData, session])
35
 
36
  useEffect(() => {
37
    const sessionInterval = setInterval(() => refetch(), 3000)
38
 
39
    return () => {
40
      clearInterval(sessionInterval)
41
    }
42
  }, [])
43
 
44
  return {
45
    menuData: { ...menuData, menu: memoMenu },
46
    totalNotifications: session?.total_notifications ?? 0,
47
    totalMessages: session?.total_messages ?? 0
48
  }
49
}
50
 
51
export default useNavbar