File size: 1,445 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
import React from 'react'
import { Navigation, NavigationElementProps } from '@adminjs/design-system'
import { useNavigate, useLocation } from 'react-router'

import ViewHelpers from '../../../../backend/utils/view-helpers/view-helpers.js'
import { useTranslation } from '../../../hooks/use-translation.js'
import { ReduxState } from '../../../store/store.js'
import allowOverride from '../../../hoc/allow-override.js'

type Props = {
  pages?: ReduxState['pages'];
}

const h = new ViewHelpers()

const SidebarPages: React.FC<Props> = (props) => {
  const { pages } = props

  const { translateLabel, translatePage } = useTranslation()
  const location = useLocation()
  const navigate = useNavigate()

  if (!pages || !pages.length) {
    return null
  }

  const isActive = (page): boolean => (
    !!location.pathname.match(`/pages/${page.name}`)
  )

  const elements: Array<NavigationElementProps> = pages.map((page) => ({
    id: page.name,
    label: translatePage(page.name),
    isSelected: isActive(page),
    icon: page.icon,
    href: h.pageUrl(page.name),
    onClick: (event, element): void => {
      event.preventDefault()
      if (element.href) {
        navigate(element.href)
      }
    },
  }))

  return (
    <Navigation
      label={translateLabel('pages')}
      elements={elements}
    />
  )
}

export default allowOverride(SidebarPages, 'SidebarPages')
export { SidebarPages as OriginalSidebarPages, SidebarPages }