|
| 1 | +import { Button, IconButton, notify } from '@affine/component'; |
| 2 | +import { AuthPageContainer } from '@affine/component/auth-components'; |
| 3 | +import { useMutation } from '@affine/core/components/hooks/use-mutation'; |
| 4 | +import { OpenInAppService } from '@affine/core/modules/open-in-app'; |
| 5 | +import { copyTextToClipboard } from '@affine/core/utils/clipboard'; |
| 6 | +import { generateLicenseKeyMutation, UserFriendlyError } from '@affine/graphql'; |
| 7 | +import { Trans, useI18n } from '@affine/i18n'; |
| 8 | +import { CopyIcon } from '@blocksuite/icons/rc'; |
| 9 | +import { useService } from '@toeverything/infra'; |
| 10 | +import { useCallback, useEffect, useState } from 'react'; |
| 11 | +import { useSearchParams } from 'react-router-dom'; |
| 12 | + |
| 13 | +import { PageNotFound } from '../../404'; |
| 14 | +import * as styles from './styles.css'; |
| 15 | + |
| 16 | +/** |
| 17 | + * /upgrade-success/self-hosted-team page |
| 18 | + * |
| 19 | + * only on web |
| 20 | + */ |
| 21 | +export const Component = () => { |
| 22 | + const [params] = useSearchParams(); |
| 23 | + const [key, setKey] = useState<string | null>(null); |
| 24 | + const sessionId = params.get('session_id'); |
| 25 | + const { trigger: generateLicenseKey } = useMutation({ |
| 26 | + mutation: generateLicenseKeyMutation, |
| 27 | + }); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (sessionId && !key) { |
| 31 | + generateLicenseKey({ sessionId }) |
| 32 | + .then(({ generateLicenseKey }) => { |
| 33 | + setKey(generateLicenseKey); |
| 34 | + }) |
| 35 | + .catch(e => { |
| 36 | + const error = UserFriendlyError.fromAnyError(e); |
| 37 | + console.error(error); |
| 38 | + |
| 39 | + notify.error({ |
| 40 | + title: error.name, |
| 41 | + message: error.message, |
| 42 | + }); |
| 43 | + }); |
| 44 | + } |
| 45 | + }, [generateLicenseKey, key, sessionId]); |
| 46 | + |
| 47 | + if (!sessionId) { |
| 48 | + return <PageNotFound noPermission />; |
| 49 | + } |
| 50 | + |
| 51 | + if (key) { |
| 52 | + return <Success licenseKey={key} />; |
| 53 | + } else { |
| 54 | + return ( |
| 55 | + <AuthPageContainer |
| 56 | + title={'Failed to generate the license key'} |
| 57 | + subtitle={ |
| 58 | + <span> |
| 59 | + Failed to generate the license key, please contact our {''} |
| 60 | + <a href="mailto:[email protected]" className={styles.mail}> |
| 61 | + customer support |
| 62 | + </a> |
| 63 | + . |
| 64 | + </span> |
| 65 | + } |
| 66 | + ></AuthPageContainer> |
| 67 | + ); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +const Success = ({ licenseKey }: { licenseKey: string }) => { |
| 72 | + const t = useI18n(); |
| 73 | + const openInAppService = useService(OpenInAppService); |
| 74 | + |
| 75 | + const openAFFiNE = useCallback(() => { |
| 76 | + openInAppService.showOpenInAppPage(); |
| 77 | + }, [openInAppService]); |
| 78 | + |
| 79 | + const onCopy = useCallback(() => { |
| 80 | + copyTextToClipboard(licenseKey) |
| 81 | + .then(success => { |
| 82 | + if (success) { |
| 83 | + notify.success({ |
| 84 | + title: t['com.affine.payment.license-success.copy'](), |
| 85 | + }); |
| 86 | + } |
| 87 | + }) |
| 88 | + .catch(err => { |
| 89 | + console.error(err); |
| 90 | + notify.error({ title: 'Copy failed, please try again later' }); |
| 91 | + }); |
| 92 | + }, [licenseKey, t]); |
| 93 | + |
| 94 | + const subtitle = ( |
| 95 | + <span className={styles.leftContentText}> |
| 96 | + <span>{t['com.affine.payment.license-success.text-1']()}</span> |
| 97 | + <span> |
| 98 | + <Trans |
| 99 | + i18nKey={'com.affine.payment.license-success.text-2'} |
| 100 | + components={{ |
| 101 | + 1: ( |
| 102 | + <a |
| 103 | + |
| 104 | + className={styles.mail} |
| 105 | + /> |
| 106 | + ), |
| 107 | + }} |
| 108 | + /> |
| 109 | + </span> |
| 110 | + </span> |
| 111 | + ); |
| 112 | + return ( |
| 113 | + <AuthPageContainer |
| 114 | + title={t['com.affine.payment.license-success.title']()} |
| 115 | + subtitle={subtitle} |
| 116 | + > |
| 117 | + <div className={styles.content}> |
| 118 | + <div className={styles.licenseKeyContainer}> |
| 119 | + {licenseKey} |
| 120 | + <IconButton |
| 121 | + icon={<CopyIcon />} |
| 122 | + className={styles.icon} |
| 123 | + size="20" |
| 124 | + tooltip={t['Copy']()} |
| 125 | + onClick={onCopy} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + <div>{t['com.affine.payment.license-success.hint']()}</div> |
| 129 | + <div> |
| 130 | + <Button variant="primary" size="extraLarge" onClick={openAFFiNE}> |
| 131 | + {t['com.affine.payment.license-success.open-affine']()} |
| 132 | + </Button> |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + </AuthPageContainer> |
| 136 | + ); |
| 137 | +}; |
0 commit comments