From 88c66bd7a77a7a92ffa2eb79b23ac0c0022e8aa8 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Mon, 8 Jan 2024 13:43:47 +0800 Subject: [PATCH] Optimize code --- src/components/common/Indicator.tsx | 2 +- src/components/common/Sidebar/Item.tsx | 10 +++--- src/components/common/Sidebar/index.tsx | 2 +- src/pages/System/Group.tsx | 4 +-- src/pages/System/Role.tsx | 4 +-- src/pages/System/Settings/SensitiveWord.tsx | 4 +-- src/pages/System/Settings/index.tsx | 4 +-- src/pages/System/User.tsx | 36 ++++++++++----------- src/pages/SystemFramework.tsx | 20 ++++++------ src/pages/ToolsFramework.tsx | 21 ++++++------ src/pages/UserFramework.tsx | 24 +++++++------- src/util/route.tsx | 7 ++-- 12 files changed, 68 insertions(+), 70 deletions(-) diff --git a/src/components/common/Indicator.tsx b/src/components/common/Indicator.tsx index a00213e..1b56baa 100644 --- a/src/components/common/Indicator.tsx +++ b/src/components/common/Indicator.tsx @@ -11,7 +11,7 @@ interface IndicatorProps { const Indicator: React.FC = ({ total, current, onSwitch }) => { const handleClick = (index: number) => { return () => { - onSwitch && onSwitch(index) + onSwitch?.(index) } } diff --git a/src/components/common/Sidebar/Item.tsx b/src/components/common/Sidebar/Item.tsx index 87bd2ea..289b746 100644 --- a/src/components/common/Sidebar/Item.tsx +++ b/src/components/common/Sidebar/Item.tsx @@ -16,7 +16,7 @@ const Item: React.FC = (props) => { const showSubmenu = (e: React.MouseEvent) => { const parentElement = e.currentTarget.parentElement - if (parentElement && parentElement.childElementCount === 2) { + if (parentElement?.childElementCount === 2) { const parentClientRect = parentElement.getBoundingClientRect() if (parentClientRect.top <= screen.height / 2) { setSubmenuTop(parentClientRect.top) @@ -42,18 +42,16 @@ const Item: React.FC = (props) => { } >
- {props.icon ? ( - - ) : undefined} + {props.icon && }
{props.text} - {props.children ? ( + {props.children && ( {props.children} - ) : undefined} + )} ) } diff --git a/src/components/common/Sidebar/index.tsx b/src/components/common/Sidebar/index.tsx index f12716b..ae45063 100644 --- a/src/components/common/Sidebar/index.tsx +++ b/src/components/common/Sidebar/index.tsx @@ -29,7 +29,7 @@ const Sidebar: React.FC & { const switchSidebar = () => { setLocalStorage('HIDE_SIDEBAR', !hideSidebar ? 'true' : 'false') setHideSidebar(!hideSidebar) - props.onSidebarSwitch && props.onSidebarSwitch(hideSidebar) + props.onSidebarSwitch?.(hideSidebar) } return ( diff --git a/src/pages/System/Group.tsx b/src/pages/System/Group.tsx index 309565f..0340fce 100644 --- a/src/pages/System/Group.tsx +++ b/src/pages/System/Group.tsx @@ -529,9 +529,9 @@ const Group: React.FC = () => { } suffix={ <> - {!isRegexLegal ? ( + {!isRegexLegal && ( 非法表达式 - ) : undefined} + )} .* diff --git a/src/pages/System/Role.tsx b/src/pages/System/Role.tsx index 4e2a400..dc059aa 100644 --- a/src/pages/System/Role.tsx +++ b/src/pages/System/Role.tsx @@ -538,9 +538,9 @@ const Role: React.FC = () => { } suffix={ <> - {!isRegexLegal ? ( + {!isRegexLegal && ( 非法表达式 - ) : undefined} + )} .* diff --git a/src/pages/System/Settings/SensitiveWord.tsx b/src/pages/System/Settings/SensitiveWord.tsx index b2eebec..429b741 100644 --- a/src/pages/System/Settings/SensitiveWord.tsx +++ b/src/pages/System/Settings/SensitiveWord.tsx @@ -123,7 +123,7 @@ const SensitiveWord: React.FC = () => { justifyContent: 'flex-end' }} > - {selectedKeys?.length === 1 ? ( + {selectedKeys?.length === 1 && ( { onClick={handleOnDelete} /> - ) : undefined} + )} 备选 , '拦截' diff --git a/src/pages/System/Settings/index.tsx b/src/pages/System/Settings/index.tsx index ca5ea51..f411aac 100644 --- a/src/pages/System/Settings/index.tsx +++ b/src/pages/System/Settings/index.tsx @@ -27,7 +27,7 @@ export const SettingsCard: React.FC = (props) => {
{props.title}
- {!props.loading ? ( + {!props.loading && ( {props.expand} @@ -37,7 +37,7 @@ export const SettingsCard: React.FC = (props) => { - ) : undefined} + )}
} diff --git a/src/pages/System/User.tsx b/src/pages/System/User.tsx index 0aa934b..fbf2761 100644 --- a/src/pages/System/User.tsx +++ b/src/pages/System/User.tsx @@ -169,22 +169,20 @@ const User: React.FC = () => { 正常 ) : ( <> - {record.verify ? ( + {record.verify && ( <> 未验证 - ) : undefined} - {record.locking ? 锁定 : undefined} - {record.expiration && isPastTime(record.expiration) ? ( + )} + {record.locking && 锁定} + {record.expiration && isPastTime(record.expiration) && ( 过期 - ) : undefined} + )} {record.credentialsExpiration && - isPastTime(record.credentialsExpiration) ? ( - 改密 - ) : undefined} - {!record.enable ? 禁用 : undefined} + isPastTime(record.credentialsExpiration) && 改密} + {!record.enable && 禁用} )} @@ -215,14 +213,14 @@ const User: React.FC = () => { - {record.id !== '0' ? ( + {record.id !== '0' && ( 删除 - ) : undefined} + )} @@ -378,7 +376,7 @@ const User: React.FC = () => { > - {value.id !== '0' ? ( + {value.id !== '0' && ( { > - ) : undefined} + )} ), onOk: () => @@ -803,7 +801,7 @@ const User: React.FC = () => { > - {!isDrawerEdit ? ( + {!isDrawerEdit && ( <> { - ) : undefined} + )} @@ -824,7 +822,7 @@ const User: React.FC = () => { > - {formValues?.id !== '0' ? ( + {formValues?.id !== '0' && ( <> { - ) : undefined} + )} ) @@ -943,9 +941,9 @@ const User: React.FC = () => { } suffix={ <> - {!isRegexLegal ? ( + {!isRegexLegal && ( 非法表达式 - ) : undefined} + )} .* diff --git a/src/pages/SystemFramework.tsx b/src/pages/SystemFramework.tsx index fbfc2b0..a47a786 100644 --- a/src/pages/SystemFramework.tsx +++ b/src/pages/SystemFramework.tsx @@ -13,15 +13,17 @@ const SystemFramework: React.FC = () => { {getSystemRouteJson().map((value) => { - return value.menu ? ( - - ) : undefined + return ( + value.menu && ( + + ) + ) })} diff --git a/src/pages/ToolsFramework.tsx b/src/pages/ToolsFramework.tsx index be7ff21..42e5ec2 100644 --- a/src/pages/ToolsFramework.tsx +++ b/src/pages/ToolsFramework.tsx @@ -42,17 +42,16 @@ const ToolsFramework: React.FC = () => { text={tool.name} key={tool.id} > - {tool.children - ? tool.children.map((subTool) => { - return ( - - ) - }) - : undefined} + {tool.children && + tool.children.map((subTool) => { + return ( + + ) + })} ) : undefined })} diff --git a/src/pages/UserFramework.tsx b/src/pages/UserFramework.tsx index 75a5577..3c9361a 100644 --- a/src/pages/UserFramework.tsx +++ b/src/pages/UserFramework.tsx @@ -15,13 +15,13 @@ const ToolsFramework: React.FC = () => { title={'个人中心'} bottomFixed={ - {hasPathPermission('/system') ? ( + {hasPathPermission('/system') && ( - ) : undefined} + )} { > {user.map((value) => { - return value.menu ? ( - - ) : undefined + return ( + value.menu && ( + + ) + ) })} diff --git a/src/util/route.tsx b/src/util/route.tsx index 7812af8..0899079 100644 --- a/src/util/route.tsx +++ b/src/util/route.tsx @@ -7,10 +7,9 @@ export const getRedirectUrl = (path: string, redirectUrl: string): string => { export const getFullTitle = (data: _DataNode, preTitle?: string) => { data.fullTitle = `${preTitle ? `${preTitle}-` : ''}${data.title as string}` - data.children && - data.children.forEach((value) => { - getFullTitle(value, data.fullTitle) - }) + data.children?.forEach((value) => { + getFullTitle(value, data.fullTitle) + }) return data }