fix(layout): 修复顶栏连点和头像菜单抖动,统一三中心侧栏加载态
This commit is contained in:
@@ -4,12 +4,15 @@ import App from '../../App';
|
||||
import React, {
|
||||
useContext,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useState,
|
||||
lazy,
|
||||
Suspense,
|
||||
useRef,
|
||||
} from 'react';
|
||||
import { useIsMobile } from '../../hooks/common/useIsMobile';
|
||||
import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed';
|
||||
import { CenterSidebarLoading } from './components/CenterSidebarLoading';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API } from '../../helpers/api';
|
||||
import { isAuthPagePath } from '../../helpers/auth';
|
||||
@@ -62,6 +65,7 @@ const PageLayout = () => {
|
||||
const [userState, userDispatch] = useContext(UserContext);
|
||||
const [, statusDispatch] = useContext(StatusContext);
|
||||
const isMobile = useIsMobile();
|
||||
const [collapsed] = useSidebarCollapsed();
|
||||
const [drawerOpen, setDrawerOpen] = useState(false);
|
||||
const { i18n } = useTranslation();
|
||||
const location = useLocation();
|
||||
@@ -120,6 +124,10 @@ const PageLayout = () => {
|
||||
const headerBarHeight = isMobile ? 56 : 72;
|
||||
const totalHeaderHeight = headerBarHeight + marqueeHeight;
|
||||
|
||||
useLayoutEffect(() => {
|
||||
document.body.classList.toggle('sidebar-collapsed', collapsed);
|
||||
}, [collapsed]);
|
||||
|
||||
// 滚动容器引用
|
||||
const scrollContainerRef = useRef(null);
|
||||
const authBootstrappedRef = useRef(false);
|
||||
@@ -155,7 +163,7 @@ const PageLayout = () => {
|
||||
// 顶栏总高(顶栏 + 跑马灯)发布成全局 CSS 变量。
|
||||
// 顶栏是 position:fixed,内容区从视口 0 开始流动,任何 `sticky top-0` 的页内元素
|
||||
// 都会吸到顶栏底下被盖住。页面无从得知跑马灯开没开,只能由这里统一给出。
|
||||
useEffect(() => {
|
||||
useLayoutEffect(() => {
|
||||
const root = document.documentElement;
|
||||
root.style.setProperty('--app-header-height', `${totalHeaderHeight}px`);
|
||||
}, [totalHeaderHeight]);
|
||||
@@ -408,7 +416,11 @@ const PageLayout = () => {
|
||||
: undefined,
|
||||
}}
|
||||
>
|
||||
<Suspense fallback={null}>
|
||||
<Suspense
|
||||
fallback={
|
||||
<CenterSidebarLoading forceExpanded={siderForceExpanded} />
|
||||
}
|
||||
>
|
||||
<SiderBar
|
||||
forceExpanded={siderForceExpanded}
|
||||
onNavigate={() => {
|
||||
|
||||
@@ -13,7 +13,6 @@ import '../../styles/center-layout.css';
|
||||
import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed';
|
||||
import { useSidebar } from '../../hooks/common/useSidebar';
|
||||
import { usePartnerWorkspaceEntry } from '../../hooks/common/usePartnerWorkspaceEntry';
|
||||
import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime';
|
||||
import { isAdmin, isRoot, showError } from '../../helpers';
|
||||
import {
|
||||
isMiniProgramPaymentPath,
|
||||
@@ -29,7 +28,7 @@ import {
|
||||
loadPersonalConsentGranted,
|
||||
readPersonalConsentCache,
|
||||
} from '../smart-router/api';
|
||||
import SkeletonWrapper from './components/SkeletonWrapper';
|
||||
import { CenterSidebarLoading } from './components/CenterSidebarLoading';
|
||||
import { CLOUD_BRAIN_UI_ENABLED } from '../../config/cloudBrainUi';
|
||||
import { useAuthzView, usePerm } from '../../authz';
|
||||
|
||||
@@ -77,8 +76,6 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => {
|
||||
isRoot() || (isAdmin() && delegatedSettingTabs.length > 0);
|
||||
const commissionEnabled = statusState?.status?.commission_enabled !== false;
|
||||
|
||||
const showSkeleton = useMinimumLoadingTime(sidebarLoading, 200);
|
||||
|
||||
// 权限管理分组:模块切到新体系后按权限点判断,切换前只有 root 可见(legacy = isRoot())
|
||||
const canAuthzMenu = usePerm('platform.authz.menu.query', isRoot());
|
||||
const canAuthzRole = usePerm('platform.authz.role.query', isRoot());
|
||||
@@ -577,7 +574,7 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => {
|
||||
|
||||
useEffect(() => {
|
||||
const navigation = navigationRef.current;
|
||||
if (!navigation || showSkeleton) return;
|
||||
if (!navigation || sidebarLoading) return;
|
||||
let frame;
|
||||
const scheduleReveal = () => {
|
||||
cancelAnimationFrame(frame);
|
||||
@@ -599,7 +596,7 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => {
|
||||
mutationObserver.disconnect();
|
||||
};
|
||||
}, [
|
||||
showSkeleton,
|
||||
sidebarLoading,
|
||||
selectedKeys,
|
||||
collapsed,
|
||||
forceExpanded,
|
||||
@@ -607,17 +604,6 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => {
|
||||
foldedGroups,
|
||||
]);
|
||||
|
||||
// 监控折叠状态变化以更新 body class(--sidebar-current-width 由它驱动)。
|
||||
// 抽屉态下跳过:body class 是全局的,抽屉临时展开不该顺手把桌面侧栏宽度也改掉。
|
||||
useEffect(() => {
|
||||
if (forceExpanded) return;
|
||||
if (persistedCollapsed) {
|
||||
document.body.classList.add('sidebar-collapsed');
|
||||
} else {
|
||||
document.body.classList.remove('sidebar-collapsed');
|
||||
}
|
||||
}, [persistedCollapsed, forceExpanded]);
|
||||
|
||||
const renderMenuIcon = (key) =>
|
||||
React.cloneElement(getLucideIcon(key), {
|
||||
size: 16,
|
||||
@@ -735,6 +721,9 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => {
|
||||
);
|
||||
};
|
||||
|
||||
if (sidebarLoading)
|
||||
return <CenterSidebarLoading forceExpanded={forceExpanded} />;
|
||||
|
||||
return (
|
||||
<div
|
||||
className='ui-console-sidebar ui-center-sidebar'
|
||||
@@ -746,55 +735,47 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => {
|
||||
background: 'var(--surface-sidebar)',
|
||||
}}
|
||||
>
|
||||
<SkeletonWrapper
|
||||
loading={showSkeleton}
|
||||
type='sidebar'
|
||||
className=''
|
||||
collapsed={collapsed}
|
||||
showAdmin={isAdmin()}
|
||||
>
|
||||
<nav ref={navigationRef} className='ui-center-sidebar-nav'>
|
||||
{/* 聊天区域 */}
|
||||
{hasSectionVisibleModules('chat') &&
|
||||
renderGroup(
|
||||
'chat-group',
|
||||
t('聊天'),
|
||||
chatMenuItems.map(renderSubItem),
|
||||
)}
|
||||
<nav ref={navigationRef} className='ui-center-sidebar-nav'>
|
||||
{/* 聊天区域 */}
|
||||
{hasSectionVisibleModules('chat') &&
|
||||
renderGroup(
|
||||
'chat-group',
|
||||
t('聊天'),
|
||||
chatMenuItems.map(renderSubItem),
|
||||
)}
|
||||
|
||||
{/* 控制台区域 */}
|
||||
{hasSectionVisibleModules('console') &&
|
||||
renderGroup(
|
||||
'console-group',
|
||||
t('控制台'),
|
||||
workspaceItems.map((item) => renderNavItem(item)),
|
||||
)}
|
||||
{/* 控制台区域 */}
|
||||
{hasSectionVisibleModules('console') &&
|
||||
renderGroup(
|
||||
'console-group',
|
||||
t('控制台'),
|
||||
workspaceItems.map((item) => renderNavItem(item)),
|
||||
)}
|
||||
|
||||
{/* 个人中心区域 */}
|
||||
{hasSectionVisibleModules('personal') &&
|
||||
renderGroup(
|
||||
'personal-group',
|
||||
t('个人中心'),
|
||||
financeItems.map((item) => renderNavItem(item)),
|
||||
)}
|
||||
{/* 个人中心区域 */}
|
||||
{hasSectionVisibleModules('personal') &&
|
||||
renderGroup(
|
||||
'personal-group',
|
||||
t('个人中心'),
|
||||
financeItems.map((item) => renderNavItem(item)),
|
||||
)}
|
||||
|
||||
{/* 管理员区域:按角色、权限下放及菜单配置过滤。分佣管理为子菜单,走 renderSubItem。 */}
|
||||
{(isAdmin() || visibleAdminItems.length > 0) &&
|
||||
renderGroup(
|
||||
'admin-group',
|
||||
t('管理员'),
|
||||
visibleAdminItems.map(renderSubItem),
|
||||
)}
|
||||
{/* 管理员区域:按角色、权限下放及菜单配置过滤。分佣管理为子菜单,走 renderSubItem。 */}
|
||||
{(isAdmin() || visibleAdminItems.length > 0) &&
|
||||
renderGroup(
|
||||
'admin-group',
|
||||
t('管理员'),
|
||||
visibleAdminItems.map(renderSubItem),
|
||||
)}
|
||||
|
||||
{/* 权限管理区域:至少有一项可见时显示 */}
|
||||
{authzItems.length > 0 &&
|
||||
renderGroup(
|
||||
'authz-group',
|
||||
t('权限管理'),
|
||||
authzItems.map((item) => renderNavItem(item)),
|
||||
)}
|
||||
</nav>
|
||||
</SkeletonWrapper>
|
||||
{/* 权限管理区域:至少有一项可见时显示 */}
|
||||
{authzItems.length > 0 &&
|
||||
renderGroup(
|
||||
'authz-group',
|
||||
t('权限管理'),
|
||||
authzItems.map((item) => renderNavItem(item)),
|
||||
)}
|
||||
</nav>
|
||||
|
||||
{/* 底部折叠按钮 —— 抽屉态不提供:收起后抽屉会变成 60px 窄条,等于自废 */}
|
||||
{!forceExpanded && (
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useSidebarCollapsed } from '../../../hooks/common/useSidebarCollapsed';
|
||||
|
||||
const rows = [0, 1, 2, 3, 4, 5];
|
||||
|
||||
export function CenterSidebarLoading({ forceExpanded = false }) {
|
||||
const [collapsed] = useSidebarCollapsed();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<div
|
||||
className='ui-center-sidebar ui-center-sidebar-placeholder'
|
||||
data-collapsed={!forceExpanded && collapsed}
|
||||
data-mobile={forceExpanded || undefined}
|
||||
aria-busy='true'
|
||||
aria-label={t('主导航')}
|
||||
>
|
||||
<div
|
||||
className='ui-center-sidebar-nav ui-center-sidebar-placeholder-rows'
|
||||
aria-hidden='true'
|
||||
>
|
||||
{rows.map((row) => (
|
||||
<div key={row} className='ui-center-menu-item'>
|
||||
<span />
|
||||
<span />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{!forceExpanded && (
|
||||
<div
|
||||
className='ui-center-sidebar-placeholder-footer'
|
||||
aria-hidden='true'
|
||||
>
|
||||
<span />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function CenterRouteLoading() {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<div className='ui-center-route-loading' role='status'>
|
||||
<div className='hidden md:block'>
|
||||
<CenterSidebarLoading />
|
||||
</div>
|
||||
<div className='ui-center-route-loading-body'>{t('加载中...')}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Compass as ActionCompass } from '@/components/common/icons/ActionIcons';
|
||||
import React, { useState } from 'react';
|
||||
import { useLocation, useNavigate } from 'react-router-dom';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { Button, Dropdown } from 'antd';
|
||||
import { ExternalLink } from 'lucide-react';
|
||||
import { isNavigationPathActive } from '../../console/consoleRoutes';
|
||||
@@ -14,9 +14,8 @@ import { isNavigationPathActive } from '../../console/consoleRoutes';
|
||||
*
|
||||
* 目标路径/外链的推导逻辑必须与桌面端 Navigation 保持一致,避免两端跳转不同。
|
||||
*/
|
||||
const MobileNavMenu = ({ mainNavLinks, userState, t }) => {
|
||||
const MobileNavMenu = ({ mainNavLinks, userState, followTopLink, t }) => {
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const [visible, setVisible] = useState(false);
|
||||
|
||||
if (!mainNavLinks || mainNavLinks.length === 0) {
|
||||
@@ -88,7 +87,7 @@ const MobileNavMenu = ({ mainNavLinks, userState, t }) => {
|
||||
if (item.externalLink) {
|
||||
window.open(item.externalLink, '_blank', 'noopener,noreferrer');
|
||||
} else if (item.to) {
|
||||
navigate(item.to);
|
||||
followTopLink(item.to);
|
||||
}
|
||||
},
|
||||
label: (
|
||||
|
||||
@@ -9,7 +9,13 @@ import { useTranslation } from 'react-i18next';
|
||||
import SkeletonWrapper from '../components/SkeletonWrapper';
|
||||
import { isNavigationPathActive } from '../../console/consoleRoutes';
|
||||
|
||||
const Navigation = ({ mainNavLinks, isMobile, isLoading, userState }) => {
|
||||
const Navigation = ({
|
||||
mainNavLinks,
|
||||
isMobile,
|
||||
isLoading,
|
||||
userState,
|
||||
followTopLink,
|
||||
}) => {
|
||||
const location = useLocation();
|
||||
const { t } = useTranslation();
|
||||
|
||||
@@ -61,7 +67,14 @@ const Navigation = ({ mainNavLinks, isMobile, isLoading, userState }) => {
|
||||
.map((item) => item.itemKey),
|
||||
items: link.items.map((subItem) => ({
|
||||
key: subItem.itemKey,
|
||||
label: <Link to={subItem.to}>{subItem.text}</Link>,
|
||||
label: (
|
||||
<Link
|
||||
to={subItem.to}
|
||||
onClick={(event) => followTopLink(subItem.to, event)}
|
||||
>
|
||||
{subItem.text}
|
||||
</Link>
|
||||
),
|
||||
})),
|
||||
}}
|
||||
>
|
||||
@@ -97,6 +110,7 @@ const Navigation = ({ mainNavLinks, isMobile, isLoading, userState }) => {
|
||||
<Link
|
||||
key={link.itemKey}
|
||||
to={targetPath}
|
||||
onClick={(event) => followTopLink(targetPath, event)}
|
||||
className={finalClasses}
|
||||
aria-current={isActive ? 'page' : undefined}
|
||||
>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChevronDown as ActionChevronDown } from '@/components/common/icons/ActionIcons';
|
||||
import React, { useState } from 'react';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { Avatar, Button as AntButton, Dropdown as AntDropdown } from 'antd';
|
||||
import { UserRound, KeyRound, LogOut } from 'lucide-react';
|
||||
import { getUserAvatarUrl } from '../../../helpers';
|
||||
@@ -20,6 +20,7 @@ const AccountMenuLabel = ({ icon, tone, title, description }) => (
|
||||
);
|
||||
|
||||
const MENU_ICON_PROPS = { size: 18, strokeWidth: 1.65, 'aria-hidden': true };
|
||||
const HOVER_DELAY = 100;
|
||||
|
||||
const UserArea = ({
|
||||
userState,
|
||||
@@ -33,7 +34,56 @@ const UserArea = ({
|
||||
navigate,
|
||||
t,
|
||||
}) => {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [openMode, setOpenMode] = useState(null);
|
||||
const triggerRef = useRef(null);
|
||||
const popupRef = useRef(null);
|
||||
const hoverTimer = useRef();
|
||||
const open = openMode !== null;
|
||||
|
||||
const close = () => {
|
||||
clearTimeout(hoverTimer.current);
|
||||
setOpenMode(null);
|
||||
};
|
||||
|
||||
useEffect(() => () => clearTimeout(hoverTimer.current), []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const handleOutsidePress = (event) => {
|
||||
if (
|
||||
!triggerRef.current?.contains(event.target) &&
|
||||
!popupRef.current?.contains(event.target)
|
||||
) {
|
||||
clearTimeout(hoverTimer.current);
|
||||
setOpenMode(null);
|
||||
}
|
||||
};
|
||||
document.addEventListener('pointerdown', handleOutsidePress, true);
|
||||
return () =>
|
||||
document.removeEventListener('pointerdown', handleOutsidePress, true);
|
||||
}, [open]);
|
||||
|
||||
const queueHoverChange = (event, opening) => {
|
||||
if (isMobile || event.pointerType !== 'mouse') return;
|
||||
clearTimeout(hoverTimer.current);
|
||||
if (opening ? open : openMode !== 'hover') return;
|
||||
|
||||
hoverTimer.current = setTimeout(() => {
|
||||
setOpenMode((mode) => {
|
||||
if (opening) return mode ?? 'hover';
|
||||
return mode === 'hover' ? null : mode;
|
||||
});
|
||||
}, HOVER_DELAY);
|
||||
};
|
||||
|
||||
const toggleOnClick = (event) => {
|
||||
clearTimeout(hoverTimer.current);
|
||||
const clickedMode = event.detail === 0 ? 'keyboard' : 'click';
|
||||
// Clicking a hover preview keeps the panel open until the next click or dismissal.
|
||||
setOpenMode((mode) =>
|
||||
mode === null || mode === 'hover' ? clickedMode : null,
|
||||
);
|
||||
};
|
||||
const isActionVisible = (key) => rightActions?.[key] !== false;
|
||||
const hasHeaderModuleConfig =
|
||||
headerNavModules && Object.keys(headerNavModules).length > 0;
|
||||
@@ -117,13 +167,22 @@ const UserArea = ({
|
||||
|
||||
return (
|
||||
<AntDropdown
|
||||
trigger={['hover', 'click']}
|
||||
trigger={[]}
|
||||
placement='bottomRight'
|
||||
transitionName=''
|
||||
open={open}
|
||||
onOpenChange={setOpen}
|
||||
menu={{ items, onClick: () => setOpen(false) }}
|
||||
autoFocus={openMode === 'keyboard'}
|
||||
onOpenChange={(nextOpen) => {
|
||||
if (!nextOpen) close();
|
||||
}}
|
||||
menu={{ items, onClick: close }}
|
||||
popupRender={(menu) => (
|
||||
<div className='ui-account-panel'>
|
||||
<div
|
||||
ref={popupRef}
|
||||
className='ui-account-panel'
|
||||
onPointerEnter={(event) => queueHoverChange(event, true)}
|
||||
onPointerLeave={(event) => queueHoverChange(event, false)}
|
||||
>
|
||||
<div className='ui-account-summary'>
|
||||
<Avatar
|
||||
shape='square'
|
||||
@@ -144,10 +203,14 @@ const UserArea = ({
|
||||
classNames={{ root: 'ui-account-dropdown' }}
|
||||
>
|
||||
<AntButton
|
||||
ref={triggerRef}
|
||||
type='text'
|
||||
className='ui-header-account'
|
||||
aria-haspopup='menu'
|
||||
aria-expanded={open}
|
||||
onPointerEnter={(event) => queueHoverChange(event, true)}
|
||||
onPointerLeave={(event) => queueHoverChange(event, false)}
|
||||
onClick={toggleOnClick}
|
||||
>
|
||||
<Avatar
|
||||
shape='square'
|
||||
|
||||
@@ -4,6 +4,7 @@ import { isAuthPagePath } from '../../../helpers/auth';
|
||||
import { useHeaderBar } from '../../../hooks/common/useHeaderBar';
|
||||
import { useNotifications } from '../../../hooks/common/useNotifications';
|
||||
import { useNavigation } from '../../../hooks/common/useNavigation';
|
||||
import { useTopNavigation } from '../../../hooks/common/useTopNavigation';
|
||||
import { usePartnerWorkspaceEntry } from '../../../hooks/common/usePartnerWorkspaceEntry';
|
||||
import MobileMenuButton from './MobileMenuButton';
|
||||
import HeaderLogo from './HeaderLogo';
|
||||
@@ -69,6 +70,7 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
||||
const mainNavLinks = configuredNavLinks.filter(
|
||||
(link) => link.itemKey !== 'keyManagement',
|
||||
);
|
||||
const followTopLink = useTopNavigation();
|
||||
|
||||
const [hasOpenedNotice, setHasOpenedNotice] = useState(false);
|
||||
useEffect(() => {
|
||||
@@ -146,6 +148,7 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
||||
<MobileNavMenu
|
||||
mainNavLinks={mainNavLinks}
|
||||
userState={userState}
|
||||
followTopLink={followTopLink}
|
||||
t={t}
|
||||
/>
|
||||
)}
|
||||
@@ -157,6 +160,7 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
||||
isMobile={isMobile}
|
||||
isLoading={isLoading}
|
||||
userState={userState}
|
||||
followTopLink={followTopLink}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user