diff --git a/client/shared/index.tsx b/client/shared/index.tsx index f52ce4ca..7d5b0821 100644 --- a/client/shared/index.tsx +++ b/client/shared/index.tsx @@ -210,7 +210,7 @@ export { } from './redux/slices'; export type { ChatConverseState } from './redux/slices/chat'; export { setupRedux } from './redux/setup'; -export { createStore, ReduxProvider } from './redux/store'; +export { reduxStore, ReduxProvider } from './redux/store'; export type { AppStore, AppState, AppDispatch } from './redux/store'; // utils diff --git a/client/shared/model/config.ts b/client/shared/model/config.ts index 1374245f..63a146b3 100644 --- a/client/shared/model/config.ts +++ b/client/shared/model/config.ts @@ -1,4 +1,7 @@ import { request } from '../api/request'; +import { globalActions } from '../redux/slices'; +import { defaultGlobalConfig } from '../redux/slices/global'; +import { reduxStore } from '../redux/store'; /** * 后端的全局设置 @@ -9,26 +12,25 @@ export interface GlobalConfig { * 默认1m */ uploadFileLimit: number; + /** + * 是否在注册时校验邮箱 + */ + emailVerification: boolean; } -let globalConfig = { - uploadFileLimit: 1 * 1024 * 1024, - emailVerification: false, // 是否在注册时校验邮箱 -}; - -export function getGlobalConfig() { - return { - ...globalConfig, - }; +export function getGlobalConfig(): GlobalConfig { + return reduxStore.getState().global.config; } export async function fetchGlobalClientConfig(): Promise { const { data: config } = await request.get('/api/config/client'); - globalConfig = { - ...globalConfig, - ...config, - }; + reduxStore.dispatch( + globalActions.setGlobalConfig({ + ...defaultGlobalConfig, + ...config, + }) + ); return config; } diff --git a/client/shared/redux/slices/global.ts b/client/shared/redux/slices/global.ts index 2d735170..40cd32ef 100644 --- a/client/shared/redux/slices/global.ts +++ b/client/shared/redux/slices/global.ts @@ -1,4 +1,10 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; +import type { GlobalConfig } from '../../model/config'; + +export const defaultGlobalConfig: GlobalConfig = { + uploadFileLimit: 1 * 1024 * 1024, + emailVerification: false, +}; export interface GlobalState { /** @@ -6,11 +12,13 @@ export interface GlobalState { */ networkStatus: 'initial' | 'connected' | 'reconnecting' | 'disconnected'; reconnectNum: number; + config: GlobalConfig; } const initialState: GlobalState = { networkStatus: 'initial', reconnectNum: 0, + config: defaultGlobalConfig, }; const globalSlice = createSlice({ @@ -26,6 +34,9 @@ const globalSlice = createSlice({ incReconnectNum(state) { state.reconnectNum += 1; }, + setGlobalConfig(state, action: PayloadAction) { + state.config = action.payload; + }, }, }); diff --git a/client/shared/redux/store.ts b/client/shared/redux/store.ts index 8ff69e42..15daf5d5 100644 --- a/client/shared/redux/store.ts +++ b/client/shared/redux/store.ts @@ -1,7 +1,7 @@ import { configureStore } from '@reduxjs/toolkit'; import { appReducer } from './slices'; -export function createStore() { +function createStore() { const store = configureStore({ reducer: appReducer, middleware: (getDefaultMiddleware) => getDefaultMiddleware(), @@ -11,6 +11,8 @@ export function createStore() { return store; } +export const reduxStore = createStore(); + export type AppStore = ReturnType; export type AppState = ReturnType; export type AppDispatch = AppStore['dispatch']; diff --git a/client/web/src/components/modals/SettingsView/Account.tsx b/client/web/src/components/modals/SettingsView/Account.tsx index 119ae53e..bf00204b 100644 --- a/client/web/src/components/modals/SettingsView/Account.tsx +++ b/client/web/src/components/modals/SettingsView/Account.tsx @@ -82,9 +82,8 @@ export const SettingsAccount: React.FC = React.memo(() => { // 登出 const handleLogout = useCallback(async () => { await setUserJWT(null); - getGlobalSocket()?.disconnect(); - setGlobalUserLoginInfo(null); - navigate('/'); + + window.location.replace('/'); // 重载页面以清空所有状态 }, []); if (!userInfo) { diff --git a/client/web/src/routes/Main/Provider.tsx b/client/web/src/routes/Main/Provider.tsx index 1a559d85..649d0dfe 100644 --- a/client/web/src/routes/Main/Provider.tsx +++ b/client/web/src/routes/Main/Provider.tsx @@ -1,12 +1,12 @@ import { createSocket, - createStore, setupRedux, useAsync, userActions, t, ReduxProvider, UserLoginInfo, + reduxStore, } from 'tailchat-shared'; import React, { PropsWithChildren } from 'react'; import { LoadingSpinner } from '@/components/LoadingSpinner'; @@ -41,7 +41,7 @@ function useAppState() { // 到这里 userLoginInfo 必定存在 // 创建Redux store - const store = createStore(); + const store = reduxStore; store.dispatch(userActions.setUserInfo(userLoginInfo)); setGlobalStore(store);