-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathEmail.tsx
More file actions
238 lines (224 loc) · 7.65 KB
/
Copy pathEmail.tsx
File metadata and controls
238 lines (224 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { Input } from '@porto/apps/components'
import { Button, TextButton } from '@porto/ui'
import { cx } from 'cva'
import { Hooks } from 'porto/remote'
import * as React from 'react'
import * as Dialog from '~/lib/Dialog'
import { porto } from '~/lib/Porto'
import { Layout } from '~/routes/-components/Layout'
import { Permissions } from '~/routes/-components/Permissions'
import { StringFormatter } from '~/utils'
import LucideHaze from '~icons/lucide/haze'
import IconScanFace from '~icons/porto/scan-face'
import { SkipPreviews } from './SkipPreviews'
export function Email(props: Email.Props) {
const {
allowBlindSigning,
defaultValue = '',
onApprove,
permissions,
status,
} = props
const [actions, setActions] = React.useState<
readonly ('sign-in' | 'sign-up')[]
>(props.actions ?? ['sign-in', 'sign-up'])
const enableBlindSigningRef = React.useRef<HTMLInputElement>(null)
const account = Hooks.useAccount(porto)
const email = Dialog.useStore((state) =>
account?.address
? state.accountMetadata[account.address]?.email
: undefined,
)
const displayName = (() => {
if (!account) return undefined
if (email) return email
return StringFormatter.truncate(account.address)
})()
const cli = Dialog.useStore((state) =>
state.referrer?.url?.toString().startsWith('cli'),
)
const hostname = Dialog.useStore((state) => state.referrer?.url?.hostname)
const [mode, setMode] = React.useState<'sign-in' | 'sign-up'>('sign-in')
const signingIn = mode === 'sign-in' && status === 'responding'
const signingUp = mode === 'sign-up' && status === 'responding'
const onSignUpSubmit = React.useCallback<
React.FormEventHandler<HTMLFormElement>
>(
async (event) => {
event.preventDefault()
const formData = new FormData(event.target as HTMLFormElement)
const email = formData.get('email')?.toString()
const enableBlindSigning = enableBlindSigningRef.current?.checked
setMode('sign-up')
onApprove({ email, enableBlindSigning, signIn: false })
},
[onApprove],
)
const content = React.useMemo(() => {
if (cli) return undefined
return (
<>
Use <span className="font-medium">Porto</span> to sign in to{' '}
{hostname ? (
<>
<span className="font-medium">{hostname}</span>
{actions.includes('sign-up') ? ' and more' : ''}
</>
) : (
'this website'
)}
.
</>
)
}, [actions, cli, hostname])
const [invalid, setInvalid] = React.useState(false)
return (
<Layout>
<Layout.Header className="flex-grow">
<Layout.Header.Default
content={content}
icon={LucideHaze}
title={actions.includes('sign-up') ? 'Get started' : 'Sign in'}
/>
</Layout.Header>
{permissions ? (
<Permissions title="Permissions requested" {...permissions} />
) : allowBlindSigning ? (
<>
<div className="px-3">
<SkipPreviews ref={enableBlindSigningRef} />
</div>
<div className="h-4" />
</>
) : null}
<div className="group flex min-h-[48px] w-full flex-col items-center justify-center space-y-3 px-3 pb-3">
{actions.includes('sign-in') && (
<Button
data-testid="sign-in"
disabled={status === 'loading' || signingUp}
icon={<IconScanFace className="size-5.25" />}
loading={signingIn && 'Signing in…'}
onClick={() => {
const enableBlindSigning = enableBlindSigningRef.current?.checked
setMode('sign-in')
onApprove({ enableBlindSigning, signIn: true })
}}
type="button"
variant="primary"
width="full"
>
{actions.includes('sign-up')
? 'Sign in with Porto'
: 'Continue with Porto'}
</Button>
)}
{actions.includes('sign-up') ? (
<form
className="flex w-full flex-grow flex-col gap-2"
onInvalid={(event) => {
event.preventDefault()
setInvalid(true)
}}
onSubmit={onSignUpSubmit}
>
{/* If "Sign in" button is present, show the "First time?" text for sign up. */}
{actions.includes('sign-in') && (
<div className="-tracking-[2.8%] flex items-center whitespace-nowrap text-[12px] text-th_base-secondary leading-[17px]">
First time?
<div className="ms-2 h-px w-full bg-th_separator" />
</div>
)}
<div className="relative flex items-center">
<label className="sr-only" htmlFor="email">
Email
</label>
<Input
className={cx(
'w-full bg-th_field',
invalid && 'not-focus-visible:border-th_negative',
)}
defaultValue={defaultValue}
disabled={status === 'loading' || signingIn}
name="email"
onChange={() => setInvalid(false)}
placeholder="example@ithaca.xyz"
type="email"
/>
<div className="-tracking-[2.8%] absolute end-3 text-[12px] text-th_base-secondary leading-normal">
Optional
</div>
</div>
<Button
data-testid="sign-up"
disabled={status === 'loading' || signingIn}
loading={signingUp && 'Signing up…'}
size="medium"
type="submit"
variant={actions.includes('sign-in') ? 'secondary' : 'primary'}
>
{invalid ? (
'Invalid email'
) : actions.includes('sign-in') ? (
'Create Porto account'
) : (
<div className="flex gap-2">
<IconScanFace className="size-5.25" />
Sign up with Porto
</div>
)}
</Button>
</form>
) : (
// If no sign up button, this means the user is already logged in, however
// the user may want to sign in with a different passkey.
<div className="flex w-full justify-between gap-2">
<div>
<span className="text-th_base-secondary">Using</span>{' '}
{displayName}
</div>
<div className="flex items-center gap-0.5">
<TextButton
color="link"
onClick={() => {
const enableBlindSigning =
enableBlindSigningRef.current?.checked
onApprove({
enableBlindSigning,
selectAccount: true,
signIn: true,
})
}}
>
Switch
</TextButton>
<div className="text-th_base-secondary">⋅</div>
<TextButton
color="link"
onClick={() => {
setActions(['sign-up'])
}}
>
Sign up
</TextButton>
</div>
</div>
)}
</div>
</Layout>
)
}
export namespace Email {
export type Props = {
actions?: readonly ('sign-in' | 'sign-up')[]
allowBlindSigning?: boolean
defaultValue?: string | undefined
onApprove: (p: {
email?: string
selectAccount?: boolean
enableBlindSigning?: boolean
signIn?: boolean
}) => void
permissions?: Permissions.Props
status?: 'loading' | 'responding' | undefined
}
}