From 460998102a90ab44d1809c217e96a86e28f88d01 Mon Sep 17 00:00:00 2001 From: Felipe Forbeck Date: Fri, 20 Sep 2024 15:15:30 -0300 Subject: [PATCH] fix: share space via ucan file --- src/share.tsx | 75 +++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/src/share.tsx b/src/share.tsx index b543c21..9e7d012 100644 --- a/src/share.tsx +++ b/src/share.tsx @@ -33,17 +33,17 @@ function isEmail(value: string): boolean { return !isDID(value) && emailRegex.test(value) } -export function ShareSpace({spaceDID}: {spaceDID: string}): JSX.Element { +export function ShareSpace({ spaceDID }: { spaceDID: string }): JSX.Element { const [{ client }] = useW3() const [value, setValue] = useState('') - const [downloadUrl, setDownloadUrl] = useState('') const [sharedEmails, setSharedEmails] = useState<{ email: string, capabilities: string[] }[]>([]) useEffect(() => { if (client && spaceDID) { - // Find all delegations where the spaceDID is present + // Find all delegations via email where the spaceDID is present const delegations = client.delegations() .filter(d => d.capabilities.some(c => c.with === spaceDID)) + .filter(d => d.audience.did().startsWith('did:mailto:')) .map(d => ({ email: DIDMailTo.toEmail(DIDMailTo.fromString(d.audience.did())), capabilities: d.capabilities.map(c => c.can) @@ -79,41 +79,43 @@ export function ShareSpace({spaceDID}: {spaceDID: string}): 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 (err) { - 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('') } } @@ -128,6 +130,15 @@ export function ShareSpace({spaceDID}: {spaceDID: string}): 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
@@ -158,14 +169,8 @@ export function ShareSpace({spaceDID}: {spaceDID: string}): 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)}