Skip to content

Commit 05a88fc

Browse files
committed
fix: share space via ucan file
1 parent d3c6fc1 commit 05a88fc

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

src/share.tsx

+36-34
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[] }[]) => {
@@ -82,41 +81,42 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
8281
}
8382
}
8483

85-
async function makeDownloadLink(input: string): Promise<void> {
86-
if (!client) return
87-
88-
let audience
84+
async function makeDownloadLink(did: string): Promise<string> {
8985
try {
90-
audience = DID.parse(input.trim())
91-
} catch {
92-
setDownloadUrl('')
93-
return
94-
}
86+
if (!client)
87+
throw new Error('missing w3up client')
9588

96-
try {
97-
const delegation = await client.createDelegation(audience, ['*'], {
98-
expiration: Infinity,
99-
})
100-
const archiveRes = await delegation.archive()
101-
if (archiveRes.error) {
102-
throw new Error('failed to archive delegation', { cause: archiveRes.error })
103-
}
104-
const blob = new Blob([archiveRes.ok])
105-
const url = URL.createObjectURL(blob)
106-
setDownloadUrl(url)
89+
const audience = DID.parse(did.trim())
90+
const delegation = await client.createDelegation(audience, [
91+
'space/*',
92+
'blob/*',
93+
'store/*',
94+
'upload/*',
95+
'access/*',
96+
'filecoin/*',
97+
'usage/*',
98+
], {
99+
expiration: Infinity,
100+
})
101+
102+
const archiveRes = await delegation.archive()
103+
if (archiveRes.error) {
104+
throw new Error('failed to archive delegation', { cause: archiveRes.error })
105+
}
106+
const blob = new Blob([archiveRes.ok])
107+
const url = URL.createObjectURL(blob)
108+
return url
107109
} catch (err: any) {
108110
throw new Error(err.message ?? err, { cause: err })
109111
}
110112
}
111113

112-
function onSubmit(e: React.FormEvent<HTMLFormElement>): void {
114+
async function onSubmit(e: React.FormEvent<HTMLFormElement>): Promise<void> {
113115
e.preventDefault()
114116
if (isDID(value)) {
115-
void makeDownloadLink(value)
117+
void autoDownload(value)
116118
} else if (isEmail(value)) {
117119
void shareViaEmail(value)
118-
} else {
119-
setDownloadUrl('')
120120
}
121121
}
122122

@@ -131,6 +131,16 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
131131
return `did-${method}-${id?.substring(0, 10)}.ucan`
132132
}
133133

134+
async function autoDownload(value: string): Promise<void> {
135+
const resourceURL = await makeDownloadLink(value)
136+
const link = document.createElement('a')
137+
link.href = resourceURL
138+
link.download = downloadName(true, value)
139+
document.body.appendChild(link)
140+
link.click()
141+
document.body.removeChild(link)
142+
}
143+
134144
return (
135145
<div className='max-w-4xl'>
136146
<Header>Share your space</Header>
@@ -157,18 +167,10 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
157167
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`}
158168
onClick={async (e) => {
159169
e.preventDefault()
160-
161170
if (isEmail(value)) {
162171
await shareViaEmail(value)
163172
} else if (isDID(value)) {
164-
if (!downloadUrl) await makeDownloadLink(value)
165-
166-
const link = document.createElement('a')
167-
link.href = downloadUrl
168-
link.download = downloadName(true, value)
169-
document.body.appendChild(link)
170-
link.click()
171-
document.body.removeChild(link)
173+
await autoDownload(value)
172174
}
173175
}}
174176
disabled={!isEmail(value) && !isDID(value)}

0 commit comments

Comments
 (0)