From cd6ffdbe6862957151a51e26e20d1f765be688ff Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Wed, 26 Nov 2025 15:45:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dhooks=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/contexts/NotificationContext.js | 67 +++++++++++++++-------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/src/contexts/NotificationContext.js b/src/contexts/NotificationContext.js index b8ccd63a..799fcf9d 100644 --- a/src/contexts/NotificationContext.js +++ b/src/contexts/NotificationContext.js @@ -49,39 +49,7 @@ export const NotificationProvider = ({ children }) => { // ⚡ 移动端检测(使用 Redux 状态) const isMobile = useSelector(selectIsMobile); - // ⚡ 移动端禁用完整通知能力:返回空壳 Provider - // 移动端不支持桌面通知,且不需要 Socket 实时推送 - if (isMobile) { - const emptyValue = { - notifications: [], - isConnected: false, - soundEnabled: false, - browserPermission: 'default', - connectionStatus: CONNECTION_STATUS.DISCONNECTED, - reconnectAttempt: 0, - maxReconnectAttempts: 0, - addNotification: () => null, - removeNotification: () => {}, - clearAllNotifications: () => {}, - toggleSound: () => {}, - requestBrowserPermission: () => Promise.resolve('default'), - trackNotificationClick: () => {}, - retryConnection: () => {}, - showWelcomeGuide: () => {}, - showCommunityGuide: () => {}, - showFirstFollowGuide: () => {}, - registerEventUpdateCallback: () => () => {}, - unregisterEventUpdateCallback: () => {}, - }; - - return ( - - {children} - - ); - } - - // ========== 桌面端:完整通知功能 ========== + // ========== 所有 Hooks 必须在条件判断之前调用(React 规则) ========== const toast = useToast(); const [notifications, setNotifications] = useState([]); const [isConnected, setIsConnected] = useState(false); @@ -1047,6 +1015,39 @@ export const NotificationProvider = ({ children }) => { }; }, [browserPermission, toast]); + // ⚡ 移动端禁用完整通知能力:返回空壳 Provider + // 注意:此判断必须在所有 Hooks 之后(React 规则要求 Hooks 调用顺序一致) + if (isMobile) { + const emptyValue = { + notifications: [], + isConnected: false, + soundEnabled: false, + browserPermission: 'default', + connectionStatus: CONNECTION_STATUS.DISCONNECTED, + reconnectAttempt: 0, + maxReconnectAttempts: 0, + addNotification: () => null, + removeNotification: () => {}, + clearAllNotifications: () => {}, + toggleSound: () => {}, + requestBrowserPermission: () => Promise.resolve('default'), + trackNotificationClick: () => {}, + retryConnection: () => {}, + showWelcomeGuide: () => {}, + showCommunityGuide: () => {}, + showFirstFollowGuide: () => {}, + registerEventUpdateCallback: () => () => {}, + unregisterEventUpdateCallback: () => {}, + }; + + return ( + + {children} + + ); + } + + // ========== 桌面端:完整通知功能 ========== const value = { notifications, isConnected,