Skip to content

Commit

Permalink
fix: share space via ucan file
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Sep 23, 2024
1 parent d3c6fc1 commit 8a4fdc7
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions src/share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[] }[]) => {
Expand Down Expand Up @@ -82,41 +81,43 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
}
}

async function makeDownloadLink(input: string): Promise<void> {
if (!client) return

let audience
async function makeDownloadLink(did: string): Promise<string> {
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<HTMLFormElement>): void {
async function onSubmit(e: React.FormEvent<HTMLFormElement>): Promise<void> {
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('')
}
}

Expand All @@ -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 (
<div className='max-w-4xl'>
<Header>Share your space</Header>
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit 8a4fdc7

Please sign in to comment.