From 4b55f0e8912ad69c9cfc122fbbc7166644337c11 Mon Sep 17 00:00:00 2001 From: goodmice Date: Tue, 19 Apr 2022 12:51:30 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=20=D1=80=D0=BE=D1=83=D1=82=D0=B8=D0=BD=D0=B3.=20=D0=94=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BA=D0=BE=D0=BC=D0=BF?= =?UTF-8?q?=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=20PrivateMenu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Private/PrivateMenu.tsx | 45 +++++++++++++++++++ src/components/Private/PrivateMenuItem.tsx | 10 ++--- src/components/Private/index.ts | 10 +++-- src/pages/AdminPanel/Telemetry/index.jsx | 12 ++--- src/pages/AdminPanel/index.jsx | 28 ++++++------ .../Analytics/WellCompositeEditor/index.jsx | 12 ++--- src/pages/Analytics/index.jsx | 12 ++--- src/pages/Documents/index.jsx | 12 +++-- src/pages/Telemetry/index.jsx | 17 +++---- src/pages/TelemetryAnalysis/index.jsx | 17 ++++--- src/pages/Well.jsx | 24 +++++----- src/pages/WellOperations/index.jsx | 20 ++++----- 12 files changed, 132 insertions(+), 87 deletions(-) create mode 100644 src/components/Private/PrivateMenu.tsx diff --git a/src/components/Private/PrivateMenu.tsx b/src/components/Private/PrivateMenu.tsx new file mode 100644 index 0000000..1a06311 --- /dev/null +++ b/src/components/Private/PrivateMenu.tsx @@ -0,0 +1,45 @@ +import { join } from 'path' +import { Menu, MenuItemProps, MenuProps } from 'antd' +import { Children, cloneElement, memo, ReactElement } from 'react' +import { Link, useLocation } from 'react-router-dom' + +import { isURLAvailable } from '@utils/permissions' + +export type PrivateMenuProps = MenuProps & { root?: string } + +export type PrivateMenuLinkProps = MenuItemProps & { + tabName?: string + path?: string + title: string + visible?: boolean +} + +export const PrivateMenuLink = memo(({ tabName = '', path = '', title, ...other }) => { + const location = useLocation() + return ( + + {title} + + ) +}) + +const PrivateMenuMain = memo(({ root = '', children, ...other }) => { + const items = Children.toArray(children).map((child) => { + const element = child as ReactElement + let key = element.key?.toString() + const visible: boolean | undefined = element.props.visible + if (key && visible !== false) { + key = key.slice(key.lastIndexOf('$') + 1) // Ключ автоматический преобразуется в "(.+)\$ключ" + const path = join(root, key) + if (visible || isURLAvailable(path)) + return cloneElement(element, { key, path, tabName: key }) + } + return null + }) + + return +}) + +export const PrivateMenu = Object.assign(PrivateMenuMain, { Link: PrivateMenuLink }) + +export default PrivateMenu diff --git a/src/components/Private/PrivateMenuItem.tsx b/src/components/Private/PrivateMenuItem.tsx index fd48ee9..1d0d101 100755 --- a/src/components/Private/PrivateMenuItem.tsx +++ b/src/components/Private/PrivateMenuItem.tsx @@ -1,22 +1,22 @@ import { join } from 'path' import { Menu, MenuItemProps } from 'antd' import { memo, NamedExoticComponent } from 'react' +import { Link, useLocation } from 'react-router-dom' import { isURLAvailable } from '@utils/permissions' -import { Link, useLocation } from 'react-router-dom' export type PrivateMenuItemProps = MenuItemProps & { root: string path: string } -export type PrivateMenuLinkProps = MenuItemProps & { +export type PrivateMenuItemLinkProps = MenuItemProps & { root?: string path: string title: string } -export const PrivateMenuItemLink = memo(({ root = '', path, title, ...other }) => { +export const PrivateMenuItemLink = memo(({ root = '', path, title, ...other }) => { const location = useLocation() return ( @@ -26,9 +26,9 @@ export const PrivateMenuItemLink = memo(({ root = '', path }) export const PrivateMenuItem: NamedExoticComponent & { - Link: NamedExoticComponent + Link: NamedExoticComponent } = Object.assign(memo(({ root, path, ...other }) => - isURLAvailable(join(root, path)) ? : null +