Sidebar.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ...(currentProject?.permission ?? {}),
  76. ...{
  77. dataOverviiewPermission: 3,
  78. dataDictionaryPermission: 3,
  79. makeRulePermission: 3,
  80. qualityAuditPermission: 3,
  81. auditAnalysisPermission: 3
  82. }
  83. })
  84. , [currentProject?.permission])
  85. useEffect(() => {
  86. if (!currentProject) {
  87. return
  88. }
  89. const { id: projectId } = currentProject
  90. const match = matchPath<IRouteParams>(pathname, {
  91. path: `/project/:projectId/dataManager`,
  92. exact: true,
  93. strict: false
  94. })
  95. if (match) {
  96. const hasPermission = SidebarPermissions.some((sidebarPermission) => {
  97. if (nextPermission[sidebarPermission] > 0) {
  98. const path = sidebarPermission.slice(0, -10)
  99. history.replace(`/project/${projectId}/dataManager/${path}s`)
  100. return true
  101. }
  102. })
  103. !hasPermission && history.replace('/noAuthorization')
  104. }
  105. }, [pathname, currentProject?.id, nextPermission])
  106. const AuthorizedSidebarOptions = useProjectPermission(
  107. SidebarOption,
  108. sidebarSource.map(({ permissionName }) => permissionName)
  109. )
  110. const sidebar = useMemo(() => {
  111. if (!currentProject) {
  112. return null
  113. }
  114. if (isEmpty(nextPermission)) {
  115. return null
  116. }
  117. const { id: projectId } = currentProject
  118. const vizOnly = SidebarPermissions.every((permissionName) =>
  119. permissionName === 'vizPermission'
  120. ? nextPermission[permissionName]
  121. : !nextPermission[permissionName]
  122. )
  123. if (vizOnly) {
  124. return null
  125. }
  126. const sidebarOptions = sidebarSource.map(
  127. ({ permissionName, routes, icon, name }, idx) => {
  128. if (!nextPermission[permissionName]) {
  129. return null
  130. }
  131. const active = routes.some((route) => pathname.includes(route))
  132. const AuthorizedSidebarOption = AuthorizedSidebarOptions[idx]
  133. return (
  134. <AuthorizedSidebarOption
  135. key={permissionName}
  136. active={active}
  137. indexRoute={routes[0]}
  138. projectId={projectId}
  139. icon={icon}
  140. name={name}
  141. link={`/project/${projectId}/dataManager/${routes[0]}`}
  142. />
  143. )
  144. }
  145. )
  146. console.log(sidebarOptions)
  147. return <Sidebar>{sidebarOptions}</Sidebar>
  148. }, [currentProject?.id, nextPermission, pathname])
  149. return (
  150. <div className={styles.sidebar}>
  151. {sidebar}
  152. <div className={styles.content}>{props.children}</div>
  153. </div>
  154. )
  155. }
  156. export default MainSidebar