File size: 3,288 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Box, cssClass, Text } from '@adminjs/design-system'
import { styled } from '@adminjs/design-system/styled-components'
import React from 'react'
import { Link } from 'react-router-dom'

import ViewHelpers from '../../../backend/utils/view-helpers/view-helpers.js'
import allowOverride from '../../hoc/allow-override.js'
import { useTranslation } from '../../hooks/use-translation.js'
import { RecordJSON, ResourceJSON } from '../../interfaces/index.js'
import { getActionElementCss } from '../../utils/index.js'

export const BreadcrumbLink: any = styled(Link)`
  color: ${({ theme }): string => theme.colors.grey60};
  font-family: ${({ theme }): string => theme.font};
  line-height: ${({ theme }): string => theme.lineHeights.default};
  font-size: ${({ theme }): string => theme.fontSizes.default};
  text-decoration: none;

  &:hover {
    color: ${({ theme }): string => theme.colors.primary100};
    &:after {
      color: ${({ theme }): string => theme.colors.grey60};
    }
  }

  &:after {
    content: '/';
    padding: 0 ${({ theme }): string => theme.space.default};
  }

  &:last-child {
    color: ${({ theme }): string => theme.colors.text};
    &:after {
      content: '';
    }
  }
`

export const BreadcrumbText: any = styled<any>(Text)`
  color: ${({ theme }): string => theme.colors.grey100};
  font-family: ${({ theme }): string => theme.font};
  font-weight: ${({ theme }): string => theme.fontWeights.normal.toString()};
  line-height: ${({ theme }): string => theme.lineHeights.default};
  font-size: ${({ theme }): string => theme.fontSizes.default};
  cursor: pointer;
  display: inline;

  &:after {
    content: '/';
    padding: 0 ${({ theme }): string => theme.space.default};
  }

  &:last-child {
    &:after {
      content: '';
    }
  }
`

/**
 * @memberof Breadcrumbs
 */
export type BreadcrumbProps = {
  /**
   * Resource
   */
  resource: ResourceJSON
  /**
   * record
   */
  record?: RecordJSON | null
  /**
   * Name of an action
   */
  actionName: string
}

/**
 * @component
 * @private
 */
const Breadcrumbs: React.FC<BreadcrumbProps> = (props) => {
  const { resource, record, actionName } = props

  const listAction = resource.resourceActions.find(({ name }) => name === 'list')
  const action = resource.actions.find((a) => a.name === actionName)
  const h = new ViewHelpers()
  const { tl, ta } = useTranslation()
  const contentTag = getActionElementCss(resource.id, actionName, 'breadcrumbs')

  return (
    <Box flexGrow={1} className={cssClass('Breadcrumbs')} data-css={contentTag}>
      <BreadcrumbLink to={h.dashboardUrl()}>{tl('dashboard')}</BreadcrumbLink>
      {listAction ? (
        <BreadcrumbLink
          to={resource.href ? resource.href : '/'}
          className={record ? 'is-active' : ''}
        >
          {tl(resource.name, resource.id)}
        </BreadcrumbLink>
      ) : (
        <BreadcrumbText>{tl(resource.name, resource.id)}</BreadcrumbText>
      )}
      {action && action.name !== 'list' && (
        <BreadcrumbLink to="#">{ta(action.label)}</BreadcrumbLink>
      )}
    </Box>
  )
}

const OverridableBreadcrumbs = allowOverride(Breadcrumbs, 'Breadcrumbs')

export {
  OverridableBreadcrumbs as default,
  OverridableBreadcrumbs as Breadcrumbs,
  Breadcrumbs as OriginalBreadcrumbs,
}