Skip to content

Commit d72ffbb

Browse files
committed
fix: share space via ucan file
1 parent 896079b commit d72ffbb

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/share.tsx

+28-26
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function isEmail(value: string): boolean {
3636
export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
3737
const [{ client }] = useW3()
3838
const [value, setValue] = useState('')
39-
const [downloadUrl, setDownloadUrl] = useState('')
4039
const [sharedEmails, setSharedEmails] = useState<{ email: string, capabilities: string[] }[]>([])
4140

4241
const updateSharedEmails = (delegations: { email: string, capabilities: string[] }[]) => {
@@ -125,41 +124,42 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
125124
}
126125
}
127126

128-
async function makeDownloadLink(input: string): Promise<void> {
129-
if (!client) return
130-
131-
let audience
127+
async function makeDownloadLink(did: string): Promise<string> {
132128
try {
133-
audience = DID.parse(input.trim())
134-
} catch {
135-
setDownloadUrl('')
136-
return
137-
}
129+
if (!client)
130+
throw new Error('missing w3up client')
138131

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+
], {
141142
expiration: Infinity,
142143
})
144+
143145
const archiveRes = await delegation.archive()
144146
if (archiveRes.error) {
145147
throw new Error('failed to archive delegation', { cause: archiveRes.error })
146148
}
147149
const blob = new Blob([archiveRes.ok])
148150
const url = URL.createObjectURL(blob)
149-
setDownloadUrl(url)
151+
return url
150152
} catch (err: any) {
151153
throw new Error(err.message ?? err, { cause: err })
152154
}
153155
}
154156

155-
function onSubmit(e: React.FormEvent<HTMLFormElement>): void {
157+
async function onSubmit(e: React.FormEvent<HTMLFormElement>): Promise<void> {
156158
e.preventDefault()
157159
if (isDID(value)) {
158-
void makeDownloadLink(value)
160+
void autoDownload(value)
159161
} else if (isEmail(value)) {
160162
void shareViaEmail(value)
161-
} else {
162-
setDownloadUrl('')
163163
}
164164
}
165165

@@ -174,6 +174,16 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
174174
return `did-${method}-${id?.substring(0, 10)}.ucan`
175175
}
176176

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+
177187
return (
178188
<div className='max-w-4xl'>
179189
<Header>Share your space</Header>
@@ -200,18 +210,10 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
200210
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`}
201211
onClick={async (e) => {
202212
e.preventDefault()
203-
204213
if (isEmail(value)) {
205214
await shareViaEmail(value)
206215
} 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)
215217
}
216218
}}
217219
disabled={!isEmail(value) && !isDID(value)}

0 commit comments

Comments
 (0)