diff --git a/src/share.tsx b/src/share.tsx index 979d17e..aaf4cc8 100644 --- a/src/share.tsx +++ b/src/share.tsx @@ -36,7 +36,6 @@ function isEmail(value: string): boolean { export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element { const [{ client }] = useW3() const [value, setValue] = useState('') - const [downloadUrl, setDownloadUrl] = useState('') const [sharedEmails, setSharedEmails] = useState<{ email: string, capabilities: string[] }[]>([]) const updateSharedEmails = (delegations: { email: string, capabilities: string[] }[]) => { @@ -82,41 +81,43 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element { } } - async function makeDownloadLink(input: string): Promise { - if (!client) return - - let audience + async function makeDownloadLink(did: string): Promise { try { - audience = DID.parse(input.trim()) - } catch { - setDownloadUrl('') - return - } + if (!client) + throw new Error('missing w3up client') - try { - const delegation = await client.createDelegation(audience, ['*'], { - expiration: Infinity, - }) - const archiveRes = await delegation.archive() - if (archiveRes.error) { - throw new Error('failed to archive delegation', { cause: archiveRes.error }) - } - const blob = new Blob([archiveRes.ok]) - const url = URL.createObjectURL(blob) - setDownloadUrl(url) + const audience = DID.parse(did.trim()) + const delegation = await client.createDelegation(audience, [ + 'space/*', + 'blob/*', + 'store/*', + 'upload/*', + 'access/*', + 'filecoin/*', + 'usage/*', + ], { + expiration: Infinity, + }) + + const archiveRes = await delegation.archive() + if (archiveRes.error) { + throw new Error('failed to archive delegation', { cause: archiveRes.error }) + } + const blob = new Blob([archiveRes.ok]) + const url = URL.createObjectURL(blob) + return url } catch (err: any) { throw new Error(err.message ?? err, { cause: err }) } } - function onSubmit(e: React.FormEvent): void { + async function onSubmit(e: React.FormEvent): Promise { e.preventDefault() if (isDID(value)) { - void makeDownloadLink(value) + const link = await makeDownloadLink(value) + void autoDownload(link) } else if (isEmail(value)) { void shareViaEmail(value) - } else { - setDownloadUrl('') } } @@ -131,6 +132,15 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element { return `did-${method}-${id?.substring(0, 10)}.ucan` } + function autoDownload(resourceUrl: string): void { + const link = document.createElement('a') + link.href = resourceUrl + link.download = downloadName(true, value) + document.body.appendChild(link) + link.click() + document.body.removeChild(link) + } + return (
Share your space
@@ -161,14 +171,8 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element { if (isEmail(value)) { await shareViaEmail(value) } else if (isDID(value)) { - if (!downloadUrl) await makeDownloadLink(value) - - const link = document.createElement('a') - link.href = downloadUrl - link.download = downloadName(true, value) - document.body.appendChild(link) - link.click() - document.body.removeChild(link) + const link = await makeDownloadLink(value) + void autoDownload(link) } }} disabled={!isEmail(value) && !isDID(value)}