From 0402b46ee920530fc0b30a363b51425eb2cf8cb8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-31IBRMI\\Administrator" <1509326266@qq.com> Date: Fri, 6 Sep 2024 13:38:16 +0800 Subject: [PATCH] optimize(projects): optimize code --- src/components/advanced/DragContent.tsx | 1 + src/layouts/blank-layout/index.tsx | 2 +- .../modules/global-breadcrumb/index.tsx | 63 +++++-------------- .../modules/global-breadcrumb/shared.ts | 36 +++++++++++ src/layouts/modules/global-content/index.tsx | 8 +-- src/layouts/modules/global-footer/index.tsx | 1 + src/layouts/modules/global-header/index.tsx | 1 + 7 files changed, 59 insertions(+), 53 deletions(-) create mode 100644 src/layouts/modules/global-breadcrumb/shared.ts diff --git a/src/components/advanced/DragContent.tsx b/src/components/advanced/DragContent.tsx index 7a81379..a8a8fcd 100644 --- a/src/components/advanced/DragContent.tsx +++ b/src/components/advanced/DragContent.tsx @@ -14,6 +14,7 @@ const reorder = (list: AntDesign.TableColumnCheck[], startIndex: number, endInde result[endIndex] = list[startIndex]; return result; }; + const DragContent: FC = ({ columns, setColumnChecks }) => { const dragEnd: OnDragEndResponder = result => { if (!result.destination) { diff --git a/src/layouts/blank-layout/index.tsx b/src/layouts/blank-layout/index.tsx index 91ec50c..7e3a90c 100644 --- a/src/layouts/blank-layout/index.tsx +++ b/src/layouts/blank-layout/index.tsx @@ -1,5 +1,5 @@ import GlobalContent from '../modules/global-content'; export function Component() { - return ; + return ; } diff --git a/src/layouts/modules/global-breadcrumb/index.tsx b/src/layouts/modules/global-breadcrumb/index.tsx index e6629d6..d0e60f1 100644 --- a/src/layouts/modules/global-breadcrumb/index.tsx +++ b/src/layouts/modules/global-breadcrumb/index.tsx @@ -1,15 +1,25 @@ import { Breadcrumb } from 'antd'; -import { createElement } from 'react'; +import { cloneElement } from 'react'; import type { MenuInfo } from 'rc-menu/lib/interface'; -import type { ItemType, MenuItemType, SubMenuType } from 'antd/es/menu/interface'; +import type { MenuItemType } from 'antd/es/menu/interface'; import type { BreadcrumbProps } from 'antd'; -import type { Route } from '@sa/simple-router'; import { useRouterPush } from '@/hooks/common/routerPush'; +import { getBreadcrumbsByRoute } from './shared'; + +function BreadcrumbContent({ label, icon }: { label: JSX.Element; icon: JSX.Element }) { + return ( +
+ {cloneElement(icon, { className: 'mr-4px text-icon', ...icon.props })} + {label} +
+ ); +} const GlobalBreadcrumb: FC> = memo(props => { - const { allMenus: menus } = useMixMenuContext(); - const route = useRoute(); + const { allMenus: menus, route } = useMixMenuContext(); + const routerPush = useRouterPush(); + const breadcrumb = getBreadcrumbsByRoute(route, menus); function handleClickMenu(menuInfo: MenuInfo) { @@ -46,47 +56,4 @@ const GlobalBreadcrumb: FC> = memo(props => { ); }); -function BreadcrumbContent({ label, icon }: { label: JSX.Element; icon: JSX.Element }) { - return ( -
- {createElement(icon.type, { className: 'mr-4px text-icon', ...icon.props })} - {label} -
- ); -} - -function removeChildren(menu: SubMenuType): Omit { - const { children: _, ...rest } = menu; - - return { - ...rest - }; -} -// eslint-disable-next-line max-params -function getBreadcrumbsByRoute( - route: Route, - menus: ItemType[], - index: number = 0, - breadcrumbs: Extract[] = [] -) { - const currentMenu = menus.find(item => item?.key === route.matched[index]?.name) as SubMenuType | undefined; - - if (currentMenu) { - const flattenedChildren = currentMenu.children?.map(item => { - if (item && 'children' in item) { - return removeChildren(item as SubMenuType); - } - return item; - }) as MenuItemType[]; - - breadcrumbs.push({ ...currentMenu, children: flattenedChildren }); - - if (index < route.matched.length - 1) { - getBreadcrumbsByRoute(route, currentMenu.children || [], index + 1, breadcrumbs); - } - } - - return breadcrumbs; -} - export default GlobalBreadcrumb; diff --git a/src/layouts/modules/global-breadcrumb/shared.ts b/src/layouts/modules/global-breadcrumb/shared.ts new file mode 100644 index 0000000..efe7360 --- /dev/null +++ b/src/layouts/modules/global-breadcrumb/shared.ts @@ -0,0 +1,36 @@ +import type { Route } from '@sa/simple-router'; +import type { ItemType, MenuItemType, SubMenuType } from 'antd/es/menu/interface'; + +function removeChildren(menu: SubMenuType): Omit { + const { children: _, ...rest } = menu; + + return { + ...rest + }; +} +// eslint-disable-next-line max-params +export function getBreadcrumbsByRoute( + route: Route, + menus: ItemType[], + index: number = 0, + breadcrumbs: Extract[] = [] +) { + const currentMenu = menus.find(item => item?.key === route.matched[index]?.name) as SubMenuType | undefined; + + if (currentMenu) { + const flattenedChildren = currentMenu.children?.map(item => { + if (item && 'children' in item) { + return removeChildren(item as SubMenuType); + } + return item; + }) as MenuItemType[]; + + breadcrumbs.push({ ...currentMenu, children: flattenedChildren }); + + if (index < route.matched.length - 1) { + getBreadcrumbsByRoute(route, currentMenu.children || [], index + 1, breadcrumbs); + } + } + + return breadcrumbs; +} diff --git a/src/layouts/modules/global-content/index.tsx b/src/layouts/modules/global-content/index.tsx index 3f289f8..ccfd33c 100644 --- a/src/layouts/modules/global-content/index.tsx +++ b/src/layouts/modules/global-content/index.tsx @@ -7,7 +7,7 @@ import { getReloadFlag, setContentXScrollable } from '@/store/slice/app'; interface Props { /** Show padding for content */ - showPadding?: boolean; + closePadding?: boolean; } function resetScroll() { @@ -16,7 +16,7 @@ function resetScroll() { el?.scrollTo({ left: 0, top: 0 }); } -const GlobalContent: FC = memo(({ showPadding }) => { +const GlobalContent: FC = memo(({ closePadding }) => { const currentOutlet = useOutlet(); const location = useLocation(); const reloadFlag = useAppSelector(getReloadFlag); @@ -26,7 +26,7 @@ const GlobalContent: FC = memo(({ showPadding }) => { return ( dispatch(setContentXScrollable(true))} @@ -36,7 +36,7 @@ const GlobalContent: FC = memo(({ showPadding }) => { unmountOnExit >
{reloadFlag && currentOutlet} diff --git a/src/layouts/modules/global-footer/index.tsx b/src/layouts/modules/global-footer/index.tsx index d8d04b6..464e2b2 100644 --- a/src/layouts/modules/global-footer/index.tsx +++ b/src/layouts/modules/global-footer/index.tsx @@ -1,4 +1,5 @@ import DarkModeContainer from '@/components/stateless/common/DarkModeContainer'; + const GlobalFooter = memo(() => { return ( diff --git a/src/layouts/modules/global-header/index.tsx b/src/layouts/modules/global-header/index.tsx index 785773c..afef959 100644 --- a/src/layouts/modules/global-header/index.tsx +++ b/src/layouts/modules/global-header/index.tsx @@ -31,6 +31,7 @@ const GlobalHeader: FC = memo(({ showLogo, showMenuToggler, showMenu, isM style={{ width: `${settings.sider.width}px` }} /> )} + {showMenuToggler && }