Sidebar.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useEffect, useMemo, PropsWithChildren } from 'react'
  21. import { useDispatch, useSelector } from 'react-redux'
  22. import { useLocation, matchPath, useHistory } from 'react-router-dom'
  23. import { showNavigator } from 'containers/App/actions'
  24. import { makeSelectCurrentProject } from 'containers/Projects/selectors'
  25. import { Icon } from 'antd'
  26. import SidebarOption from 'components/SidebarOption'
  27. import Sidebar from 'components/Sidebar'
  28. import { SidebarPermissions } from './constants'
  29. import { IRouteParams } from 'utils/types'
  30. import useProjectPermission from '../Projects/hooks/projectPermission'
  31. import styles from './Main.less'
  32. import classnames from 'classnames'
  33. const sidebarSource: Array<{
  34. icon: React.ReactNode
  35. name: string
  36. routes: string[]
  37. permissionName: typeof SidebarPermissions[number]
  38. }> = [
  39. {
  40. icon: <i className='iconfont icon-dashboard' />,
  41. name: 'vizs',
  42. routes: ['vizs'],
  43. permissionName: 'vizPermission'
  44. },
  45. {
  46. icon: <i className='iconfont icon-widget-gallery' />,
  47. name: 'widgets',
  48. routes: ['widgets'],
  49. permissionName: 'widgetPermission'
  50. },
  51. {
  52. icon: <i className='iconfont icon-custom-business' />,
  53. name: 'views',
  54. routes: ['views'],
  55. permissionName: 'viewPermission'
  56. },
  57. {
  58. icon: <i className='iconfont icon-datasource24' />,
  59. name: 'sources',
  60. routes: ['sources'],
  61. permissionName: 'sourcePermission'
  62. },
  63. {
  64. icon: <Icon type='clock-circle' />,
  65. name: 'schedules',
  66. routes: ['schedules'],
  67. permissionName: 'schedulePermission'
  68. }
  69. ]
  70. const MainSidebar: React.FC<PropsWithChildren<{}>> = (props) => {
  71. const dispatch = useDispatch()
  72. useEffect(() => {
  73. dispatch(showNavigator())
  74. }, [])
  75. const location = useLocation()
  76. const { pathname } = location
  77. const history = useHistory()
  78. const currentProject = useSelector(makeSelectCurrentProject())
  79. useEffect(() => {
  80. if (!currentProject) {
  81. return
  82. }
  83. const { id: projectId, permission } = currentProject
  84. const match = matchPath<IRouteParams>(pathname, {
  85. path: `/project/:projectId`,
  86. exact: true,
  87. strict: false
  88. })
  89. if (match) {
  90. const hasPermission = SidebarPermissions.some((sidebarPermission) => {
  91. if (permission[sidebarPermission] > 0) {
  92. const path = sidebarPermission.slice(0, -10)
  93. history.replace(`/project/${projectId}/${path}s`)
  94. return true
  95. }
  96. })
  97. !hasPermission && history.replace('/noAuthorization')
  98. }
  99. }, [pathname, currentProject])
  100. const AuthorizedSidebarOptions = useProjectPermission(
  101. SidebarOption,
  102. sidebarSource.map(({ permissionName }) => permissionName)
  103. )
  104. const sidebar = useMemo(() => {
  105. if (!currentProject) {
  106. return null
  107. }
  108. const { id: projectId, permission } = currentProject
  109. const vizOnly = SidebarPermissions.every((permissionName) =>
  110. permissionName === 'vizPermission'
  111. ? permission[permissionName]
  112. : !permission[permissionName]
  113. )
  114. if (vizOnly) {
  115. return null
  116. }
  117. const sidebarOptions = sidebarSource.map(
  118. ({ permissionName, routes, icon, name }, idx) => {
  119. if (!permission[permissionName]) {
  120. return null
  121. }
  122. const active = routes.some((route) => pathname.includes(route))
  123. const AuthorizedSidebarOption = AuthorizedSidebarOptions[idx]
  124. return (
  125. // @ts-ignore
  126. <AuthorizedSidebarOption
  127. key={permissionName}
  128. active={active}
  129. indexRoute={routes[0]}
  130. projectId={projectId}
  131. icon={icon}
  132. name={name}
  133. />
  134. )
  135. }
  136. )
  137. return <Sidebar>{sidebarOptions}</Sidebar>
  138. }, [currentProject, pathname])
  139. const sidebarClass = classnames(styles.sidebar, {
  140. [styles.hide]: pathname.includes('dataManager') || pathname.includes('dataShareService') || pathname.includes('dataGovernance')
  141. })
  142. return (
  143. <div className={sidebarClass}>
  144. {sidebar}
  145. <div className={styles.content}>{props.children}</div>
  146. </div>
  147. )
  148. }
  149. export default MainSidebar