1- import { useCallback , useEffect , useMemo , useRef } from 'react' ;
1+ import {
2+ useCallback ,
3+ useEffect ,
4+ useMemo ,
5+ useRef ,
6+ startTransition
7+ } from 'react' ;
28import { useTranslation } from 'react-i18next' ;
39import { useDispatch } from 'react-redux' ;
410import { Typography , Form } from 'antd' ;
511import { OauthLoginFormFields } from './index.type' ;
6- import { updateToken } from '../../store/user' ;
12+ import { updateToken , updateIsLoggingIn } from '../../store/user' ;
713import { ResponseCode } from '@actiontech/dms-kit' ;
814import OAuth2 from '@actiontech/shared/lib/api/base/service/OAuth2' ;
915import LoginLayout from '../Login/components/LoginLayout' ;
@@ -24,6 +30,8 @@ import {
2430 StorageKey ,
2531 CompanyNoticeDisplayStatusEnum
2632} from '@actiontech/dms-kit' ;
33+ import useSessionUser from '../../hooks/useSessionUser' ;
34+ import useNavigateToWorkbench from '../../hooks/useNavigateToWorkbench' ;
2735const BindUser = ( ) => {
2836 const navigate = useTypedNavigate ( ) ;
2937 const { baseTheme } = useThemeStyleData ( ) ;
@@ -35,28 +43,65 @@ const BindUser = () => {
3543 } , [ extractQueries ] ) ;
3644 useBrowserVersionTips ( ) ;
3745 const loginLock = useRef ( false ) ;
46+
47+ const { getSessionUserInfoAsync, getSessionUserSystemLoading } =
48+ useSessionUser ( ) ;
49+
50+ const {
51+ navigateToWorkbenchAsync,
52+ getAvailabilityZoneTipsAsync,
53+ navigateToWorkbenchLoading,
54+ getAvailabilityZoneTipsLoading
55+ } = useNavigateToWorkbench ( ) ;
56+
3857 const concatToken = ( token = '' ) => {
3958 if ( ! token ) {
4059 return '' ;
4160 }
4261 return `Bearer ${ token } ` ;
4362 } ;
63+
4464 const navigateToTarget = useCallback ( ( ) => {
45- const encodedTarget = urlParams ?. target ;
46- if ( encodedTarget ) {
47- const decoded = decodeURIComponent ( encodedTarget ) ;
48- const [ path , targetParams ] = decoded . split ( '?' ) ;
49- if ( targetParams ) {
50- navigate ( `${ path } ?${ targetParams } ` ) ;
51- } else if ( path . endsWith ( 'cloud-beaver' ) ) {
52- navigate ( `${ path } ?${ OPEN_CLOUD_BEAVER_URL_PARAM_NAME } =true` ) ;
65+ dispatch ( updateIsLoggingIn ( true ) ) ;
66+ getSessionUserInfoAsync ( ) . then ( ( shouldNavigateToWorkbench ) => {
67+ if ( shouldNavigateToWorkbench ) {
68+ // #if [ee]
69+ getAvailabilityZoneTipsAsync ( ) . then ( ( ) => {
70+ navigateToWorkbenchAsync ( ) . then ( ( ) => {
71+ dispatch ( updateIsLoggingIn ( false ) ) ;
72+ } ) ;
73+ } ) ;
74+ // #else
75+ navigateToWorkbenchAsync ( ) . then ( ( ) => {
76+ dispatch ( updateIsLoggingIn ( false ) ) ;
77+ } ) ;
78+ // #endif
5379 } else {
54- navigate ( path ) ;
80+ const encodedTarget = urlParams ?. target ;
81+ if ( encodedTarget ) {
82+ const decoded = decodeURIComponent ( encodedTarget ) ;
83+ const [ path , targetParams ] = decoded . split ( '?' ) ;
84+ if ( targetParams ) {
85+ navigate ( `${ path } ?${ targetParams } ` ) ;
86+ } else if ( path . endsWith ( 'cloud-beaver' ) ) {
87+ navigate ( `${ path } ?${ OPEN_CLOUD_BEAVER_URL_PARAM_NAME } =true` ) ;
88+ } else {
89+ navigate ( path ) ;
90+ }
91+ } else {
92+ navigate ( ROUTE_PATHS . BASE . HOME ) ;
93+ }
94+ dispatch ( updateIsLoggingIn ( false ) ) ;
5595 }
56- } else {
57- navigate ( ROUTE_PATHS . BASE . HOME ) ;
58- }
59- } , [ navigate , urlParams ] ) ;
96+ } ) ;
97+ } , [
98+ dispatch ,
99+ getSessionUserInfoAsync ,
100+ getAvailabilityZoneTipsAsync ,
101+ navigateToWorkbenchAsync ,
102+ urlParams ,
103+ navigate
104+ ] ) ;
60105 const login = ( values : OauthLoginFormFields ) => {
61106 const oauth2Token = urlParams ?. oauth2_token ;
62107 loginLock . current = true ;
@@ -67,7 +112,13 @@ const BindUser = () => {
67112 duration : 0
68113 } ) ;
69114 loginLock . current = false ;
70- navigate ( ROUTE_PATHS . BASE . LOGIN . index . path ) ;
115+ // 使用startTransition的原因如下:
116+ // login 函数是表单的 onFinish 回调,属于同步用户交互事件
117+ // navigate 可能会触发懒加载组件(Suspense)
118+ // React 18 不允许在同步事件中直接触发 Suspense,否则会抛出错误
119+ startTransition ( ( ) => {
120+ navigate ( ROUTE_PATHS . BASE . LOGIN . index . path ) ;
121+ } ) ;
71122 return ;
72123 }
73124 OAuth2 . BindOauth2User ( {
@@ -134,9 +185,15 @@ const BindUser = () => {
134185 urlParams ?. error ,
135186 urlParams ?. user_exist
136187 ] ) ;
188+ const isLoading =
189+ loginLock . current ||
190+ getSessionUserSystemLoading ||
191+ getAvailabilityZoneTipsLoading ||
192+ navigateToWorkbenchLoading ;
193+
137194 return (
138195 < LoginLayout >
139- < Form onFinish = { login } disabled = { loginLock . current } >
196+ < Form onFinish = { login } disabled = { isLoading } >
140197 < Form . Item
141198 name = "username"
142199 rules = { [
@@ -196,7 +253,7 @@ const BindUser = () => {
196253 block
197254 htmlType = "submit"
198255 className = "login-btn"
199- loading = { loginLock . current }
256+ loading = { isLoading }
200257 >
201258 { t ( 'dmsLogin.oauth.submitButton' ) }
202259 </ BasicButton >
0 commit comments