From 4d554df237a3603686fdb1fc86152b0907892a28 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 5 Feb 2023 16:04:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=8F=82=E6=95=B0=E7=A7=BB=E5=8A=A8=E5=88=B0?= =?UTF-8?q?redux=E4=B8=AD=EF=BC=8C=E5=B9=B6=E5=B0=86redux=E7=9A=84api?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=94=B9=E4=B8=BA=E5=85=A8=E5=B1=80=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为此,需要将退出登录操作改为刷新页面以清理上下文 --- client/shared/index.tsx | 2 +- client/shared/model/config.ts | 28 ++++++++++--------- client/shared/redux/slices/global.ts | 11 ++++++++ client/shared/redux/store.ts | 4 ++- .../modals/SettingsView/Account.tsx | 5 ++-- client/web/src/routes/Main/Provider.tsx | 4 +-- 6 files changed, 34 insertions(+), 20 deletions(-) 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);