-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also get the build working so I can create an example that uses these
- Loading branch information
Showing
10 changed files
with
322 additions
and
941 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useContext } from 'react' | ||
import { CARMetadata } from '@w3ui/uploader-core' | ||
import { Link, Version } from 'multiformats' | ||
|
||
import Uploader, { UploaderContext } from './Uploader'; | ||
|
||
export const Uploading = ({ file, storedDAGShards }: { file?: File, storedDAGShards?: CARMetadata[] }) => ( | ||
<div className="uploading"> | ||
<h1 className="title">Uploading DAG for {file?.name}</h1> | ||
{storedDAGShards?.map(({ cid, size }) => ( | ||
<p className="shard" key={cid.toString()}> | ||
{cid.toString()} ({size} bytes) | ||
</p> | ||
))} | ||
</div> | ||
) | ||
|
||
export const Errored = ({ error }: { error: any }) => ( | ||
<div className="error"> | ||
<h1 className="message">⚠️ Error: failed to upload file: {error.message}</h1> | ||
<p className="details">Check the browser console for details.</p> | ||
</div> | ||
) | ||
|
||
export const Done = ({ file, dataCid, storedDAGShards }: {file?: File, dataCid?: Link<unknown, number, number, Version>, storedDAGShards?: CARMetadata[]}) => ( | ||
<div className="done"> | ||
<h1 className="title">Done!</h1> | ||
<p className="cid">{dataCid?.toString()}</p> | ||
<p className="view"><a href={`https://w3s.link/ipfs/${dataCid}`}>View {file?.name} on IPFS Gateway.</a></p> | ||
<h5 className="chunks">Shards ({storedDAGShards?.length}):</h5> | ||
{storedDAGShards?.map(({ cid, size }) => ( | ||
<p className="shard" key={cid.toString()}> | ||
{cid.toString()} ({size} bytes) | ||
</p> | ||
))} | ||
</div> | ||
) | ||
|
||
|
||
const SimpleUploader = () => { | ||
const { status, file, error, dataCid, storedDAGShards } = useContext(UploaderContext) | ||
return ( | ||
<Uploader> | ||
{(status === 'uploading') ? ( | ||
<Uploading file={file} storedDAGShards={storedDAGShards} /> | ||
) : ( | ||
(status === 'done') ? ( | ||
error ? <Errored error={error} /> : <Done file={file} dataCid={dataCid} storedDAGShards={storedDAGShards} /> | ||
) : ( | ||
<Uploader.Form className=""> | ||
<div className="field"> | ||
<label htmlFor='w3ui-uploader-file'>File:</label> | ||
<Uploader.Input id="w3ui-uploader-file" /> | ||
</div> | ||
<button type='submit'>Upload</button> | ||
</Uploader.Form> | ||
) | ||
)} | ||
</Uploader> | ||
) | ||
} | ||
|
||
export default SimpleUploader |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useContext, createContext, useState } from 'react' | ||
import { useUploader, CARMetadata } from '@w3ui/react-uploader' | ||
import { Link, Version } from 'multiformats' | ||
|
||
export type UploaderContextValue = { | ||
storedDAGShards?: CARMetadata[], | ||
file?: File, | ||
setFile: React.Dispatch<React.SetStateAction<File | undefined>>, | ||
dataCid?: Link<unknown, number, number, Version>, | ||
status?: string, | ||
error?: any, | ||
handleUploadSubmit?: (e: Event) => void | ||
|
||
} | ||
|
||
export const UploaderContext = createContext<UploaderContextValue>({ | ||
setFile: () => {} | ||
}) | ||
|
||
export type UploaderProps = { | ||
children: React.ReactNode, | ||
} | ||
|
||
export const Uploader = ({ | ||
children, | ||
}: UploaderProps) => { | ||
const [{ storedDAGShards }, uploader] = useUploader() | ||
const [file, setFile] = useState<File>() | ||
const [dataCid, setDataCid] = useState<Link<unknown, number, number, Version>>() | ||
const [status, setStatus] = useState('') | ||
const [error, setError] = useState() | ||
|
||
const handleUploadSubmit = async (e: Event) => { | ||
e.preventDefault() | ||
if (file) { | ||
try { | ||
setStatus('uploading') | ||
const cid = await uploader.uploadFile(file) | ||
setDataCid(cid) | ||
} catch (err: any) { | ||
console.error(err) | ||
setError(err) | ||
} finally { | ||
setStatus('done') | ||
} | ||
} | ||
} | ||
return ( | ||
<UploaderContext.Provider value={{ storedDAGShards, file, setFile, dataCid, status, error, handleUploadSubmit }}> | ||
{children} | ||
</UploaderContext.Provider> | ||
) | ||
} | ||
|
||
Uploader.Input = (props: any) => { | ||
const { setFile } = useContext(UploaderContext) | ||
return ( | ||
<input type='file' onChange={e => setFile(e?.target?.files?.[0])} {...props} /> | ||
) | ||
} | ||
|
||
Uploader.Form = ({ children, ...props }: {children: React.ReactNode} & any) => { | ||
const { handleUploadSubmit } = useContext(UploaderContext) | ||
return ( | ||
<form onSubmit={handleUploadSubmit} {...props}> | ||
{children} | ||
</form> | ||
) | ||
} | ||
|
||
export default Uploader |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Uploader' | ||
export * from './SimpleUploader' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters