fix(header): 右侧快捷入口去重节流,稳定菜单和弹窗交互
This commit is contained in:
@@ -10,6 +10,14 @@
|
|||||||
|
|
||||||
## 2026-11-27
|
## 2026-11-27
|
||||||
|
|
||||||
|
### 优化右侧顶栏快捷操作
|
||||||
|
|
||||||
|
- **范围** 顶栏教程、WeyAi 下载、充值、推广、主题、语言和头像菜单。
|
||||||
|
- **变更** 快捷入口复用主导航节流和当前路由去重。主题切换限制连续触发,语言和头像菜单共用展开与关闭逻辑。
|
||||||
|
- **影响** 高频点击减少重复路由和主题更新,推广遮罩短暂过滤连点,连续退出登录只执行一个请求。
|
||||||
|
- **回滚** 恢复本次变更涉及的顶栏操作组件和相关 hook。
|
||||||
|
- **关联** 全站 UI 基线第 4.1 节。
|
||||||
|
|
||||||
### 修复顶栏连点、侧栏加载和头像菜单交互
|
### 修复顶栏连点、侧栏加载和头像菜单交互
|
||||||
|
|
||||||
- **范围** 全站主导航、用户中心、控制台、企业中心和顶栏头像菜单。
|
- **范围** 全站主导航、用户中心、控制台、企业中心和顶栏头像菜单。
|
||||||
|
|||||||
@@ -55,6 +55,7 @@
|
|||||||
- 全站共享顶栏不展示未登录用户的“登录 / 注册”按钮,桌面与移动端一致;已登录用户保留头像与账户菜单,登录 / 注册页面及受保护导航的原有鉴权跳转保持不变。
|
- 全站共享顶栏不展示未登录用户的“登录 / 注册”按钮,桌面与移动端一致;已登录用户保留头像与账户菜单,登录 / 注册页面及受保护导航的原有鉴权跳转保持不变。
|
||||||
- 主题按钮为 `Button` + `Tooltip`,点击在浅色与深色之间直接切换,不提供下拉或“跟随系统”;沿用现有主题 setter 与持久化,覆盖 `/org/login`、`/login` 与共享顶栏。
|
- 主题按钮为 `Button` + `Tooltip`,点击在浅色与深色之间直接切换,不提供下拉或“跟随系统”;沿用现有主题 setter 与持久化,覆盖 `/org/login`、`/login` 与共享顶栏。
|
||||||
- WeyAi 下载按钮复用现有下载配置与权限判定,跳转 `/weyai/download`;使用 antd text 按钮默认悬停背景与 `Tooltip`,不硬编码外部地址。
|
- WeyAi 下载按钮复用现有下载配置与权限判定,跳转 `/weyai/download`;使用 antd text 按钮默认悬停背景与 `Tooltip`,不硬编码外部地址。
|
||||||
|
- 右侧快捷链接复用主导航的当前路由去重与节流。主题切换即时响应首击,语言和头像菜单统一处理悬停、点击和键盘关闭。推广弹窗短暂过滤打开时落到遮罩上的连点。
|
||||||
|
|
||||||
### 4.2 公告栏与系统公告弹窗
|
### 4.2 公告栏与系统公告弹窗
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { useState, useContext } from 'react';
|
import React, { useState, useContext, useRef } from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { useHref } from 'react-router-dom';
|
||||||
import { isMiniProgramWebView } from '../../../helpers/miniProgram';
|
import { isMiniProgramWebView } from '../../../helpers/miniProgram';
|
||||||
import { Button as AntButton, Divider, Tooltip } from 'antd';
|
import { Button as AntButton, Divider, Tooltip } from 'antd';
|
||||||
import { Share2, BookOpen, Download, Wallet } from 'lucide-react';
|
import { Share2, BookOpen, Download, Wallet } from 'lucide-react';
|
||||||
@@ -13,12 +13,13 @@ import { StatusContext } from '../../../context/Status';
|
|||||||
import { isHeaderActionButtonVisible } from '../../../helpers/navigationVisibility';
|
import { isHeaderActionButtonVisible } from '../../../helpers/navigationVisibility';
|
||||||
import { useWeyAiDownloadEntry } from '../../../hooks/common/useWeyAiWebEntry';
|
import { useWeyAiDownloadEntry } from '../../../hooks/common/useWeyAiWebEntry';
|
||||||
|
|
||||||
|
const PROMOTION_MASK_INTERVAL = 240;
|
||||||
|
|
||||||
const ActionButtons = ({
|
const ActionButtons = ({
|
||||||
isAuthPage = false,
|
isAuthPage = false,
|
||||||
isNewYear,
|
isNewYear,
|
||||||
unreadCount,
|
unreadCount,
|
||||||
onNoticeOpen,
|
onNoticeOpen,
|
||||||
theme,
|
|
||||||
onThemeToggle,
|
onThemeToggle,
|
||||||
currentLang,
|
currentLang,
|
||||||
onLanguageChange,
|
onLanguageChange,
|
||||||
@@ -29,10 +30,14 @@ const ActionButtons = ({
|
|||||||
sidebarModulesAdmin,
|
sidebarModulesAdmin,
|
||||||
isOrgUser,
|
isOrgUser,
|
||||||
logout,
|
logout,
|
||||||
navigate,
|
followTopLink,
|
||||||
t,
|
t,
|
||||||
}) => {
|
}) => {
|
||||||
const [promotionVisible, setPromotionVisible] = useState(false);
|
const [promotionVisible, setPromotionVisible] = useState(false);
|
||||||
|
const promotionOpenedAt = useRef(-Infinity);
|
||||||
|
const tutorialHref = useHref('/tutorial');
|
||||||
|
const downloadHref = useHref('/weyai/download');
|
||||||
|
const rechargeHref = useHref('/recharge');
|
||||||
const [statusState] = useContext(StatusContext);
|
const [statusState] = useContext(StatusContext);
|
||||||
const isLoggedIn = userState?.user?.username;
|
const isLoggedIn = userState?.user?.username;
|
||||||
const userGroup = userState?.user?.group || 'default';
|
const userGroup = userState?.user?.group || 'default';
|
||||||
@@ -127,14 +132,14 @@ const ActionButtons = ({
|
|||||||
<div className='ui-header-actions flex items-center shrink-0'>
|
<div className='ui-header-actions flex items-center shrink-0'>
|
||||||
{/* Tutorial Button - 新手教程按钮 (桌面端显示) */}
|
{/* Tutorial Button - 新手教程按钮 (桌面端显示) */}
|
||||||
{isLoggedIn && isButtonVisible('tutorial') && (
|
{isLoggedIn && isButtonVisible('tutorial') && (
|
||||||
<Link
|
<AntButton
|
||||||
to='/tutorial'
|
href={tutorialHref}
|
||||||
className='ui-header-action-link ui-header-desktop-action'
|
className='ui-header-action-link ui-header-desktop-action'
|
||||||
|
icon={actionIcon(BookOpen)}
|
||||||
|
onClick={(event) => followTopLink('/tutorial', event)}
|
||||||
>
|
>
|
||||||
<AntButton icon={actionIcon(BookOpen)}>
|
|
||||||
{!isMobile && t('新手教程')}
|
{!isMobile && t('新手教程')}
|
||||||
</AntButton>
|
</AntButton>
|
||||||
</Link>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Promotion Button - 推广按钮 (桌面端显示) */}
|
{/* Promotion Button - 推广按钮 (桌面端显示) */}
|
||||||
@@ -142,7 +147,11 @@ const ActionButtons = ({
|
|||||||
<AntButton
|
<AntButton
|
||||||
icon={actionIcon(Share2)}
|
icon={actionIcon(Share2)}
|
||||||
className='ui-header-desktop-action'
|
className='ui-header-desktop-action'
|
||||||
onClick={() => setPromotionVisible(true)}
|
onClick={() => {
|
||||||
|
if (promotionVisible) return;
|
||||||
|
promotionOpenedAt.current = performance.now();
|
||||||
|
setPromotionVisible(true);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{!isMobile && t('推广')}
|
{!isMobile && t('推广')}
|
||||||
</AntButton>
|
</AntButton>
|
||||||
@@ -151,37 +160,47 @@ const ActionButtons = ({
|
|||||||
{/* WeyAi 桌面端下载 - 有 WeyAi 访问权限且下载页已开放时显示 (桌面端显示) */}
|
{/* WeyAi 桌面端下载 - 有 WeyAi 访问权限且下载页已开放时显示 (桌面端显示) */}
|
||||||
{showWeyAiDownload && (
|
{showWeyAiDownload && (
|
||||||
<Tooltip title={t('下载 WeyAi')}>
|
<Tooltip title={t('下载 WeyAi')}>
|
||||||
<Link
|
|
||||||
to='/weyai/download'
|
|
||||||
className='ui-header-action-link ui-header-desktop-action'
|
|
||||||
aria-label={t('下载 WeyAi 桌面端')}
|
|
||||||
>
|
|
||||||
<AntButton
|
<AntButton
|
||||||
|
href={downloadHref}
|
||||||
type='text'
|
type='text'
|
||||||
className='ui-header-download'
|
className='ui-header-action-link ui-header-desktop-action ui-header-download'
|
||||||
|
aria-label={t('下载 WeyAi 桌面端')}
|
||||||
icon={actionIcon(Download)}
|
icon={actionIcon(Download)}
|
||||||
|
onClick={(event) => followTopLink('/weyai/download', event)}
|
||||||
>
|
>
|
||||||
{!isMobile && t('下载 WeyAi')}
|
{!isMobile && t('下载 WeyAi')}
|
||||||
</AntButton>
|
</AntButton>
|
||||||
</Link>
|
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Recharge Button - 充值按钮 */}
|
{/* Recharge Button - 充值按钮 */}
|
||||||
{isButtonVisible('recharge') && !isMiniProgramWebView() && (
|
{isButtonVisible('recharge') && !isMiniProgramWebView() && (
|
||||||
<Link to='/recharge' className='ui-header-action-link'>
|
|
||||||
<AntButton
|
<AntButton
|
||||||
|
href={rechargeHref}
|
||||||
|
className='ui-header-action-link'
|
||||||
icon={actionIcon(Wallet)}
|
icon={actionIcon(Wallet)}
|
||||||
aria-label={isMobile ? t('充值') : undefined}
|
aria-label={isMobile ? t('充值') : undefined}
|
||||||
|
onClick={(event) => followTopLink('/recharge', event)}
|
||||||
>
|
>
|
||||||
{!isMobile && t('充值')}
|
{!isMobile && t('充值')}
|
||||||
</AntButton>
|
</AntButton>
|
||||||
</Link>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<PromotionPosterModal
|
<PromotionPosterModal
|
||||||
visible={promotionVisible}
|
visible={promotionVisible}
|
||||||
onClose={() => setPromotionVisible(false)}
|
onClose={(event) => {
|
||||||
|
// The remaining trigger clicks can land on the newly opened mask.
|
||||||
|
if (
|
||||||
|
event?.type === 'click' &&
|
||||||
|
event.target === event.currentTarget &&
|
||||||
|
event.currentTarget?.classList.contains('ant-modal-wrap') &&
|
||||||
|
performance.now() - promotionOpenedAt.current <
|
||||||
|
PROMOTION_MASK_INTERVAL
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setPromotionVisible(false);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Divider:原型仅在主操作与工具按钮之间保留一条竖线 */}
|
{/* Divider:原型仅在主操作与工具按钮之间保留一条竖线 */}
|
||||||
@@ -194,7 +213,7 @@ const ActionButtons = ({
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{isActionVisible('theme') && (
|
{isActionVisible('theme') && (
|
||||||
<ThemeToggle theme={theme} onThemeToggle={onThemeToggle} t={t} />
|
<ThemeToggle onThemeToggle={onThemeToggle} t={t} />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* 系统公告:原型位于主题与语言之间 */}
|
{/* 系统公告:原型位于主题与语言之间 */}
|
||||||
@@ -210,6 +229,7 @@ const ActionButtons = ({
|
|||||||
<LanguageSelector
|
<LanguageSelector
|
||||||
currentLang={currentLang}
|
currentLang={currentLang}
|
||||||
onLanguageChange={onLanguageChange}
|
onLanguageChange={onLanguageChange}
|
||||||
|
isMobile={isMobile}
|
||||||
t={t}
|
t={t}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@@ -223,7 +243,7 @@ const ActionButtons = ({
|
|||||||
sidebarModulesAdmin={sidebarModulesAdmin}
|
sidebarModulesAdmin={sidebarModulesAdmin}
|
||||||
isOrgUser={isOrgUser}
|
isOrgUser={isOrgUser}
|
||||||
logout={logout}
|
logout={logout}
|
||||||
navigate={navigate}
|
followTopLink={followTopLink}
|
||||||
t={t}
|
t={t}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { Button as AntButton, Dropdown } from 'antd';
|
|||||||
import { CN, GB, JP, KR, TH, VN } from 'country-flag-icons/react/3x2';
|
import { CN, GB, JP, KR, TH, VN } from 'country-flag-icons/react/3x2';
|
||||||
import { normalizeLanguage } from '../../../i18n/language';
|
import { normalizeLanguage } from '../../../i18n/language';
|
||||||
import { orderEnabledLanguages } from '../../../i18n/catalog';
|
import { orderEnabledLanguages } from '../../../i18n/catalog';
|
||||||
|
import { useHeaderDropdown } from './useHeaderDropdown';
|
||||||
import {
|
import {
|
||||||
LANG_BOOT_UPDATED_EVENT,
|
LANG_BOOT_UPDATED_EVENT,
|
||||||
getBootConfig,
|
getBootConfig,
|
||||||
@@ -12,7 +13,9 @@ import {
|
|||||||
// 语言目录里的 flag 国家码 → 旗帜组件
|
// 语言目录里的 flag 国家码 → 旗帜组件
|
||||||
const FLAGS = { CN, GB, JP, KR, TH, VN };
|
const FLAGS = { CN, GB, JP, KR, TH, VN };
|
||||||
|
|
||||||
const LanguageSelector = ({ currentLang, onLanguageChange, t }) => {
|
const LanguageSelector = ({ currentLang, onLanguageChange, isMobile, t }) => {
|
||||||
|
const { close, dropdownProps, popupProps, triggerProps } =
|
||||||
|
useHeaderDropdown(isMobile);
|
||||||
// i18n.language 可能带地区码(如 zh-CN / en-US),高亮判断先归一化;
|
// i18n.language 可能带地区码(如 zh-CN / en-US),高亮判断先归一化;
|
||||||
// 繁体靠地区码区分,不能简单取主语言
|
// 繁体靠地区码区分,不能简单取主语言
|
||||||
const lang = normalizeLanguage(currentLang);
|
const lang = normalizeLanguage(currentLang);
|
||||||
@@ -32,12 +35,16 @@ const LanguageSelector = ({ currentLang, onLanguageChange, t }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Dropdown
|
<Dropdown
|
||||||
trigger={['hover', 'click']}
|
{...dropdownProps}
|
||||||
placement='bottomRight'
|
placement='bottomRight'
|
||||||
|
popupRender={(menu) => <div {...popupProps}>{menu}</div>}
|
||||||
menu={{
|
menu={{
|
||||||
selectable: true,
|
selectable: true,
|
||||||
selectedKeys: [lang],
|
selectedKeys: [lang],
|
||||||
onClick: ({ key }) => onLanguageChange(key),
|
onClick: ({ key }) => {
|
||||||
|
close();
|
||||||
|
onLanguageChange(key);
|
||||||
|
},
|
||||||
items: languages.map(({ code, endonym, flag }) => {
|
items: languages.map(({ code, endonym, flag }) => {
|
||||||
const Flag = FLAGS[flag];
|
const Flag = FLAGS[flag];
|
||||||
return {
|
return {
|
||||||
@@ -49,10 +56,10 @@ const LanguageSelector = ({ currentLang, onLanguageChange, t }) => {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<AntButton
|
<AntButton
|
||||||
|
{...triggerProps}
|
||||||
type='text'
|
type='text'
|
||||||
icon={<ActionGlobe size={16} strokeWidth={1.5} aria-hidden />}
|
icon={<ActionGlobe size={16} strokeWidth={1.5} aria-hidden />}
|
||||||
aria-label={t('common.changeLanguage')}
|
aria-label={t('common.changeLanguage')}
|
||||||
aria-haspopup='menu'
|
|
||||||
className='ui-header-icon-action'
|
className='ui-header-icon-action'
|
||||||
/>
|
/>
|
||||||
</Dropdown>
|
</Dropdown>
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
import React from 'react';
|
import React, { useRef } from 'react';
|
||||||
import { Button, Tooltip } from 'antd';
|
import { Button, Tooltip } from 'antd';
|
||||||
import { Moon, Sun } from '../../common/icons/ActionIcons';
|
import { Moon, Sun } from '../../common/icons/ActionIcons';
|
||||||
import { useActualTheme } from '../../../context/Theme';
|
import { useActualTheme } from '../../../context/Theme';
|
||||||
|
|
||||||
|
const THEME_TOGGLE_INTERVAL = 350;
|
||||||
|
|
||||||
const ThemeToggle = ({ onThemeToggle, t }) => {
|
const ThemeToggle = ({ onThemeToggle, t }) => {
|
||||||
|
const lastToggle = useRef(-Infinity);
|
||||||
const isDark = useActualTheme() === 'dark';
|
const isDark = useActualTheme() === 'dark';
|
||||||
const label = isDark ? t('切换至亮色主题') : t('切换至暗色主题');
|
const label = isDark ? t('切换至亮色主题') : t('切换至暗色主题');
|
||||||
const Icon = isDark ? Sun : Moon;
|
const Icon = isDark ? Sun : Moon;
|
||||||
@@ -15,7 +18,12 @@ const ThemeToggle = ({ onThemeToggle, t }) => {
|
|||||||
icon={<Icon size={16} strokeWidth={1.5} aria-hidden />}
|
icon={<Icon size={16} strokeWidth={1.5} aria-hidden />}
|
||||||
aria-label={t('切换主题')}
|
aria-label={t('切换主题')}
|
||||||
className='ui-header-icon-action'
|
className='ui-header-icon-action'
|
||||||
onClick={() => onThemeToggle(isDark ? 'light' : 'dark')}
|
onClick={() => {
|
||||||
|
const now = performance.now();
|
||||||
|
if (now - lastToggle.current < THEME_TOGGLE_INTERVAL) return;
|
||||||
|
lastToggle.current = now;
|
||||||
|
onThemeToggle(isDark ? 'light' : 'dark');
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { ChevronDown as ActionChevronDown } from '@/components/common/icons/ActionIcons';
|
import { ChevronDown as ActionChevronDown } from '@/components/common/icons/ActionIcons';
|
||||||
import React, { useEffect, useRef, useState } from 'react';
|
import React from 'react';
|
||||||
import { Avatar, Button as AntButton, Dropdown as AntDropdown } from 'antd';
|
import { Avatar, Button as AntButton, Dropdown as AntDropdown } from 'antd';
|
||||||
import { UserRound, KeyRound, LogOut } from 'lucide-react';
|
import { UserRound, KeyRound, LogOut } from 'lucide-react';
|
||||||
import { getUserAvatarUrl } from '../../../helpers';
|
import { getUserAvatarUrl } from '../../../helpers';
|
||||||
import SkeletonWrapper from '../components/SkeletonWrapper';
|
import SkeletonWrapper from '../components/SkeletonWrapper';
|
||||||
import { isHeaderModuleEnabled } from '../../../constants/headerNavModules';
|
import { isHeaderModuleEnabled } from '../../../constants/headerNavModules';
|
||||||
|
import { useHeaderDropdown } from './useHeaderDropdown';
|
||||||
|
|
||||||
// 头像菜单项:图标框 + 标题 + 说明(原型 account-menu-label)
|
// 头像菜单项:图标框 + 标题 + 说明(原型 account-menu-label)
|
||||||
const AccountMenuLabel = ({ icon, tone, title, description }) => (
|
const AccountMenuLabel = ({ icon, tone, title, description }) => (
|
||||||
@@ -20,7 +21,6 @@ const AccountMenuLabel = ({ icon, tone, title, description }) => (
|
|||||||
);
|
);
|
||||||
|
|
||||||
const MENU_ICON_PROPS = { size: 18, strokeWidth: 1.65, 'aria-hidden': true };
|
const MENU_ICON_PROPS = { size: 18, strokeWidth: 1.65, 'aria-hidden': true };
|
||||||
const HOVER_DELAY = 100;
|
|
||||||
|
|
||||||
const UserArea = ({
|
const UserArea = ({
|
||||||
userState,
|
userState,
|
||||||
@@ -31,59 +31,11 @@ const UserArea = ({
|
|||||||
sidebarModulesAdmin = {},
|
sidebarModulesAdmin = {},
|
||||||
isOrgUser = false,
|
isOrgUser = false,
|
||||||
logout,
|
logout,
|
||||||
navigate,
|
followTopLink,
|
||||||
t,
|
t,
|
||||||
}) => {
|
}) => {
|
||||||
const [openMode, setOpenMode] = useState(null);
|
const { open, close, dropdownProps, popupProps, triggerProps } =
|
||||||
const triggerRef = useRef(null);
|
useHeaderDropdown(isMobile);
|
||||||
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 isActionVisible = (key) => rightActions?.[key] !== false;
|
||||||
const hasHeaderModuleConfig =
|
const hasHeaderModuleConfig =
|
||||||
headerNavModules && Object.keys(headerNavModules).length > 0;
|
headerNavModules && Object.keys(headerNavModules).length > 0;
|
||||||
@@ -134,7 +86,7 @@ const UserArea = ({
|
|||||||
description={t('管理账户信息')}
|
description={t('管理账户信息')}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
onClick: () => navigate('/user-center/settings'),
|
onClick: () => followTopLink('/user-center/settings'),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (showKeyManagement) {
|
if (showKeyManagement) {
|
||||||
@@ -148,7 +100,7 @@ const UserArea = ({
|
|||||||
description={t('查看和管理 API 密钥')}
|
description={t('查看和管理 API 密钥')}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
onClick: () => navigate('/user-center/key-management'),
|
onClick: () => followTopLink('/user-center/key-management'),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
items.push({ key: 'account-divider', type: 'divider' });
|
items.push({ key: 'account-divider', type: 'divider' });
|
||||||
@@ -167,22 +119,11 @@ const UserArea = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<AntDropdown
|
<AntDropdown
|
||||||
trigger={[]}
|
{...dropdownProps}
|
||||||
placement='bottomRight'
|
placement='bottomRight'
|
||||||
transitionName=''
|
|
||||||
open={open}
|
|
||||||
autoFocus={openMode === 'keyboard'}
|
|
||||||
onOpenChange={(nextOpen) => {
|
|
||||||
if (!nextOpen) close();
|
|
||||||
}}
|
|
||||||
menu={{ items, onClick: close }}
|
menu={{ items, onClick: close }}
|
||||||
popupRender={(menu) => (
|
popupRender={(menu) => (
|
||||||
<div
|
<div {...popupProps} className='ui-account-panel'>
|
||||||
ref={popupRef}
|
|
||||||
className='ui-account-panel'
|
|
||||||
onPointerEnter={(event) => queueHoverChange(event, true)}
|
|
||||||
onPointerLeave={(event) => queueHoverChange(event, false)}
|
|
||||||
>
|
|
||||||
<div className='ui-account-summary'>
|
<div className='ui-account-summary'>
|
||||||
<Avatar
|
<Avatar
|
||||||
shape='square'
|
shape='square'
|
||||||
@@ -202,16 +143,7 @@ const UserArea = ({
|
|||||||
)}
|
)}
|
||||||
classNames={{ root: 'ui-account-dropdown' }}
|
classNames={{ root: 'ui-account-dropdown' }}
|
||||||
>
|
>
|
||||||
<AntButton
|
<AntButton {...triggerProps} type='text' className='ui-header-account'>
|
||||||
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
|
<Avatar
|
||||||
shape='square'
|
shape='square'
|
||||||
size={38}
|
size={38}
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
docsLink,
|
docsLink,
|
||||||
isDemoSiteMode,
|
isDemoSiteMode,
|
||||||
isConsoleRoute,
|
isConsoleRoute,
|
||||||
theme,
|
|
||||||
headerNavModules,
|
headerNavModules,
|
||||||
sidebarModulesAdmin,
|
sidebarModulesAdmin,
|
||||||
showOrgEntry,
|
showOrgEntry,
|
||||||
@@ -44,7 +43,6 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
handleLanguageChange,
|
handleLanguageChange,
|
||||||
handleThemeToggle,
|
handleThemeToggle,
|
||||||
handleMobileMenuToggle,
|
handleMobileMenuToggle,
|
||||||
navigate,
|
|
||||||
t,
|
t,
|
||||||
} = useHeaderBar({ onMobileMenuToggle, drawerOpen });
|
} = useHeaderBar({ onMobileMenuToggle, drawerOpen });
|
||||||
|
|
||||||
@@ -169,7 +167,6 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
isNewYear={isNewYear}
|
isNewYear={isNewYear}
|
||||||
unreadCount={unreadCount}
|
unreadCount={unreadCount}
|
||||||
onNoticeOpen={handleNoticeOpen}
|
onNoticeOpen={handleNoticeOpen}
|
||||||
theme={theme}
|
|
||||||
onThemeToggle={handleThemeToggle}
|
onThemeToggle={handleThemeToggle}
|
||||||
currentLang={currentLang}
|
currentLang={currentLang}
|
||||||
onLanguageChange={handleLanguageChange}
|
onLanguageChange={handleLanguageChange}
|
||||||
@@ -180,7 +177,7 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
sidebarModulesAdmin={sidebarModulesAdmin}
|
sidebarModulesAdmin={sidebarModulesAdmin}
|
||||||
isOrgUser={isOrgUser}
|
isOrgUser={isOrgUser}
|
||||||
logout={logout}
|
logout={logout}
|
||||||
navigate={navigate}
|
followTopLink={followTopLink}
|
||||||
t={t}
|
t={t}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
|
const HOVER_DELAY = 100;
|
||||||
|
const CLICK_INTERVAL = 240;
|
||||||
|
|
||||||
|
export function useHeaderDropdown(isMobile) {
|
||||||
|
const [mode, setMode] = useState(null);
|
||||||
|
const triggerRef = useRef(null);
|
||||||
|
const popupRef = useRef(null);
|
||||||
|
const hoverTimer = useRef();
|
||||||
|
const lastClick = useRef(-Infinity);
|
||||||
|
const open = mode !== null;
|
||||||
|
|
||||||
|
const close = useCallback(() => {
|
||||||
|
clearTimeout(hoverTimer.current);
|
||||||
|
lastClick.current = -Infinity;
|
||||||
|
setMode(null);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => () => clearTimeout(hoverTimer.current), []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return;
|
||||||
|
const handleOutsidePress = (event) => {
|
||||||
|
if (
|
||||||
|
!triggerRef.current?.contains(event.target) &&
|
||||||
|
!popupRef.current?.contains(event.target)
|
||||||
|
) {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener('pointerdown', handleOutsidePress, true);
|
||||||
|
return () =>
|
||||||
|
document.removeEventListener('pointerdown', handleOutsidePress, true);
|
||||||
|
}, [close, open]);
|
||||||
|
|
||||||
|
const queueHoverChange = (event, opening) => {
|
||||||
|
if (isMobile || event.pointerType !== 'mouse') return;
|
||||||
|
clearTimeout(hoverTimer.current);
|
||||||
|
if (opening ? open : mode !== 'hover') return;
|
||||||
|
|
||||||
|
hoverTimer.current = setTimeout(() => {
|
||||||
|
setMode((current) => {
|
||||||
|
if (opening) return current ?? 'hover';
|
||||||
|
return current === 'hover' ? null : current;
|
||||||
|
});
|
||||||
|
}, HOVER_DELAY);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEscape = (event) => {
|
||||||
|
if (event.key !== 'Escape' || !open) return;
|
||||||
|
event.stopPropagation();
|
||||||
|
close();
|
||||||
|
if (popupRef.current?.contains(event.target)) triggerRef.current?.focus();
|
||||||
|
};
|
||||||
|
|
||||||
|
const pointerEvents = {
|
||||||
|
onPointerEnter: (event) => queueHoverChange(event, true),
|
||||||
|
onPointerLeave: (event) => queueHoverChange(event, false),
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
open,
|
||||||
|
close,
|
||||||
|
dropdownProps: {
|
||||||
|
trigger: [],
|
||||||
|
transitionName: '',
|
||||||
|
open,
|
||||||
|
autoFocus: mode === 'keyboard',
|
||||||
|
onOpenChange: (nextOpen) => {
|
||||||
|
if (!nextOpen) close();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
popupProps: { ...pointerEvents, ref: popupRef, onKeyDown: handleEscape },
|
||||||
|
triggerProps: {
|
||||||
|
...pointerEvents,
|
||||||
|
ref: triggerRef,
|
||||||
|
'aria-haspopup': 'menu',
|
||||||
|
'aria-expanded': open,
|
||||||
|
onKeyDown: handleEscape,
|
||||||
|
onClick: (event) => {
|
||||||
|
const now = performance.now();
|
||||||
|
if (now - lastClick.current < CLICK_INTERVAL) return;
|
||||||
|
lastClick.current = now;
|
||||||
|
clearTimeout(hoverTimer.current);
|
||||||
|
const clickedMode = event.detail === 0 ? 'keyboard' : 'click';
|
||||||
|
setMode((current) =>
|
||||||
|
current === null || current === 'hover' ? clickedMode : null,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,9 +1,16 @@
|
|||||||
import { useState, useEffect, useContext, useCallback, useMemo } from 'react';
|
import {
|
||||||
|
useState,
|
||||||
|
useEffect,
|
||||||
|
useContext,
|
||||||
|
useCallback,
|
||||||
|
useMemo,
|
||||||
|
useRef,
|
||||||
|
} from 'react';
|
||||||
import { useNavigate, useLocation } from 'react-router-dom';
|
import { useNavigate, useLocation } from 'react-router-dom';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { UserContext } from '../../context/User';
|
import { UserContext } from '../../context/User';
|
||||||
import { StatusContext } from '../../context/Status';
|
import { StatusContext } from '../../context/Status';
|
||||||
import { useSetTheme, useTheme, useActualTheme } from '../../context/Theme';
|
import { useSetTheme, useActualTheme } from '../../context/Theme';
|
||||||
import {
|
import {
|
||||||
getLogo,
|
getLogo,
|
||||||
getSystemName,
|
getSystemName,
|
||||||
@@ -12,6 +19,7 @@ import {
|
|||||||
updateAPI,
|
updateAPI,
|
||||||
} from '../../helpers';
|
} from '../../helpers';
|
||||||
import { rememberUserLanguage } from '../../i18n/bootstrap';
|
import { rememberUserLanguage } from '../../i18n/bootstrap';
|
||||||
|
import { normalizeLanguage } from '../../i18n/language';
|
||||||
import { useIsMobile } from './useIsMobile';
|
import { useIsMobile } from './useIsMobile';
|
||||||
import { useSidebarCollapsed } from './useSidebarCollapsed';
|
import { useSidebarCollapsed } from './useSidebarCollapsed';
|
||||||
import { useMinimumLoadingTime } from './useMinimumLoadingTime';
|
import { useMinimumLoadingTime } from './useMinimumLoadingTime';
|
||||||
@@ -34,6 +42,8 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
const [logoLoaded, setLogoLoaded] = useState(false);
|
const [logoLoaded, setLogoLoaded] = useState(false);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [currentLang, setCurrentLang] = useState(i18n.language);
|
const [currentLang, setCurrentLang] = useState(i18n.language);
|
||||||
|
const languageChanging = useRef(false);
|
||||||
|
const loggingOut = useRef(false);
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
const loading = statusState?.status === undefined || userState?.authChecking;
|
const loading = statusState?.status === undefined || userState?.authChecking;
|
||||||
@@ -94,7 +104,6 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
|
|
||||||
const isConsoleRoute = location.pathname.startsWith('/console');
|
const isConsoleRoute = location.pathname.startsWith('/console');
|
||||||
|
|
||||||
const theme = useTheme();
|
|
||||||
const actualTheme = useActualTheme();
|
const actualTheme = useActualTheme();
|
||||||
const setTheme = useSetTheme();
|
const setTheme = useSetTheme();
|
||||||
|
|
||||||
@@ -143,18 +152,31 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
const logout = useCallback(async () => {
|
const logout = useCallback(async () => {
|
||||||
|
if (loggingOut.current) return;
|
||||||
|
loggingOut.current = true;
|
||||||
|
try {
|
||||||
await API.get('/api/user/logout');
|
await API.get('/api/user/logout');
|
||||||
userDispatch({ type: 'logout' });
|
userDispatch({ type: 'logout' });
|
||||||
clearUserData();
|
clearUserData();
|
||||||
updateAPI();
|
updateAPI();
|
||||||
navigate('/org/login');
|
navigate('/org/login');
|
||||||
|
} finally {
|
||||||
|
loggingOut.current = false;
|
||||||
|
}
|
||||||
}, [navigate, userDispatch]);
|
}, [navigate, userDispatch]);
|
||||||
|
|
||||||
const handleLanguageChange = useCallback(
|
const handleLanguageChange = useCallback(
|
||||||
(lang) => {
|
async (lang) => {
|
||||||
|
if (languageChanging.current) return;
|
||||||
// 记为显式选择:此后优先于 IP 国家判定(后台关闭顶栏切换时失效)
|
// 记为显式选择:此后优先于 IP 国家判定(后台关闭顶栏切换时失效)
|
||||||
rememberUserLanguage(lang);
|
const language = rememberUserLanguage(lang);
|
||||||
i18n.changeLanguage(lang);
|
if (!language || language === normalizeLanguage(i18n.language)) return;
|
||||||
|
languageChanging.current = true;
|
||||||
|
try {
|
||||||
|
await i18n.changeLanguage(language);
|
||||||
|
} finally {
|
||||||
|
languageChanging.current = false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[i18n],
|
[i18n],
|
||||||
);
|
);
|
||||||
@@ -197,7 +219,6 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
docsLink,
|
docsLink,
|
||||||
isDemoSiteMode,
|
isDemoSiteMode,
|
||||||
isConsoleRoute,
|
isConsoleRoute,
|
||||||
theme,
|
|
||||||
drawerOpen,
|
drawerOpen,
|
||||||
headerNavModules,
|
headerNavModules,
|
||||||
sidebarModulesAdmin,
|
sidebarModulesAdmin,
|
||||||
@@ -211,7 +232,6 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|||||||
handleLanguageChange,
|
handleLanguageChange,
|
||||||
handleThemeToggle,
|
handleThemeToggle,
|
||||||
handleMobileMenuToggle,
|
handleMobileMenuToggle,
|
||||||
navigate,
|
|
||||||
t,
|
t,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user