Proyectos de Subversion LeadersLinked - SPA

Rev

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