1
- import { useContext } from 'react ' ;
2
- import { useNavigate } from 'react-router-dom ' ;
1
+ import { type ConsentInfoResponse } from '@logto/schemas ' ;
2
+ import { useContext , useEffect , useState } from 'react' ;
3
3
4
4
import StaticPageLayout from '@/Layout/StaticPageLayout' ;
5
5
import PageContext from '@/Providers/PageContextProvider/PageContext' ;
6
+ import { getConsentInfo } from '@/apis/consent' ;
6
7
import Button from '@/components/Button' ;
7
8
import DynamicT from '@/components/DynamicT' ;
8
- import NavBar from '@/components/NavBar ' ;
9
+ import LoadingLayer from '@/components/LoadingLayer ' ;
9
10
import PageMeta from '@/components/PageMeta' ;
11
+ import TextLink from '@/components/TextLink' ;
12
+ import useApi from '@/hooks/use-api' ;
13
+ import useErrorHandler from '@/hooks/use-error-handler' ;
14
+ import UserProfile from '@/pages/Consent/UserProfile' ;
15
+ import ErrorPage from '@/pages/ErrorPage' ;
10
16
import { getBrandingLogoUrl } from '@/utils/logo' ;
11
17
12
- import SupportInfo from '../ErrorPage/SupportInfo' ;
13
-
14
18
import styles from './index.module.scss' ;
15
19
20
+ /**
21
+ * This component is only used when there's an active session, and then the user
22
+ * is trying to sign-in with another account (e.g., using a magic link).
23
+ */
24
+
16
25
type Props = {
17
- readonly account ?: string ;
26
+ /**
27
+ * The account name of the current active session
28
+ */
29
+ readonly account : string ;
30
+ /**
31
+ * The callback function to be called after clicking the "Go back" link
32
+ */
33
+ readonly onCancel : ( ) => Promise < void > ;
34
+ /**
35
+ * The callback function to be called after clicking the "Switch" button
36
+ */
37
+ readonly onSwitch : ( ) => Promise < void > ;
18
38
} ;
19
39
20
- const SwitchAccount = ( { account } : Props ) => {
21
- const navigate = useNavigate ( ) ;
40
+ const SwitchAccount = ( { account, onCancel, onSwitch } : Props ) => {
22
41
const { experienceSettings, theme } = useContext ( PageContext ) ;
23
- const searchParams = new URLSearchParams ( window . location . search ) ;
24
- const currentAccountName = searchParams . get ( 'account' ) ?? account ;
42
+ const handleError = useErrorHandler ( ) ;
43
+
44
+ const [ consentData , setConsentData ] = useState < ConsentInfoResponse > ( ) ;
45
+ const asyncGetConsentInfo = useApi ( getConsentInfo ) ;
46
+
47
+ useEffect ( ( ) => {
48
+ ( async ( ) => {
49
+ const [ error , result ] = await asyncGetConsentInfo ( ) ;
25
50
26
- if ( ! experienceSettings ) {
27
- return null ;
51
+ if ( error ) {
52
+ await handleError ( error ) ;
53
+ return ;
54
+ }
55
+ setConsentData ( result ) ;
56
+ } ) ( ) ;
57
+ } , [ asyncGetConsentInfo , handleError ] ) ;
58
+
59
+ if ( ! account ) {
60
+ return < ErrorPage title = "error.unknown" message = "error.unknown" /> ;
61
+ }
62
+
63
+ if ( ! experienceSettings || ! consentData ) {
64
+ return < LoadingLayer /> ;
28
65
}
29
66
30
67
const {
@@ -36,28 +73,26 @@ const SwitchAccount = ({ account }: Props) => {
36
73
return (
37
74
< StaticPageLayout >
38
75
< PageMeta titleKey = "description.switch_account" />
39
- { history . length > 1 && < NavBar /> }
40
76
< div className = { styles . container } >
41
77
{ logoUrl && < img className = { styles . logo } src = { logoUrl } alt = "app logo" /> }
42
78
< div className = { styles . title } >
43
- < DynamicT
44
- forKey = "description.account_mismatch_title"
45
- interpolation = { { account : currentAccountName } }
46
- />
79
+ < DynamicT forKey = "description.switch_account_title" interpolation = { { account } } />
47
80
</ div >
81
+ < UserProfile user = { consentData . user } className = { styles . userProfile } />
48
82
< div className = { styles . message } >
49
- < DynamicT forKey = "description.account_mismatch_description " />
83
+ < DynamicT forKey = "description.switch_account_description " />
50
84
</ div >
51
- < SupportInfo />
52
85
< Button
53
86
className = { styles . button }
54
87
type = "primary"
55
88
size = "large"
56
- title = "description.switch_account"
57
- onClick = { ( ) => {
58
- navigate ( '/sign-in' ) ;
59
- } }
89
+ title = "action.switch_to"
90
+ i18nProps = { { method : account } }
91
+ onClick = { onSwitch }
60
92
/>
93
+ < div className = { styles . linkButton } >
94
+ < TextLink text = "action.back_to_current_account" onClick = { onCancel } />
95
+ </ div >
61
96
</ div >
62
97
</ StaticPageLayout >
63
98
) ;
0 commit comments