File size: 2,656 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { useLocation, useNavigate } from 'react-router'
/* eslint-disable no-param-reassign */
import type {
  IconProps,
  NavigationElementWithChildrenProps,
  NavigationProps,
} from '@adminjs/design-system'
import { useMemo } from 'react'

import { useTranslation } from '../hooks/use-translation.js'
import { ResourceJSON } from '../interfaces/index.js'
import useLocalStorage from './use-local-storage/use-local-storage.js'

const isSelected = (href, location): boolean => {
  const regExp = new RegExp(`${href}($|/)`)
  return !!location.pathname.match(regExp)
}

export function useNavigationResources(
  resources: Array<ResourceJSON>,
): NavigationProps['elements'] {
  const [openElements, setOpenElements] = useLocalStorage<Record<string, boolean>>('sidebarElements', {})
  const navigate = useNavigate()
  const location = useLocation()
  const { translateLabel } = useTranslation()

  const enrichResource = useMemo(() => (
    resource: ResourceJSON,
    icon?: IconProps['icon'],
  ): NavigationElementWithChildrenProps => ({
    href: resource.href || undefined,
    icon,
    isSelected: isSelected(resource.href, location),
    label: translateLabel(resource.name, resource.id),
    id: resource.id,
    onClick: (event): void => {
      if (resource.href) {
        event.preventDefault()
        navigate(resource.href)
      }
    },
  }), [location, navigate])

  // grouping resources into parents
  const map = resources
    // first filter out resources which are not visible
    .filter((res) => res.href && res.navigation?.show !== false)
    .reduce((memo, resource) => {
      // in case resource has the same name as parent we namespace it wit "resource-""
      const key = resource.navigation?.name || ['resource', resource.name].join('-')

      if (!resource.navigation || resource.navigation.name === null) {
        memo[key] = enrichResource(resource, resource.navigation?.icon)
      } else if (memo[key] && memo[key].elements && resource.navigation?.name) {
        memo[key].label = translateLabel(resource.navigation?.name)
        memo[key].elements?.push?.(enrichResource(resource))
      } else {
        memo[key] = {
          elements: [enrichResource(resource)],
          label: translateLabel(resource.navigation?.name),
          icon: resource.navigation?.icon,
          onClick: (): void => setOpenElements({
            ...openElements,
            [key]: !openElements[key],
          }),
          isOpen: !!openElements[key],
        }
      }
      return memo
    }, {} as Record<string, NavigationElementWithChildrenProps>)

  return Object.values(map)
}

export default useNavigationResources