Sidebar.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 SidebarOption from 'components/SidebarOption'
  26. import Sidebar from 'components/Sidebar'
  27. import { SidebarPermissions } from '../Main/constants'
  28. import { IRouteParams } from 'utils/types'
  29. import useProjectPermission from '../Projects/hooks/projectPermission'
  30. import styles from '../Main/Main.less'
  31. import { IProjectPermission } from 'containers/Projects/types'
  32. import isEmpty from 'lodash/isEmpty'
  33. const sidebarSource: Array<{
  34. icon: React.ReactNode
  35. name: string
  36. routes: string[]
  37. permissionName: typeof SidebarPermissions[number]
  38. }> = [
  39. {
  40. icon: <img src={require('assets/images/sjzl.svg')} />,
  41. name: '数据总览',
  42. routes: ['dataOverviiews'],
  43. permissionName: 'dataOverviiewPermission'
  44. },
  45. {
  46. icon: <img src={require('assets/images/sjzc.svg')} />,
  47. name: '数据资产',
  48. routes: ['views'],
  49. permissionName: 'viewPermission'
  50. },
  51. {
  52. icon: <img src={require('assets/images/sjy.svg')} />,
  53. name: '数据源',
  54. routes: ['sources'],
  55. permissionName: 'sourcePermission'
  56. },
  57. {
  58. icon: <img src={require('assets/images/dictionary.svg')} />,
  59. name: '数据字典',
  60. routes: ['dataDictionarys'],
  61. permissionName: 'dataDictionaryPermission'
  62. }
  63. ]
  64. const MainSidebar: React.FC<PropsWithChildren<{}>> = (props) => {
  65. const dispatch = useDispatch()
  66. useEffect(() => {
  67. dispatch(showNavigator())
  68. }, [])
  69. const location = useLocation()
  70. const { pathname } = location
  71. const history = useHistory()
  72. const currentProject = useSelector(makeSelectCurrentProject())
  73. // @ts-ignore
  74. const nextPermission = useMemo<IProjectPermission>(
  75. () => ({
  76. ...(currentProject?.permission ?? {}),
  77. ...{
  78. dataOverviiewPermission: 3,
  79. dataDictionaryPermission: 3,
  80. makeRulePermission: 3,
  81. qualityAuditPermission: 3,
  82. auditAnalysisPermission: 3
  83. }
  84. }),
  85. [currentProject?.permission]
  86. )
  87. useEffect(() => {
  88. if (!currentProject) {
  89. return
  90. }
  91. const { id: projectId } = currentProject
  92. const match = matchPath<IRouteParams>(pathname, {
  93. path: `/project/:projectId/dataManager`,
  94. exact: true,
  95. strict: false
  96. })
  97. if (match) {
  98. const hasPermission = SidebarPermissions.some((sidebarPermission) => {
  99. if (nextPermission[sidebarPermission] > 0) {
  100. const path = sidebarPermission.slice(0, -10)
  101. history.replace(`/project/${projectId}/dataManager/${path}s`)
  102. return true
  103. }
  104. })
  105. !hasPermission && history.replace('/noAuthorization')
  106. }
  107. }, [pathname, currentProject?.id, nextPermission])
  108. const AuthorizedSidebarOptions = useProjectPermission(
  109. SidebarOption,
  110. sidebarSource.map(({ permissionName }) => permissionName)
  111. )
  112. const sidebar = useMemo(() => {
  113. if (!currentProject) {
  114. return null
  115. }
  116. if (isEmpty(nextPermission)) {
  117. return null
  118. }
  119. const { id: projectId } = currentProject
  120. const vizOnly = SidebarPermissions.every((permissionName) =>
  121. permissionName === 'vizPermission'
  122. ? nextPermission[permissionName]
  123. : !nextPermission[permissionName]
  124. )
  125. if (vizOnly) {
  126. return null
  127. }
  128. const sidebarOptions = sidebarSource.map(
  129. ({ permissionName, routes, icon, name }, idx) => {
  130. if (!nextPermission[permissionName]) {
  131. return null
  132. }
  133. const active = routes.some((route) => pathname.includes(route))
  134. const AuthorizedSidebarOption = AuthorizedSidebarOptions[idx]
  135. return (
  136. <AuthorizedSidebarOption
  137. key={permissionName}
  138. active={active}
  139. indexRoute={routes[0]}
  140. projectId={projectId}
  141. icon={icon}
  142. name={name}
  143. link={`/project/${projectId}/dataManager/${routes[0]}`}
  144. />
  145. )
  146. }
  147. )
  148. return <Sidebar>{sidebarOptions}</Sidebar>
  149. }, [currentProject?.id, nextPermission, pathname])
  150. return (
  151. <div className={styles.sidebar}>
  152. {sidebar}
  153. <div className={styles.content}>{props.children}</div>
  154. </div>
  155. )
  156. }
  157. export default MainSidebar