Sidebar.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 '../Main/constants'
  29. import { IRouteParams } from 'utils/types'
  30. import useProjectPermission from '../Projects/hooks/projectPermission'
  31. import styles from '../Main/Main.less'
  32. const sidebarSource: Array<{
  33. icon: React.ReactNode
  34. name: string
  35. routes: string[]
  36. permissionName: typeof SidebarPermissions[number]
  37. }> = [
  38. {
  39. icon: <i className='iconfont icon-dashboard' />,
  40. name: '数据看板',
  41. routes: ['vizs'],
  42. permissionName: 'vizPermission'
  43. },
  44. {
  45. icon: <i className='iconfont icon-widget-gallery' />,
  46. name: '可视化组件',
  47. routes: ['widgets'],
  48. permissionName: 'widgetPermission'
  49. },
  50. // {
  51. // icon: <i className='iconfont icon-custom-business' />,
  52. // name: '数据资产',
  53. // routes: ['views'],
  54. // permissionName: 'viewPermission'
  55. // },
  56. // {
  57. // icon: <i className='iconfont icon-datasource24' />,
  58. // name: '数据源',
  59. // routes: ['sources'],
  60. // permissionName: 'sourcePermission'
  61. // },
  62. {
  63. icon: <Icon type='clock-circle' />,
  64. name: '定时任务',
  65. routes: ['schedules'],
  66. permissionName: 'schedulePermission'
  67. }
  68. ]
  69. const MainSidebar: React.FC<PropsWithChildren<{}>> = (props) => {
  70. const dispatch = useDispatch()
  71. useEffect(() => {
  72. dispatch(showNavigator())
  73. }, [])
  74. const location = useLocation()
  75. const { pathname } = location
  76. const history = useHistory()
  77. const currentProject = useSelector(makeSelectCurrentProject())
  78. useEffect(() => {
  79. if (!currentProject) {
  80. return
  81. }
  82. const { id: projectId, permission } = currentProject
  83. const match = matchPath<IRouteParams>(pathname, {
  84. path: `/project/:projectId/dataShareService`,
  85. exact: true,
  86. strict: false
  87. })
  88. if (match) {
  89. const hasPermission = SidebarPermissions.some((sidebarPermission) => {
  90. if (permission[sidebarPermission] > 0) {
  91. const path = sidebarPermission.slice(0, -10)
  92. history.replace(`/project/${projectId}/dataShareService/${path}s`)
  93. return true
  94. }
  95. })
  96. !hasPermission && history.replace('/noAuthorization')
  97. }
  98. }, [pathname, currentProject])
  99. const AuthorizedSidebarOptions = useProjectPermission(
  100. SidebarOption,
  101. sidebarSource.map(({ permissionName }) => permissionName)
  102. )
  103. const sidebar = useMemo(() => {
  104. if (!currentProject) {
  105. return null
  106. }
  107. const { id: projectId, permission } = currentProject
  108. const vizOnly = SidebarPermissions.every((permissionName) =>
  109. permissionName === 'vizPermission'
  110. ? permission[permissionName]
  111. : !permission[permissionName]
  112. )
  113. if (vizOnly) {
  114. return null
  115. }
  116. const sidebarOptions = sidebarSource.map(
  117. ({ permissionName, routes, icon, name }, idx) => {
  118. if (!permission[permissionName]) {
  119. return null
  120. }
  121. const active = routes.some((route) => pathname.includes(route))
  122. const AuthorizedSidebarOption = AuthorizedSidebarOptions[idx]
  123. return (
  124. <AuthorizedSidebarOption
  125. key={permissionName}
  126. active={active}
  127. indexRoute={routes[0]}
  128. projectId={projectId}
  129. icon={icon}
  130. name={name}
  131. link={`/project/${projectId}/dataShareService/${routes[0]}`}
  132. />
  133. )
  134. }
  135. )
  136. return <Sidebar>{sidebarOptions}</Sidebar>
  137. }, [currentProject, pathname])
  138. return (
  139. <div className={styles.sidebar}>
  140. {sidebar}
  141. <div className={styles.content}>{props.children}</div>
  142. </div>
  143. )
  144. }
  145. export default MainSidebar