Skip to content

Commit 64dc68d

Browse files
authored
refactor: using the new client method for space sharing (#175)
Resolves: storacha/project-tracking#238 The client provides a new method to share a Space via delegation, so we don't need to implement that on the Console App side, we just need to call the new method.
1 parent 0eb122d commit 64dc68d

File tree

1 file changed

+10
-58
lines changed

1 file changed

+10
-58
lines changed

src/share.tsx

+10-58
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ChangeEvent, useEffect, useState } from 'react'
22
import { SpaceDID, useW3 } from '@w3ui/react'
33
import { extract } from '@ucanto/core/delegation'
44
import type { PropsWithChildren } from 'react'
5-
import type { Delegation } from '@ucanto/interface'
5+
import type { Capabilities, Delegation } from '@ucanto/interface'
66
import { SpacePreview } from './components/SpaceCreator'
77
import { H2, H3 } from '@/components/Text'
88
import CopyButton from './components/CopyButton'
@@ -64,64 +64,16 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
6464
throw new Error(`Client not found`)
6565
}
6666

67-
const currentSpace = client.agent.currentSpace()
68-
try {
69-
const space = client.spaces().find(s => s.did() === spaceDID)
70-
if (!space) {
71-
throw new Error(`Could not find space to share`)
72-
}
73-
74-
const delegatedEmail = DIDMailTo.email(email)
75-
76-
// FIXME (fforbeck): enable shareSpace function call after @w3ui/react lib is updated to v2.4.0 and the issue with blobs are solved
77-
// const delegation = await client.shareSpace(delegatedEmail, space.did())
78-
79-
// Make sure the agent is using the shared space before delegating
80-
await client.agent.setCurrentSpace(spaceDID)
81-
82-
// Delegate capabilities to the delegate account to access the **current space**
83-
const delegation = await client.createDelegation(
84-
{
85-
did: () => DIDMailTo.fromEmail(delegatedEmail),
86-
},
87-
[
88-
'space/*',
89-
'store/*',
90-
'upload/*',
91-
'access/*',
92-
'usage/*',
93-
// @ts-expect-error (FIXME: https://github.com/storacha/w3up/issues/1554)
94-
'filecoin/*',
95-
], {
96-
expiration: Infinity
97-
}
98-
)
99-
100-
const sharingResult = await client.capability.access.delegate({
101-
space: spaceDID,
102-
delegations: [delegation],
103-
})
104-
105-
if (sharingResult.error) {
106-
throw new Error(
107-
`failed to share space with ${delegatedEmail}: ${sharingResult.error.message}`,
108-
{
109-
cause: sharingResult.error,
110-
}
111-
)
112-
}
113-
114-
const next = { email: delegatedEmail, capabilities: delegation.capabilities.map(c => c.can) }
115-
updateSharedEmails([next])
116-
setValue('')
117-
} catch (err) {
118-
console.error(err)
119-
} finally {
120-
// Reset to the original space if it was different
121-
if (currentSpace && currentSpace !== spaceDID) {
122-
await client.agent.setCurrentSpace(currentSpace)
123-
}
67+
const space = client.spaces().find(s => s.did() === spaceDID)
68+
if (!space) {
69+
throw new Error(`Could not find space to share`)
12470
}
71+
72+
const delegatedEmail = DIDMailTo.email(email)
73+
const delegation: Delegation<Capabilities> = await client.shareSpace(delegatedEmail, space.did())
74+
const next = { email: delegatedEmail, capabilities: delegation.capabilities.map(c => c.can) }
75+
updateSharedEmails([next])
76+
setValue('')
12577
}
12678

12779
async function makeDownloadLink(did: string): Promise<string> {

0 commit comments

Comments
 (0)