11import { confirmEmail , useStore } from '@tomic/react' ;
22import * as React from 'react' ;
33import { useState } from 'react' ;
4- import { CodeBlock } from '../components/CodeBlock' ;
4+ import toast from 'react-hot-toast' ;
5+ import { Button } from '../components/Button' ;
6+ import { CodeBlockStyled } from '../components/CodeBlock' ;
57import { ContainerNarrow } from '../components/Containers' ;
68import { isDev } from '../config' ;
79import { useSettings } from '../helpers/AppSettings' ;
8- import { handleError } from '../helpers/handlers' ;
910import {
1011 useCurrentSubject ,
1112 useSubjectParam ,
@@ -19,10 +20,13 @@ const ConfirmEmail: React.FunctionComponent = () => {
1920 const [ secret , setSecret ] = useState ( '' ) ;
2021 const store = useStore ( ) ;
2122 const [ token ] = useSubjectParam ( 'token' ) ;
22- const { agent , setAgent } = useSettings ( ) ;
23+ const { setAgent } = useSettings ( ) ;
2324 const [ destinationToGo , setDestination ] = useState < string > ( ) ;
25+ const [ err , setErr ] = useState < Error | undefined > ( undefined ) ;
26+ const [ triedConfirm , setTriedConfirm ] = useState ( false ) ;
2427
25- const handleConfirm = async ( ) => {
28+ const handleConfirm = React . useCallback ( async ( ) => {
29+ setTriedConfirm ( true ) ;
2630 let tokenUrl = subject as string ;
2731
2832 if ( isDev ( ) ) {
@@ -37,37 +41,68 @@ const ConfirmEmail: React.FunctionComponent = () => {
3741 store ,
3842 tokenUrl ,
3943 ) ;
40- setAgent ( newAgent ) ;
4144 setSecret ( newAgent . buildSecret ( ) ) ;
4245 setDestination ( destination ) ;
46+ setAgent ( newAgent ) ;
47+ toast . success ( 'Email confirmed!' ) ;
4348 } catch ( e ) {
44- handleError ( e ) ;
49+ setErr ( e ) ;
50+ }
51+ } , [ subject ] ) ;
52+
53+ if ( ! triedConfirm && subject ) {
54+ handleConfirm ( ) ;
55+ }
56+
57+ if ( err ) {
58+ if ( err . message . includes ( 'expired' ) ) {
59+ return (
60+ < ContainerNarrow >
61+ The link has expired. Request a new one by Registering again.
62+ </ ContainerNarrow >
63+ ) ;
4564 }
46- } ;
4765
48- if ( ! agent ) {
49- return (
50- < ContainerNarrow >
51- < button onClick = { handleConfirm } > confirm</ button >
52- </ ContainerNarrow >
53- ) ;
66+ return < ContainerNarrow > { err ?. message } </ ContainerNarrow > ;
67+ }
68+
69+ if ( secret ) {
70+ return < SavePassphrase secret = { secret } destination = { destinationToGo } /> ;
71+ }
72+
73+ return < ContainerNarrow > Verifying token...</ ContainerNarrow > ;
74+ } ;
75+
76+ function SavePassphrase ( { secret, destination } ) {
77+ const [ copied , setCopied ] = useState ( false ) ;
78+
79+ function copyToClipboard ( ) {
80+ setCopied ( secret ) ;
81+ navigator . clipboard . writeText ( secret || '' ) ;
82+ toast . success ( 'Copied to clipboard' ) ;
5483 }
5584
5685 return (
5786 < ContainerNarrow >
58- < h1 > Save your Passphrase </ h1 >
87+ < h1 > Mail confirmed, please save your passphrase </ h1 >
5988 < p >
6089 Your Passphrase is like your password. Never share it with anyone. Use a
61- password manager to store it securely. You will need this to log in
62- next!
90+ password manager like{ ' ' }
91+ < a href = 'https://bitwarden.com/' target = '_blank' rel = 'noreferrer' >
92+ BitWarden
93+ </ a > { ' ' }
94+ to store it securely.
6395 </ p >
64- < CodeBlock content = { secret } wrapContent />
65- { /* <Button onClick={handleGoToDestination}>Continue here</Button> */ }
66- < a href = { destinationToGo } target = '_blank' rel = 'noreferrer' >
67- Open my new Drive!
68- </ a >
96+ < CodeBlockStyled wrapContent > { secret } </ CodeBlockStyled >
97+ { copied ? (
98+ < a href = { destination } target = '_blank' rel = 'noreferrer' >
99+ { "I've saved my PassPhrase, open my new Drive!" }
100+ </ a >
101+ ) : (
102+ < Button onClick = { copyToClipboard } > Copy Passphrase</ Button >
103+ ) }
69104 </ ContainerNarrow >
70105 ) ;
71- } ;
106+ }
72107
73108export default ConfirmEmail ;
0 commit comments