Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: using the new client method for space sharing #175

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 10 additions & 58 deletions src/share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeEvent, useEffect, useState } from 'react'
import { SpaceDID, useW3 } from '@w3ui/react'
import { extract } from '@ucanto/core/delegation'
import type { PropsWithChildren } from 'react'
import type { Delegation } from '@ucanto/interface'
import type { Capabilities, Delegation } from '@ucanto/interface'
import { SpacePreview } from './components/SpaceCreator'
import { H2, H3 } from '@/components/Text'
import CopyButton from './components/CopyButton'
Expand Down Expand Up @@ -64,64 +64,16 @@ export function ShareSpace({ spaceDID }: { spaceDID: SpaceDID }): JSX.Element {
throw new Error(`Client not found`)
}

const currentSpace = client.agent.currentSpace()
try {
const space = client.spaces().find(s => s.did() === spaceDID)
if (!space) {
throw new Error(`Could not find space to share`)
}

const delegatedEmail = DIDMailTo.email(email)

// FIXME (fforbeck): enable shareSpace function call after @w3ui/react lib is updated to v2.4.0 and the issue with blobs are solved
// const delegation = await client.shareSpace(delegatedEmail, space.did())

// Make sure the agent is using the shared space before delegating
await client.agent.setCurrentSpace(spaceDID)

// Delegate capabilities to the delegate account to access the **current space**
const delegation = await client.createDelegation(
{
did: () => DIDMailTo.fromEmail(delegatedEmail),
},
[
'space/*',
'store/*',
'upload/*',
'access/*',
'usage/*',
// @ts-expect-error (FIXME: https://github.com/storacha/w3up/issues/1554)
'filecoin/*',
], {
expiration: Infinity
}
)

const sharingResult = await client.capability.access.delegate({
space: spaceDID,
delegations: [delegation],
})

if (sharingResult.error) {
throw new Error(
`failed to share space with ${delegatedEmail}: ${sharingResult.error.message}`,
{
cause: sharingResult.error,
}
)
}

const next = { email: delegatedEmail, capabilities: delegation.capabilities.map(c => c.can) }
updateSharedEmails([next])
setValue('')
} catch (err) {
console.error(err)
} finally {
// Reset to the original space if it was different
if (currentSpace && currentSpace !== spaceDID) {
await client.agent.setCurrentSpace(currentSpace)
}
const space = client.spaces().find(s => s.did() === spaceDID)
if (!space) {
throw new Error(`Could not find space to share`)
}

const delegatedEmail = DIDMailTo.email(email)
const delegation: Delegation<Capabilities> = await client.shareSpace(delegatedEmail, space.did())
const next = { email: delegatedEmail, capabilities: delegation.capabilities.map(c => c.can) }
updateSharedEmails([next])
setValue('')
}

async function makeDownloadLink(did: string): Promise<string> {
Expand Down
Loading