|
| 1 | +import { type ConsentInfoResponse } from '@logto/schemas'; |
| 2 | +import { useContext, useEffect, useState } from 'react'; |
| 3 | + |
| 4 | +import StaticPageLayout from '@/Layout/StaticPageLayout'; |
| 5 | +import PageContext from '@/Providers/PageContextProvider/PageContext'; |
| 6 | +import { getConsentInfo } from '@/apis/consent'; |
| 7 | +import Button from '@/components/Button'; |
| 8 | +import DynamicT from '@/components/DynamicT'; |
| 9 | +import LoadingLayer from '@/components/LoadingLayer'; |
| 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'; |
| 16 | +import { getBrandingLogoUrl } from '@/utils/logo'; |
| 17 | + |
| 18 | +import styles from './index.module.scss'; |
| 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 | + |
| 25 | +type Props = { |
| 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>; |
| 38 | +}; |
| 39 | + |
| 40 | +const SwitchAccount = ({ account, onCancel, onSwitch }: Props) => { |
| 41 | + const { experienceSettings, theme } = useContext(PageContext); |
| 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(); |
| 50 | + |
| 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 />; |
| 65 | + } |
| 66 | + |
| 67 | + const { |
| 68 | + color: { isDarkModeEnabled }, |
| 69 | + branding, |
| 70 | + } = experienceSettings; |
| 71 | + const logoUrl = getBrandingLogoUrl({ theme, branding, isDarkModeEnabled }); |
| 72 | + |
| 73 | + return ( |
| 74 | + <StaticPageLayout> |
| 75 | + <PageMeta titleKey="description.switch_account" /> |
| 76 | + <div className={styles.container}> |
| 77 | + {logoUrl && <img className={styles.logo} src={logoUrl} alt="app logo" />} |
| 78 | + <div className={styles.title}> |
| 79 | + <DynamicT forKey="description.switch_account_title" interpolation={{ account }} /> |
| 80 | + </div> |
| 81 | + <UserProfile user={consentData.user} className={styles.userProfile} /> |
| 82 | + <div className={styles.message}> |
| 83 | + <DynamicT forKey="description.switch_account_description" /> |
| 84 | + </div> |
| 85 | + <Button |
| 86 | + className={styles.button} |
| 87 | + type="primary" |
| 88 | + size="large" |
| 89 | + title="action.switch_to" |
| 90 | + i18nProps={{ method: account }} |
| 91 | + onClick={onSwitch} |
| 92 | + /> |
| 93 | + <div className={styles.linkButton}> |
| 94 | + <TextLink text="action.back_to_current_account" onClick={onCancel} /> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </StaticPageLayout> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +export default SwitchAccount; |
0 commit comments