@@ -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 [ ] } [ ] ) => {
@@ -125,41 +124,42 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
125
124
}
126
125
}
127
126
128
- async function makeDownloadLink ( input : string ) : Promise < void > {
129
- if ( ! client ) return
130
-
131
- let audience
127
+ async function makeDownloadLink ( did : string ) : Promise < string > {
132
128
try {
133
- audience = DID . parse ( input . trim ( ) )
134
- } catch {
135
- setDownloadUrl ( '' )
136
- return
137
- }
129
+ if ( ! client )
130
+ throw new Error ( 'missing w3up client' )
138
131
139
- try {
140
- const delegation = await client . createDelegation ( audience , [ '*' ] , {
132
+ const audience = DID . parse ( did . trim ( ) )
133
+ const delegation = await client . createDelegation ( audience , [
134
+ 'space/*' ,
135
+ 'store/*' ,
136
+ 'upload/*' ,
137
+ 'access/*' ,
138
+ 'usage/*' ,
139
+ // @ts -expect-error (FIXME: https://github.com/storacha/w3up/issues/1554)
140
+ 'filecoin/*' ,
141
+ ] , {
141
142
expiration : Infinity ,
142
143
} )
144
+
143
145
const archiveRes = await delegation . archive ( )
144
146
if ( archiveRes . error ) {
145
147
throw new Error ( 'failed to archive delegation' , { cause : archiveRes . error } )
146
148
}
147
149
const blob = new Blob ( [ archiveRes . ok ] )
148
150
const url = URL . createObjectURL ( blob )
149
- setDownloadUrl ( url )
151
+ return url
150
152
} catch ( err : any ) {
151
153
throw new Error ( err . message ?? err , { cause : err } )
152
154
}
153
155
}
154
156
155
- function onSubmit ( e : React . FormEvent < HTMLFormElement > ) : void {
157
+ async function onSubmit ( e : React . FormEvent < HTMLFormElement > ) : Promise < void > {
156
158
e . preventDefault ( )
157
159
if ( isDID ( value ) ) {
158
- void makeDownloadLink ( value )
160
+ void autoDownload ( value )
159
161
} else if ( isEmail ( value ) ) {
160
162
void shareViaEmail ( value )
161
- } else {
162
- setDownloadUrl ( '' )
163
163
}
164
164
}
165
165
@@ -174,6 +174,16 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
174
174
return `did-${ method } -${ id ?. substring ( 0 , 10 ) } .ucan`
175
175
}
176
176
177
+ async function autoDownload ( value : string ) : Promise < void > {
178
+ const resourceURL = await makeDownloadLink ( value )
179
+ const link = document . createElement ( 'a' )
180
+ link . href = resourceURL
181
+ link . download = downloadName ( true , value )
182
+ document . body . appendChild ( link )
183
+ link . click ( )
184
+ document . body . removeChild ( link )
185
+ }
186
+
177
187
return (
178
188
< div className = 'max-w-4xl' >
179
189
< Header > Share your space</ Header >
@@ -200,18 +210,10 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
200
210
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` }
201
211
onClick = { async ( e ) => {
202
212
e . preventDefault ( )
203
-
204
213
if ( isEmail ( value ) ) {
205
214
await shareViaEmail ( value )
206
215
} else if ( isDID ( value ) ) {
207
- if ( ! downloadUrl ) await makeDownloadLink ( value )
208
-
209
- const link = document . createElement ( 'a' )
210
- link . href = downloadUrl
211
- link . download = downloadName ( true , value )
212
- document . body . appendChild ( link )
213
- link . click ( )
214
- document . body . removeChild ( link )
216
+ await autoDownload ( value )
215
217
}
216
218
} }
217
219
disabled = { ! isEmail ( value ) && ! isDID ( value ) }
0 commit comments