@@ -36,7 +36,6 @@ function isEmail(value: string): boolean {
36
36
export function ShareSpace ( { spaceDID } : { spaceDID : SpaceDID } ) : JSX . Element {
37
37
const [ { client } ] = useW3 ( )
38
38
const [ value , setValue ] = useState ( '' )
39
- const [ downloadUrl , setDownloadUrl ] = useState ( '' )
40
39
const [ sharedEmails , setSharedEmails ] = useState < { email : string , capabilities : string [ ] } [ ] > ( [ ] )
41
40
42
41
const updateSharedEmails = ( delegations : { email : string , capabilities : string [ ] } [ ] ) => {
@@ -82,41 +81,42 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
82
81
}
83
82
}
84
83
85
- async function makeDownloadLink ( input : string ) : Promise < void > {
86
- if ( ! client ) return
87
-
88
- let audience
84
+ async function makeDownloadLink ( did : string ) : Promise < string > {
89
85
try {
90
- audience = DID . parse ( input . trim ( ) )
91
- } catch {
92
- setDownloadUrl ( '' )
93
- return
94
- }
86
+ if ( ! client )
87
+ throw new Error ( 'missing w3up client' )
95
88
96
- try {
97
- const delegation = await client . createDelegation ( audience , [ '*' ] , {
98
- expiration : Infinity ,
99
- } )
100
- const archiveRes = await delegation . archive ( )
101
- if ( archiveRes . error ) {
102
- throw new Error ( 'failed to archive delegation' , { cause : archiveRes . error } )
103
- }
104
- const blob = new Blob ( [ archiveRes . ok ] )
105
- const url = URL . createObjectURL ( blob )
106
- setDownloadUrl ( url )
89
+ const audience = DID . parse ( did . trim ( ) )
90
+ const delegation = await client . createDelegation ( audience , [
91
+ 'space/*' ,
92
+ 'blob/*' ,
93
+ 'store/*' ,
94
+ 'upload/*' ,
95
+ 'access/*' ,
96
+ 'filecoin/*' ,
97
+ 'usage/*' ,
98
+ ] , {
99
+ expiration : Infinity ,
100
+ } )
101
+
102
+ const archiveRes = await delegation . archive ( )
103
+ if ( archiveRes . error ) {
104
+ throw new Error ( 'failed to archive delegation' , { cause : archiveRes . error } )
105
+ }
106
+ const blob = new Blob ( [ archiveRes . ok ] )
107
+ const url = URL . createObjectURL ( blob )
108
+ return url
107
109
} catch ( err : any ) {
108
110
throw new Error ( err . message ?? err , { cause : err } )
109
111
}
110
112
}
111
113
112
- function onSubmit ( e : React . FormEvent < HTMLFormElement > ) : void {
114
+ async function onSubmit ( e : React . FormEvent < HTMLFormElement > ) : Promise < void > {
113
115
e . preventDefault ( )
114
116
if ( isDID ( value ) ) {
115
- void makeDownloadLink ( value )
117
+ void autoDownload ( value )
116
118
} else if ( isEmail ( value ) ) {
117
119
void shareViaEmail ( value )
118
- } else {
119
- setDownloadUrl ( '' )
120
120
}
121
121
}
122
122
@@ -131,6 +131,16 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
131
131
return `did-${ method } -${ id ?. substring ( 0 , 10 ) } .ucan`
132
132
}
133
133
134
+ async function autoDownload ( value : string ) : Promise < void > {
135
+ const resourceURL = await makeDownloadLink ( value )
136
+ const link = document . createElement ( 'a' )
137
+ link . href = resourceURL
138
+ link . download = downloadName ( true , value )
139
+ document . body . appendChild ( link )
140
+ link . click ( )
141
+ document . body . removeChild ( link )
142
+ }
143
+
134
144
return (
135
145
< div className = 'max-w-4xl' >
136
146
< Header > Share your space</ Header >
@@ -157,18 +167,10 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
157
167
className = { `inline-block bg-hot-red border border-hot-red ${ isEmail ( value ) || isDID ( value ) ? 'hover:bg-white hover:text-hot-red' : 'opacity-20' } font-epilogue text-white uppercase text-sm px-6 py-2 rounded-full whitespace-nowrap` }
158
168
onClick = { async ( e ) => {
159
169
e . preventDefault ( )
160
-
161
170
if ( isEmail ( value ) ) {
162
171
await shareViaEmail ( value )
163
172
} else if ( isDID ( value ) ) {
164
- if ( ! downloadUrl ) await makeDownloadLink ( value )
165
-
166
- const link = document . createElement ( 'a' )
167
- link . href = downloadUrl
168
- link . download = downloadName ( true , value )
169
- document . body . appendChild ( link )
170
- link . click ( )
171
- document . body . removeChild ( link )
173
+ await autoDownload ( value )
172
174
}
173
175
} }
174
176
disabled = { ! isEmail ( value ) && ! isDID ( value ) }
0 commit comments