Skip to content

Commit 0402b46

Browse files
committed
optimize(projects): optimize code
1 parent 8f9a86c commit 0402b46

File tree

7 files changed

+59
-53
lines changed

7 files changed

+59
-53
lines changed

src/components/advanced/DragContent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const reorder = (list: AntDesign.TableColumnCheck[], startIndex: number, endInde
1414
result[endIndex] = list[startIndex];
1515
return result;
1616
};
17+
1718
const DragContent: FC<Props> = ({ columns, setColumnChecks }) => {
1819
const dragEnd: OnDragEndResponder = result => {
1920
if (!result.destination) {

src/layouts/blank-layout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import GlobalContent from '../modules/global-content';
22

33
export function Component() {
4-
return <GlobalContent showPadding={true} />;
4+
return <GlobalContent closePadding={true} />;
55
}
Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { Breadcrumb } from 'antd';
2-
import { createElement } from 'react';
2+
import { cloneElement } from 'react';
33
import type { MenuInfo } from 'rc-menu/lib/interface';
4-
import type { ItemType, MenuItemType, SubMenuType } from 'antd/es/menu/interface';
4+
import type { MenuItemType } from 'antd/es/menu/interface';
55
import type { BreadcrumbProps } from 'antd';
6-
import type { Route } from '@sa/simple-router';
76
import { useRouterPush } from '@/hooks/common/routerPush';
7+
import { getBreadcrumbsByRoute } from './shared';
8+
9+
function BreadcrumbContent({ label, icon }: { label: JSX.Element; icon: JSX.Element }) {
10+
return (
11+
<div className="i-flex-y-center align-middle">
12+
{cloneElement(icon, { className: 'mr-4px text-icon', ...icon.props })}
13+
{label}
14+
</div>
15+
);
16+
}
817

918
const GlobalBreadcrumb: FC<Omit<BreadcrumbProps, 'items'>> = memo(props => {
10-
const { allMenus: menus } = useMixMenuContext();
11-
const route = useRoute();
19+
const { allMenus: menus, route } = useMixMenuContext();
20+
1221
const routerPush = useRouterPush();
22+
1323
const breadcrumb = getBreadcrumbsByRoute(route, menus);
1424

1525
function handleClickMenu(menuInfo: MenuInfo) {
@@ -46,47 +56,4 @@ const GlobalBreadcrumb: FC<Omit<BreadcrumbProps, 'items'>> = memo(props => {
4656
);
4757
});
4858

49-
function BreadcrumbContent({ label, icon }: { label: JSX.Element; icon: JSX.Element }) {
50-
return (
51-
<div className="i-flex-y-center align-middle">
52-
{createElement(icon.type, { className: 'mr-4px text-icon', ...icon.props })}
53-
{label}
54-
</div>
55-
);
56-
}
57-
58-
function removeChildren(menu: SubMenuType): Omit<ItemType, 'children'> {
59-
const { children: _, ...rest } = menu;
60-
61-
return {
62-
...rest
63-
};
64-
}
65-
// eslint-disable-next-line max-params
66-
function getBreadcrumbsByRoute(
67-
route: Route,
68-
menus: ItemType[],
69-
index: number = 0,
70-
breadcrumbs: Extract<ItemType, MenuItemType | SubMenuType>[] = []
71-
) {
72-
const currentMenu = menus.find(item => item?.key === route.matched[index]?.name) as SubMenuType | undefined;
73-
74-
if (currentMenu) {
75-
const flattenedChildren = currentMenu.children?.map(item => {
76-
if (item && 'children' in item) {
77-
return removeChildren(item as SubMenuType);
78-
}
79-
return item;
80-
}) as MenuItemType[];
81-
82-
breadcrumbs.push({ ...currentMenu, children: flattenedChildren });
83-
84-
if (index < route.matched.length - 1) {
85-
getBreadcrumbsByRoute(route, currentMenu.children || [], index + 1, breadcrumbs);
86-
}
87-
}
88-
89-
return breadcrumbs;
90-
}
91-
9259
export default GlobalBreadcrumb;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { Route } from '@sa/simple-router';
2+
import type { ItemType, MenuItemType, SubMenuType } from 'antd/es/menu/interface';
3+
4+
function removeChildren(menu: SubMenuType): Omit<ItemType, 'children'> {
5+
const { children: _, ...rest } = menu;
6+
7+
return {
8+
...rest
9+
};
10+
}
11+
// eslint-disable-next-line max-params
12+
export function getBreadcrumbsByRoute(
13+
route: Route,
14+
menus: ItemType[],
15+
index: number = 0,
16+
breadcrumbs: Extract<ItemType, MenuItemType | SubMenuType>[] = []
17+
) {
18+
const currentMenu = menus.find(item => item?.key === route.matched[index]?.name) as SubMenuType | undefined;
19+
20+
if (currentMenu) {
21+
const flattenedChildren = currentMenu.children?.map(item => {
22+
if (item && 'children' in item) {
23+
return removeChildren(item as SubMenuType);
24+
}
25+
return item;
26+
}) as MenuItemType[];
27+
28+
breadcrumbs.push({ ...currentMenu, children: flattenedChildren });
29+
30+
if (index < route.matched.length - 1) {
31+
getBreadcrumbsByRoute(route, currentMenu.children || [], index + 1, breadcrumbs);
32+
}
33+
}
34+
35+
return breadcrumbs;
36+
}

src/layouts/modules/global-content/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getReloadFlag, setContentXScrollable } from '@/store/slice/app';
77

88
interface Props {
99
/** Show padding for content */
10-
showPadding?: boolean;
10+
closePadding?: boolean;
1111
}
1212

1313
function resetScroll() {
@@ -16,7 +16,7 @@ function resetScroll() {
1616
el?.scrollTo({ left: 0, top: 0 });
1717
}
1818

19-
const GlobalContent: FC<Props> = memo(({ showPadding }) => {
19+
const GlobalContent: FC<Props> = memo(({ closePadding }) => {
2020
const currentOutlet = useOutlet();
2121
const location = useLocation();
2222
const reloadFlag = useAppSelector(getReloadFlag);
@@ -26,7 +26,7 @@ const GlobalContent: FC<Props> = memo(({ showPadding }) => {
2626
return (
2727
<SwitchTransition mode="out-in">
2828
<CSSTransition
29-
key={reloadFlag ? location.pathname : 'reload-page'}
29+
key={reloadFlag ? location.key : 'reload-page'}
3030
nodeRef={nodeRef}
3131
timeout={300}
3232
onExit={() => dispatch(setContentXScrollable(true))}
@@ -36,7 +36,7 @@ const GlobalContent: FC<Props> = memo(({ showPadding }) => {
3636
unmountOnExit
3737
>
3838
<div
39-
className={ClassNames('h-full flex-grow bg-layout ', { 'p-16px': !showPadding })}
39+
className={ClassNames('h-full flex-grow bg-layout ', { 'p-16px': !closePadding })}
4040
ref={nodeRef}
4141
>
4242
{reloadFlag && currentOutlet}

src/layouts/modules/global-footer/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import DarkModeContainer from '@/components/stateless/common/DarkModeContainer';
2+
23
const GlobalFooter = memo(() => {
34
return (
45
<DarkModeContainer className="h-full flex-center">

src/layouts/modules/global-header/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const GlobalHeader: FC<Props> = memo(({ showLogo, showMenuToggler, showMenu, isM
3131
style={{ width: `${settings.sider.width}px` }}
3232
/>
3333
)}
34+
3435
{showMenuToggler && <MenuToggler />}
3536

3637
<div

0 commit comments

Comments
 (0)