-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat: import a space into w3console #309
Changes from all commits
628d1eb
c578df2
a90f99e
c422389
56b1d70
7bc0838
0b2b11d
7bee838
f0a3b7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import md5 from 'blueimp-md5' | ||
|
||
export function DidIcon ({ did }: { did: string }): JSX.Element { | ||
const src = `https://www.gravatar.com/avatar/${md5(did)}?d=identicon` | ||
return <img title={did} src={src} className='w-10 hover:saturate-200 saturate-0 invert border-solid border-gray-500 border' /> | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,8 +2,11 @@ import { ChangeEvent, useState } from 'react' | |||||||||||
import { useKeyring } from '@w3ui/react-keyring' | ||||||||||||
import * as DID from '@ipld/dag-ucan/did' | ||||||||||||
import { CarWriter } from '@ipld/car/writer' | ||||||||||||
import { CarReader } from '@ipld/car/reader' | ||||||||||||
import { importDAG } from '@ucanto/core/delegation' | ||||||||||||
import type { PropsWithChildren } from 'react' | ||||||||||||
import type { Delegation } from '@ucanto/interface' | ||||||||||||
import type { Delegation, DIDKey } from '@ucanto/interface' | ||||||||||||
import { DidIcon } from './components/did-icon' | ||||||||||||
|
||||||||||||
function Header (props: PropsWithChildren): JSX.Element { | ||||||||||||
return <h3 className='font-semibold text-xs font-mono uppercase tracking-wider mb-4 text-gray-400'>{props.children}</h3> | ||||||||||||
|
@@ -27,10 +30,21 @@ export async function toCarBlob (delegation: Delegation): Promise<Blob> { | |||||||||||
return car | ||||||||||||
} | ||||||||||||
|
||||||||||||
export function SpaceShare (): JSX.Element { | ||||||||||||
const [, { createDelegation }] = useKeyring() | ||||||||||||
export async function toDelegation (car: Blob): Promise<Delegation> { | ||||||||||||
const blocks = [] | ||||||||||||
const bytes = new Uint8Array(await car.arrayBuffer()) | ||||||||||||
const reader = await CarReader.fromBytes(bytes) | ||||||||||||
for await (const block of reader.blocks()) { | ||||||||||||
blocks.push(block) | ||||||||||||
} | ||||||||||||
return importDAG(blocks) | ||||||||||||
} | ||||||||||||
|
||||||||||||
export function SpaceShare ({ viewSpace }: { viewSpace: (did: DIDKey) => void }): JSX.Element { | ||||||||||||
const [{ agent }, { createDelegation, addSpace }] = useKeyring() | ||||||||||||
const [value, setValue] = useState('') | ||||||||||||
const [downloadUrl, setDownloadUrl] = useState('') | ||||||||||||
const [proof, setProof] = useState<Delegation>() | ||||||||||||
|
||||||||||||
async function makeDownloadLink (input: string): Promise<void> { | ||||||||||||
let audience | ||||||||||||
|
@@ -68,6 +82,24 @@ export function SpaceShare (): JSX.Element { | |||||||||||
return `did-${method}-${id?.substring(0, 10)}.ucan` | ||||||||||||
} | ||||||||||||
|
||||||||||||
async function onImport (e: ChangeEvent<HTMLInputElement>): Promise<void> { | ||||||||||||
const input = e.target.files?.[0] | ||||||||||||
if (input === undefined) return | ||||||||||||
let delegation | ||||||||||||
try { | ||||||||||||
delegation = await toDelegation(input) | ||||||||||||
} catch (err) { | ||||||||||||
console.log(err) | ||||||||||||
return | ||||||||||||
} | ||||||||||||
try { | ||||||||||||
await addSpace(delegation) | ||||||||||||
setProof(delegation) | ||||||||||||
} catch (err) { | ||||||||||||
console.log(err) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return ( | ||||||||||||
<div className='pt-12'> | ||||||||||||
<div className=''> | ||||||||||||
|
@@ -80,9 +112,51 @@ export function SpaceShare (): JSX.Element { | |||||||||||
value={value} | ||||||||||||
onChange={onChange} | ||||||||||||
/> | ||||||||||||
<a className='w3ui-button text-center block w-52 opacity-30' style={{ opacity: downloadUrl !== '' ? '1' : '0.2' }} href={downloadUrl ?? ''} download={downloadName(downloadUrl !== '', value)}>Download UCAN</a> | ||||||||||||
<a className='w3ui-button text-center block w-52' style={{ opacity: downloadUrl !== '' ? '1' : '0.2' }} href={downloadUrl ?? ''} download={downloadName(downloadUrl !== '', value)}>Download UCAN</a> | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly, I'd move this
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had an issue with postcss not being able to detect dynamic classNames, which meant the class name wasn't included in the compiled css. |
||||||||||||
</form> | ||||||||||||
</div> | ||||||||||||
<div className='mt-16 py-16 border-t border-gray-700'> | ||||||||||||
<Header>Import a space</Header> | ||||||||||||
<p className='mb-2'>Copy and paste your DID to your friend</p> | ||||||||||||
<div className='bg-opacity-10 bg-white font-mono text-sm py-2 px-3 rounded break-words max-w-4xl'> | ||||||||||||
{agent?.did()} | ||||||||||||
</div> | ||||||||||||
<div className='mt-8'> | ||||||||||||
<label className='w3ui-button text-center block w-52'> | ||||||||||||
Import UCAN | ||||||||||||
<input type='file' accept='.ucan,.car,application/vnd.ipfs.car' className='hidden' onChange={(e: ChangeEvent<HTMLInputElement>) => { void onImport(e) }} /> | ||||||||||||
</label> | ||||||||||||
</div> | ||||||||||||
{proof !== undefined && ( | ||||||||||||
<div className='mt-4 pt-4'> | ||||||||||||
<Header>Added</Header> | ||||||||||||
<div className='max-w-3xl border border-gray-700 shadow-xl'> | ||||||||||||
{proof.capabilities.map((cap, i) => ( | ||||||||||||
<figure className='p-4 flex flex-row items-start gap-2' key={i}> | ||||||||||||
<DidIcon did={cap.with} /> | ||||||||||||
<figcaption className='grow'> | ||||||||||||
<a href='#' onClick={() => viewSpace(cap.with)} className='block'> | ||||||||||||
<span className='block text-xl font-semibold leading-5 mb-1'> | ||||||||||||
{proof.facts.at(i)?.space.name ?? 'Untitled Space'} | ||||||||||||
</span> | ||||||||||||
<span className='block font-mono text-xs text-gray-500 truncate'>{cap.with}</span> | ||||||||||||
</a> | ||||||||||||
</figcaption> | ||||||||||||
<div> | ||||||||||||
<a | ||||||||||||
href='#' | ||||||||||||
className='font-sm font-semibold align-[-8px]' | ||||||||||||
onClick={() => viewSpace(cap.with)} | ||||||||||||
> | ||||||||||||
View | ||||||||||||
</a> | ||||||||||||
</div> | ||||||||||||
</figure> | ||||||||||||
))} | ||||||||||||
</div> | ||||||||||||
</div> | ||||||||||||
)} | ||||||||||||
</div> | ||||||||||||
</div> | ||||||||||||
) | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there anything we should do here to ensure this is actually a delegation or is that handled in the library?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, the code should catch where the blob cannot be decoded as a CAR and let the user know that the file isn't a CAR (or corrupted).
and it should catch an error from importDAG if the CAR doesn't contain blocks that can be parsed as a ucan delegation.
This feature in general is very much a proof of concept, so i'd prefer to land it as is, and then prioritise adding the good feedback if we decide it's a thing we want to keep. It'll be much less relevant once we can fetch users other spaces for them from accounts, and share spaces by email address.