Sidebar.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: <img src={require('assets/images/sjkb.svg')} />,
  40. name: '数据看板',
  41. routes: ['vizs'],
  42. permissionName: 'vizPermission'
  43. },
  44. {
  45. icon: <img src={require('assets/images/kshzj.svg')} />,
  46. name: '可视化组件',
  47. routes: ['widgets'],
  48. permissionName: 'widgetPermission'
  49. },
  50. {
  51. icon: <img src={require('assets/images/dsrw.svg')} />,
  52. name: '定时任务',
  53. routes: ['schedules'],
  54. permissionName: 'schedulePermission'
  55. }
  56. ]
  57. const MainSidebar: React.FC<PropsWithChildren<{}>> = (props) => {
  58. const dispatch = useDispatch()
  59. useEffect(() => {
  60. dispatch(showNavigator())
  61. }, [])
  62. const location = useLocation()
  63. const { pathname } = location
  64. const history = useHistory()
  65. const currentProject = useSelector(makeSelectCurrentProject())
  66. useEffect(() => {
  67. if (!currentProject) {
  68. return
  69. }
  70. const { id: projectId, permission } = currentProject
  71. const match = matchPath<IRouteParams>(pathname, {
  72. path: `/project/:projectId/dataShareService`,
  73. exact: true,
  74. strict: false
  75. })
  76. if (match) {
  77. const hasPermission = SidebarPermissions.some((sidebarPermission) => {
  78. if (permission[sidebarPermission] > 0) {
  79. const path = sidebarPermission.slice(0, -10)
  80. history.replace(`/project/${projectId}/dataShareService/${path}s`)
  81. return true
  82. }
  83. })
  84. !hasPermission && history.replace('/noAuthorization')
  85. }
  86. }, [pathname, currentProject])
  87. const AuthorizedSidebarOptions = useProjectPermission(
  88. SidebarOption,
  89. sidebarSource.map(({ permissionName }) => permissionName)
  90. )
  91. const sidebar = useMemo(() => {
  92. if (!currentProject) {
  93. return null
  94. }
  95. const { id: projectId, permission } = currentProject
  96. const vizOnly = SidebarPermissions.every((permissionName) =>
  97. permissionName === 'vizPermission'
  98. ? permission[permissionName]
  99. : !permission[permissionName]
  100. )
  101. if (vizOnly) {
  102. return null
  103. }
  104. const sidebarOptions = sidebarSource.map(
  105. ({ permissionName, routes, icon, name }, idx) => {
  106. if (!permission[permissionName]) {
  107. return null
  108. }
  109. const active = routes.some((route) => pathname.includes(route))
  110. const AuthorizedSidebarOption = AuthorizedSidebarOptions[idx]
  111. return (
  112. <AuthorizedSidebarOption
  113. key={permissionName}
  114. active={active}
  115. indexRoute={routes[0]}
  116. projectId={projectId}
  117. icon={icon}
  118. name={name}
  119. link={`/project/${projectId}/dataShareService/${routes[0]}`}
  120. />
  121. )
  122. }
  123. )
  124. return <Sidebar>{sidebarOptions}</Sidebar>
  125. }, [currentProject, pathname])
  126. return (
  127. <div className={styles.sidebar}>
  128. {sidebar}
  129. <div className={styles.content}>{props.children}</div>
  130. </div>
  131. )
  132. }
  133. export default MainSidebar