Proyectos de Subversion LeadersLinked - SPA

Rev

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

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