123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /*
- * <<
- * Davinci
- * ==
- * Copyright (C) 2016 - 2017 EDP
- * ==
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * >>
- */
- import React, { useEffect, useMemo, PropsWithChildren } from 'react'
- import { useDispatch, useSelector } from 'react-redux'
- import { useLocation, matchPath, useHistory } from 'react-router-dom'
- import { showNavigator } from 'containers/App/actions'
- import { makeSelectCurrentProject } from 'containers/Projects/selectors'
- import SidebarOption from 'components/SidebarOption'
- import Sidebar from 'components/Sidebar'
- import { SidebarPermissions } from '../Main/constants'
- import { IRouteParams } from 'utils/types'
- import useProjectPermission from '../Projects/hooks/projectPermission'
- import styles from '../Main/Main.less'
- import { IProjectPermission } from 'containers/Projects/types'
- import isEmpty from 'lodash/isEmpty'
- const sidebarSource: Array<{
- icon: React.ReactNode
- name: string
- routes: string[]
- permissionName: typeof SidebarPermissions[number]
- }> = [
- {
- icon: <img src={require('assets/images/sjzl.svg')} />,
- name: '数据总览',
- routes: ['dataOverviiews'],
- permissionName: 'dataOverviiewPermission'
- },
- {
- icon: <img src={require('assets/images/sjzc.svg')} />,
- name: '数据资产',
- routes: ['views'],
- permissionName: 'viewPermission'
- },
- {
- icon: <img src={require('assets/images/sjy.svg')} />,
- name: '数据源',
- routes: ['sources'],
- permissionName: 'sourcePermission'
- },
- {
- icon: <img src={require('assets/images/dictionary.svg')} />,
- name: '数据字典',
- routes: ['dataDictionarys'],
- permissionName: 'dataDictionaryPermission'
- }
- ]
- const MainSidebar: React.FC<PropsWithChildren<{}>> = (props) => {
- const dispatch = useDispatch()
- useEffect(() => {
- dispatch(showNavigator())
- }, [])
- const location = useLocation()
- const { pathname } = location
- const history = useHistory()
- const currentProject = useSelector(makeSelectCurrentProject())
- // @ts-ignore
- const nextPermission = useMemo<IProjectPermission>(
- () => ({
- ...(currentProject?.permission ?? {}),
- ...{
- dataOverviiewPermission: 3,
- dataDictionaryPermission: 3,
- makeRulePermission: 3,
- qualityAuditPermission: 3,
- auditAnalysisPermission: 3
- }
- }),
- [currentProject?.permission]
- )
- useEffect(() => {
- if (!currentProject) {
- return
- }
- const { id: projectId } = currentProject
- const match = matchPath<IRouteParams>(pathname, {
- path: `/project/:projectId/dataManager`,
- exact: true,
- strict: false
- })
- if (match) {
- const hasPermission = SidebarPermissions.some((sidebarPermission) => {
- if (nextPermission[sidebarPermission] > 0) {
- const path = sidebarPermission.slice(0, -10)
- history.replace(`/project/${projectId}/dataManager/${path}s`)
- return true
- }
- })
- !hasPermission && history.replace('/noAuthorization')
- }
- }, [pathname, currentProject?.id, nextPermission])
- const AuthorizedSidebarOptions = useProjectPermission(
- SidebarOption,
- sidebarSource.map(({ permissionName }) => permissionName)
- )
- const sidebar = useMemo(() => {
- if (!currentProject) {
- return null
- }
- if (isEmpty(nextPermission)) {
- return null
- }
- const { id: projectId } = currentProject
- const vizOnly = SidebarPermissions.every((permissionName) =>
- permissionName === 'vizPermission'
- ? nextPermission[permissionName]
- : !nextPermission[permissionName]
- )
- if (vizOnly) {
- return null
- }
- const sidebarOptions = sidebarSource.map(
- ({ permissionName, routes, icon, name }, idx) => {
- if (!nextPermission[permissionName]) {
- return null
- }
- const active = routes.some((route) => pathname.includes(route))
- const AuthorizedSidebarOption = AuthorizedSidebarOptions[idx]
- return (
- <AuthorizedSidebarOption
- key={permissionName}
- active={active}
- indexRoute={routes[0]}
- projectId={projectId}
- icon={icon}
- name={name}
- link={`/project/${projectId}/dataManager/${routes[0]}`}
- />
- )
- }
- )
- return <Sidebar>{sidebarOptions}</Sidebar>
- }, [currentProject?.id, nextPermission, pathname])
- return (
- <div className={styles.sidebar}>
- {sidebar}
- <div className={styles.content}>{props.children}</div>
- </div>
- )
- }
- export default MainSidebar
|