diff --git a/shared/index.tsx b/shared/index.tsx index 13402cd8..13c1c425 100644 --- a/shared/index.tsx +++ b/shared/index.tsx @@ -169,7 +169,12 @@ export { useUserId, useUserInfoList, } from './redux/hooks/useUserInfo'; -export { userActions, groupActions, uiActions } from './redux/slices'; +export { + userActions, + groupActions, + uiActions, + globalActions, +} from './redux/slices'; export type { ChatConverseState } from './redux/slices/chat'; export { setupRedux } from './redux/setup'; export { createStore, ReduxProvider } from './redux/store'; diff --git a/shared/redux/setup.ts b/shared/redux/setup.ts index c8c32aed..041b2fee 100644 --- a/shared/redux/setup.ts +++ b/shared/redux/setup.ts @@ -1,6 +1,11 @@ import type { AppStore } from './store'; import type { AppSocket } from '../api/socket'; -import { chatActions, groupActions, userActions } from './slices'; +import { + chatActions, + globalActions, + groupActions, + userActions, +} from './slices'; import type { FriendRequest } from '../model/friend'; import { getCachedConverseInfo } from '../cache/cache'; import type { GroupInfo } from '../model/group'; @@ -18,12 +23,14 @@ import { fetchUserAck, } from '../model/converse'; import { appendUserDMConverse } from '../model/user'; +import { sharedEvent } from '../event'; /** * 初始化 Redux 上下文 * 该文件用于处理远程数据与本地 Redux 状态的交互 */ export function setupRedux(socket: AppSocket, store: AppStore) { + store.dispatch(globalActions.setNetworkStatus('initial')); initial(socket, store); listenNotify(socket, store); @@ -32,6 +39,10 @@ export function setupRedux(socket: AppSocket, store: AppStore) { console.warn('因为断线重连触发重新同步远程数据'); initial(socket, store); }); + + sharedEvent.on('updateNetworkStatus', (status) => { + store.dispatch(globalActions.setNetworkStatus(status)); + }); } /** diff --git a/shared/redux/slices/global.ts b/shared/redux/slices/global.ts new file mode 100644 index 00000000..dcb35895 --- /dev/null +++ b/shared/redux/slices/global.ts @@ -0,0 +1,28 @@ +import { createSlice, PayloadAction } from '@reduxjs/toolkit'; + +export interface GlobalState { + /** + * 网络状态 + */ + networkStatus: 'initial' | 'connected' | 'reconnecting' | 'disconnected'; +} + +const initialState: GlobalState = { + networkStatus: 'initial', +}; + +const globalSlice = createSlice({ + name: 'global', + initialState, + reducers: { + setNetworkStatus( + state, + action: PayloadAction + ) { + state.networkStatus = action.payload; + }, + }, +}); + +export const globalActions = globalSlice.actions; +export const globalReducer = globalSlice.reducer; diff --git a/shared/redux/slices/index.ts b/shared/redux/slices/index.ts index 247175a6..968e9ca9 100644 --- a/shared/redux/slices/index.ts +++ b/shared/redux/slices/index.ts @@ -3,8 +3,10 @@ import { userReducer } from './user'; import { chatReducer } from './chat'; import { groupReducer } from './group'; import { uiReducer } from './ui'; +import { globalReducer } from './global'; export const appReducer = combineReducers({ + global: globalReducer, user: userReducer, chat: chatReducer, group: groupReducer, @@ -13,6 +15,7 @@ export const appReducer = combineReducers({ export type AppState = ReturnType; +export { globalActions } from './global'; export { userActions } from './user'; export { chatActions } from './chat'; export { groupActions } from './group'; diff --git a/web/src/components/GlobalTemporaryTip.tsx b/web/src/components/GlobalTemporaryTip.tsx index cf2ba85b..1964ffe4 100644 --- a/web/src/components/GlobalTemporaryTip.tsx +++ b/web/src/components/GlobalTemporaryTip.tsx @@ -38,7 +38,6 @@ export const GlobalTemporaryTip: React.FC = React.memo(() => { 立即认领 - {t('')} ) : null; });