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. import { IProjectPermission } from 'containers/Projects/types'
  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/gzzd.svg')} />,
  41. name: '规则制定',
  42. routes: ['makeRules'],
  43. permissionName: 'makeRulePermission'
  44. },
  45. {
  46. icon: <img src={require('assets/images/zljh.svg')} />,
  47. name: '质量稽核',
  48. routes: ['qualityAudits'],
  49. permissionName: 'qualityAuditPermission'
  50. },
  51. {
  52. icon: <img src={require('assets/images/jhfx.svg')} />,
  53. name: '稽核分析',
  54. routes: ['auditAnalysiss'],
  55. permissionName: 'auditAnalysisPermission'
  56. }
  57. ]
  58. const MainSidebar: React.FC<PropsWithChildren<{}>> = (props) => {
  59. const dispatch = useDispatch()
  60. useEffect(() => {
  61. dispatch(showNavigator())
  62. }, [])
  63. const location = useLocation()
  64. const { pathname } = location
  65. const history = useHistory()
  66. const currentProject = useSelector(makeSelectCurrentProject())
  67. // @ts-ignore
  68. const nextPermission = useMemo<IProjectPermission>(() => ({
  69. ...(currentProject?.permission ?? {}),
  70. ...{
  71. makeRulePermission: 3,
  72. qualityAuditPermission: 3,
  73. auditAnalysisPermission: 3
  74. }
  75. })
  76. , [currentProject?.permission])
  77. useEffect(() => {
  78. if (!currentProject) {
  79. return
  80. }
  81. const { id: projectId } = currentProject
  82. const match = matchPath<IRouteParams>(pathname, {
  83. path: `/project/:projectId/dataGovernance`,
  84. exact: true,
  85. strict: false
  86. })
  87. if (match) {
  88. const hasPermission = SidebarPermissions.some((sidebarPermission) => {
  89. if (nextPermission[sidebarPermission] > 0) {
  90. const path = sidebarPermission.slice(0, -10)
  91. history.replace(`/project/${projectId}/dataGovernance/${path}s`)
  92. return true
  93. }
  94. })
  95. !hasPermission && history.replace('/noAuthorization')
  96. }
  97. }, [pathname, currentProject?.id, nextPermission])
  98. const AuthorizedSidebarOptions = useProjectPermission(
  99. SidebarOption,
  100. sidebarSource.map(({ permissionName }) => permissionName)
  101. )
  102. const sidebar = useMemo(() => {
  103. if (!currentProject) {
  104. return null
  105. }
  106. const { id: projectId } = currentProject
  107. const vizOnly = SidebarPermissions.every((permissionName) =>
  108. permissionName === 'vizPermission'
  109. ? nextPermission[permissionName]
  110. : !nextPermission[permissionName]
  111. )
  112. if (vizOnly) {
  113. return null
  114. }
  115. const sidebarOptions = sidebarSource.map(
  116. ({ permissionName, routes, icon, name }, idx) => {
  117. if (!nextPermission[permissionName]) {
  118. return null
  119. }
  120. const active = routes.some((route) => pathname.includes(route))
  121. const AuthorizedSidebarOption = AuthorizedSidebarOptions[idx]
  122. return (
  123. <AuthorizedSidebarOption
  124. key={permissionName}
  125. active={active}
  126. indexRoute={routes[0]}
  127. projectId={projectId}
  128. icon={icon}
  129. name={name}
  130. link={`/project/${projectId}/dataGovernance/${routes[0]}`}
  131. />
  132. )
  133. }
  134. )
  135. return <Sidebar>{sidebarOptions}</Sidebar>
  136. }, [currentProject?.id, nextPermission, pathname])
  137. return (
  138. <div className={styles.sidebar}>
  139. {sidebar}
  140. <div className={styles.content}>{props.children}</div>
  141. </div>
  142. )
  143. }
  144. export default MainSidebar