-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathConfigurationFlow.tsx
506 lines (484 loc) · 14.7 KB
/
ConfigurationFlow.tsx
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
'use client'
import {
ArrowLeftOnRectangleIcon,
ArrowRightCircleIcon,
ArrowRightOnRectangleIcon,
} from '@heroicons/react/20/solid'
import { useMutation } from '@tanstack/react-query'
import Link from 'next/link'
import { ComponentProps, ReactElement, cloneElement, useState } from 'react'
import { ErrorInfo } from '@/common/ErrorInfo'
import { Checkbox, Input } from '@/common/forms'
import { Loading } from '@/common/Loader'
import {
OzoneConfig,
OzoneConfigFull,
getServiceUrlFromDoc,
withDocAndMeta,
} from '@/lib/client-config'
import { Agent } from '@atproto/api'
import { useAuthContext, useAuthDid, useAuthIdentifier } from './AuthContext'
import { ConfigurationState } from './ConfigurationContext'
export type ConfigurationFlowProps = {
state: ConfigurationState
config: OzoneConfig | undefined
error: unknown
labelerAgent: Agent | undefined
skipRecordCreation: () => void | Promise<void>
createRecord: () => void | Promise<void>
reconfigure: () => void | Promise<void>
}
export function ConfigurationFlow({
state,
config,
error,
labelerAgent,
skipRecordCreation,
createRecord,
reconfigure,
}: ConfigurationFlowProps) {
const { signOut } = useAuthContext()
const authDid = useAuthDid()
const authIdentifier = useAuthIdentifier()
if (state === ConfigurationState.Ready) {
return <Loading message="Redirecting..." />
}
if (state === ConfigurationState.Unauthorized) {
return (
<>
<ErrorInfo type="warn" className="mt-2">
{`You're`} not logged-in. Please login using your Ozone service
account in order to configure Ozone.
</ErrorInfo>
<Button
className="w-full mt-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={signOut}
>
Restart
</Button>
</>
)
}
if (state === ConfigurationState.Pending) {
if (error != null) {
return (
<>
<ErrorInfo type="error" className="mt-2">
We encountered an error loading the service configuration:
<br />
{error instanceof Error ? error.message : String(error)}
</ErrorInfo>
<Button
className="w-full mt-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={() => reconfigure()}
>
Retry
</Button>
</>
)
}
return <Loading message="Loading configuration..." />
}
if (!config || !labelerAgent) {
// Should never happen. Required for type safety.
throw new Error('Invalid state: configuration is missing')
}
if (config.needs.key || config.needs.service) {
if (authDid !== config.did) {
return (
<>
<ErrorInfo type="warn" className="mt-2">
{`You're`} logged in as {authIdentifier}. Please login as{' '}
{config.handle || 'your Ozone service account'} in order to
configure Ozone.
</ErrorInfo>
<Button
className="w-full mt-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={signOut}
>
Restart
</Button>
</>
)
}
if (config.did.startsWith('did:web:')) {
return (
<>
<ErrorInfo type="warn" className="mt-2">
You must configure your identity on your own if {`you're`} using a
did:web. You will need to add a service with id{' '}
{`"atproto_labeler"`} and verification method with id{' '}
{`"atproto_label"`}.
</ErrorInfo>
<Button
className="w-full mt-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={signOut}
>
Restart
</Button>
</>
)
}
if (!config.doc) {
return (
<>
<ErrorInfo type="warn" className="mt-2">
We could not find identity information for the account{' '}
<b>{authIdentifier}</b>. Are you sure this account has an identity
on the network?
</ErrorInfo>
<Button
className="w-full mt-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={signOut}
>
Restart
</Button>
</>
)
}
if (!config.meta) {
return (
<>
<ErrorInfo type="warn" className="mt-2">
We could not find your Ozone service configuration. Please ensure
{`you're`} currently on the domain where your Ozone service is
running.
</ErrorInfo>
<Button
className="w-full mt-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={signOut}
>
Restart
</Button>
</>
)
}
return (
<IdentityConfigurationFlow
config={withDocAndMeta(config)}
onIdentityUpdated={reconfigure}
/>
)
}
if (
(!config.matching.key || !config.matching.service) &&
config.doc &&
config.meta
) {
return (
<ErrorInfo type="warn" className="mt-2">
{`There's`} a configuration issue: you will need to update your identity
or your Ozone service.
<br />
<br />
{!config.matching.service && (
<>
Your Ozone service is running at <b>{config.meta.url}</b>, but your
identity points to{' '}
<b>{getServiceUrlFromDoc(config.doc, 'atproto_labeler')}</b>.
</>
)}
{!config.matching.service && !config.matching.key && (
<>
<br />
<br />
</>
)}
{!config.matching.key && (
<>
Your Ozone service is configured with a different key than the key
associated with {config.handle}.
</>
)}
</ErrorInfo>
)
}
if (config.needs.record) {
return (
<RecordConfigurationFlow
config={config}
onSkip={skipRecordCreation}
onCreate={createRecord}
/>
)
}
return <Loading message="Redirecting..." />
}
function IdentityConfigurationFlow({
config,
onIdentityUpdated,
}: {
config: OzoneConfigFull
onIdentityUpdated: () => unknown
}) {
const [token, setToken] = useState('')
const { pdsAgent, signOut } = useAuthContext()
const requestPlcOperationSignature = useMutation({
mutationKey: [pdsAgent.assertDid, config.did],
mutationFn: async () => {
await pdsAgent.com.atproto.identity.requestPlcOperationSignature()
},
})
const submitPlcOperation = useMutation({
mutationKey: [pdsAgent.assertDid, config.did, config.updatedAt],
mutationFn: async () => {
await updatePlcIdentity(pdsAgent, token, config)
await onIdentityUpdated()
},
})
return (
<div className="text-gray-600 dark:text-gray-100 mt-4">
<p className="mt-4">
It looks like the network doesn't understand that{' '}
<b>{config.handle}</b> is a moderation service yet. Let's get you
setup!
</p>
<p className="mt-4">
We will be associating your service running at <b>{config.meta?.url}</b>{' '}
with your moderation account <b>{config.handle}</b>. It is highly{' '}
recommended <i>not</i> to use a personal account for this.
</p>
{requestPlcOperationSignature.isError && (
<ErrorInfo type="warn" className="mt-4">
We weren't able to send a confirmation email. Try sending again,
or seek support.
</ErrorInfo>
)}
{!requestPlcOperationSignature.isSuccess && (
<div className="flex mt-4">
<Button
disabled={requestPlcOperationSignature.isLoading}
className="w-full mr-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={signOut}
>
Cancel
</Button>
<Button
disabled={requestPlcOperationSignature.isLoading}
className="w-full ml-2"
icon={<ArrowRightCircleIcon />}
onClick={() => requestPlcOperationSignature.mutate()}
>
Continue
</Button>
</div>
)}
{requestPlcOperationSignature.isSuccess && (
<>
<p className="mt-4">
You should receive an email containing a confirmation code for a{' '}
{'"PLC Update"'}. Please enter it below:
</p>
<Input
type="text"
placeholder="Confirmation Code"
className="block w-full mt-2"
value={token}
autoFocus
onKeyDown={(ev) => {
// This is the only input field on this screen but it's not wrapped in a form
// so on Enter key, let's emulate form submission
if (
ev.key === 'Enter' &&
!submitPlcOperation.isLoading &&
!submitPlcOperation.isSuccess &&
token
) {
submitPlcOperation.mutate()
}
}}
onChange={(ev) => setToken(ev.target.value)}
/>
{submitPlcOperation.isError && (
<ErrorInfo type="warn" className="mt-4">
Submitting your PLC operation failed:
<br />
{submitPlcOperation.error?.['message']}
</ErrorInfo>
)}
<div className="flex mt-4">
<Button
disabled={
submitPlcOperation.isLoading || submitPlcOperation.isSuccess
}
className="w-full mr-2"
icon={<ArrowLeftOnRectangleIcon />}
onClick={signOut}
>
Cancel
</Button>
<Button
disabled={
submitPlcOperation.isLoading ||
submitPlcOperation.isSuccess ||
!token
}
className="w-full ml-2"
icon={<ArrowRightCircleIcon />}
onClick={() => submitPlcOperation.mutate()}
>
Submit
</Button>
</div>
</>
)}
</div>
)
}
function RecordConfigurationFlow({
config,
onSkip,
onCreate,
}: {
config: OzoneConfig
onSkip: () => void | Promise<void>
onCreate: () => void | Promise<void>
}) {
const [checked, setChecked] = useState(false)
const authDid = useAuthDid()
const identifier = useAuthIdentifier()
const putServiceRecord = useMutation({
mutationFn: async () => onCreate(),
})
return (
<div className="text-gray-600 dark:text-gray-100 mt-4">
<p className="mt-4">
Your Ozone service configuration and your identity are in sync.
</p>
<p className="mt-4">
The final step is to publish a record that will allow your account
appear as a moderation service in the Bluesky application. Users of the
app will be able to start using the labels you publish.
</p>
{config.needs.pds && (
<ErrorInfo type="warn" className="mt-4">
Your account {config.handle} needs to have a repository hosted on a
PDS before we can create the record.
<br />
<br />
You may skip this step and come back to it next time you login.
</ErrorInfo>
)}
{authDid !== config.did && (
<ErrorInfo type="warn" className="mt-4">
{`You're`} logged in as {identifier}. Please login as{' '}
{config.handle || 'your Ozone service account'} in order to configure
Ozone.
<br />
<br />
You may skip this step and come back to it next time you login.
</ErrorInfo>
)}
{putServiceRecord.isError && (
<ErrorInfo type="warn" className="mt-4">
We {`weren't`} able to create the service record. Please try again, or
seek support.
</ErrorInfo>
)}
<div className="flex mt-4">
<Button
disabled={putServiceRecord.isLoading || putServiceRecord.isSuccess}
className="w-full mr-2"
icon={<ArrowRightOnRectangleIcon />}
onClick={onSkip}
>
Skip
</Button>
<Button
disabled={
putServiceRecord.isLoading ||
putServiceRecord.isSuccess ||
authDid !== config.did ||
config.needs.pds ||
!checked
}
className="w-full ml-2"
icon={<ArrowRightCircleIcon />}
onClick={() => putServiceRecord.mutateAsync()}
>
Submit
</Button>
</div>
<p className="text-center mt-2">
<Checkbox
checked={checked}
onChange={(ev) => setChecked(ev.target.checked)}
label={
<>
I have read the{' '}
<Link
href="https://bsky.social/about/support/community-guidelines#labeler"
target="_blank"
className="text-blue-500"
>
Bluesky Labeler Community Guidelines
</Link>
</>
}
/>
</p>
</div>
)
}
function Button({
children,
className = '',
icon,
...others
}: ComponentProps<'button'> & { icon: ReactElement }) {
return (
<button
type="button"
className={`group relative flex justify-center rounded-md border border-transparent py-2 px-4 text-sm font-medium text-white focus:outline-none focus:ring-2 focus:ring-rose-500 dark:focus:ring-slate-500 focus:ring-offset-2 bg-rose-600 dark:bg-teal-600 hover:bg-rose-700 dark:hover:bg-teal-700 dark:disabled:bg-gray-500 disabled:bg-gray-500 ${className}`}
{...others}
>
<span className="absolute inset-y-0 left-0 flex items-center pl-3">
{cloneElement(icon, {
className: `h-5 w-5 text-rose-500 dark:text-gray-50 group-hover:text-rose-400 dark:group-hover:text-gray-100`,
'aria-hidden': 'true',
})}
</span>
{children}
</button>
)
}
async function updatePlcIdentity(
client: Agent,
token: string,
config: OzoneConfigFull,
) {
const services = config.needs.service ? { ...config.doc.services } : undefined
if (services) {
services['atproto_labeler'] = {
type: 'AtprotoLabeler',
endpoint: config.meta.url,
}
}
const verificationMethods = config.needs.key
? { ...config.doc.verificationMethods }
: undefined
if (verificationMethods) {
verificationMethods['atproto_label'] = config.meta.publicKey
}
const { data: signed } = await client.com.atproto.identity.signPlcOperation({
token,
verificationMethods,
services,
})
await client.com.atproto.identity.submitPlcOperation({
operation: signed.operation,
})
if (config.handle) {
// @NOTE temp hack to push an identity op through
await client.com.atproto.identity.updateHandle({
handle: config.handle,
})
}
}