1
1
import { ChangeEvent , useEffect , useState } from 'react'
2
- import { useW3 } from '@w3ui/react'
2
+ import { SpaceDID , useW3 } from '@w3ui/react'
3
3
import { extract } from '@ucanto/core/delegation'
4
4
import type { PropsWithChildren } from 'react'
5
5
import type { Delegation } from '@ucanto/interface'
@@ -23,7 +23,7 @@ function isDID(value: string): boolean {
23
23
try {
24
24
DID . parse ( value . trim ( ) )
25
25
return true
26
- } catch ( err ) {
26
+ } catch {
27
27
return false
28
28
}
29
29
}
@@ -33,22 +33,30 @@ function isEmail(value: string): boolean {
33
33
return ! isDID ( value ) && emailRegex . test ( value )
34
34
}
35
35
36
- export function ShareSpace ( { spaceDID} : { spaceDID : string } ) : JSX . Element {
36
+ export function ShareSpace ( { spaceDID } : { spaceDID : SpaceDID } ) : JSX . Element {
37
37
const [ { client } ] = useW3 ( )
38
38
const [ value , setValue ] = useState ( '' )
39
39
const [ downloadUrl , setDownloadUrl ] = useState ( '' )
40
40
const [ sharedEmails , setSharedEmails ] = useState < { email : string , capabilities : string [ ] } [ ] > ( [ ] )
41
41
42
+ const updateSharedEmails = ( delegations : { email : string , capabilities : string [ ] } [ ] ) => {
43
+ setSharedEmails ( prev => {
44
+ const newEmails = delegations . filter ( d => ! prev . some ( item => item . email === d . email ) )
45
+ return [ ...prev , ...newEmails ]
46
+ } )
47
+ }
48
+
42
49
useEffect ( ( ) => {
43
50
if ( client && spaceDID ) {
44
- // Find all delegations where the spaceDID is present
51
+ // Find all delegations via email where the spaceDID is present
45
52
const delegations = client . delegations ( )
46
53
. filter ( d => d . capabilities . some ( c => c . with === spaceDID ) )
54
+ . filter ( d => d . audience . did ( ) . startsWith ( 'did:mailto:' ) )
47
55
. map ( d => ( {
48
56
email : DIDMailTo . toEmail ( DIDMailTo . fromString ( d . audience . did ( ) ) ) ,
49
57
capabilities : d . capabilities . map ( c => c . can )
50
58
} ) )
51
- setSharedEmails ( delegations )
59
+ updateSharedEmails ( delegations )
52
60
}
53
61
} , [ client , spaceDID ] )
54
62
@@ -67,12 +75,7 @@ export function ShareSpace({spaceDID}: {spaceDID: string}): JSX.Element {
67
75
const delegation = await client . shareSpace ( delegatedEmail , space . did ( ) )
68
76
69
77
const next = { email : delegatedEmail , capabilities : delegation . capabilities . map ( c => c . can ) }
70
- setSharedEmails ( prev => {
71
- if ( prev . some ( item => item . email === next . email ) ) {
72
- return prev
73
- }
74
- return [ ...prev , next ]
75
- } )
78
+ updateSharedEmails ( [ next ] )
76
79
setValue ( '' )
77
80
} catch ( err ) {
78
81
console . error ( err )
@@ -85,7 +88,7 @@ export function ShareSpace({spaceDID}: {spaceDID: string}): JSX.Element {
85
88
let audience
86
89
try {
87
90
audience = DID . parse ( input . trim ( ) )
88
- } catch ( err ) {
91
+ } catch {
89
92
setDownloadUrl ( '' )
90
93
return
91
94
}
0 commit comments