ipfs/src/components/Add.tsx

37 lines
910 B
TypeScript
Raw Normal View History

2019-10-16 17:02:04 +02:00
import React, { useState } from 'react'
import { saveToIpfs } from '../ipfs'
2019-10-17 00:13:54 +02:00
import { ipfsGateway } from '../../site.config'
2019-10-16 17:02:04 +02:00
import Dropzone from './Dropzone'
2019-10-17 00:13:54 +02:00
import styles from './Add.module.css'
2019-10-16 17:02:04 +02:00
import Spinner from './Spinner'
export default function Add() {
2019-10-17 00:13:54 +02:00
const [fileHash, setFileHash] = useState()
2019-10-16 17:02:04 +02:00
const [loading, setLoading] = useState(false)
2019-10-17 00:13:54 +02:00
const handleCaptureFile = async (files: File[]) => {
2019-10-16 17:02:04 +02:00
setLoading(true)
const cid = await saveToIpfs(files)
setFileHash(cid)
setLoading(false)
}
return (
2019-10-16 22:10:03 +02:00
<div className={styles.add}>
2019-10-16 17:02:04 +02:00
{loading ? (
<Spinner />
) : fileHash ? (
2019-10-16 17:26:46 +02:00
<a
target="_blank"
rel="noopener noreferrer"
href={`${ipfsGateway}/ipfs/${fileHash}`}
>
ipfs://{fileHash}
</a>
2019-10-16 17:02:04 +02:00
) : (
<Dropzone multiple={false} handleOnDrop={handleCaptureFile} />
)}
</div>
)
}