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