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

[NEW][β] IPFS snapshot component #151

Merged
merged 8 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# misc
.DS_Store
*.pem
package-lock.json

# debug
npm-debug.log*
Expand Down
120 changes: 120 additions & 0 deletions app/components/marketplace/ipfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { Button, Card, Spinner, Stack } from "react-bootstrap";
import * as IPFS from "ipfs-core";
import { useRef, useState } from "react";

const IpfsAdder = ({ showText }) => {
const [fileUrl, updateFileUrl] = useState(``);
const [cid, setCID] = useState("");
const [adding, setAdding] = useState(false);

const hiddenInput = useRef(null);

const getIPFS = async (e) => {
try {
setAdding(true);
const file = e.target.files[0];

const ipfs = await IPFS.create({ repo: "ok" + Math.random() });

console.log("ipfs", ipfs);
const { cid } = await ipfs.add(file);
const url = `https://ipfs.io/ipfs/${cid.toString()}`;
updateFileUrl(url);

setCID(cid.toString());
setAdding(false);
} catch (e) {
setAdding(false);
console.error("An error occurred while uploading media", e);
}
};

const handleInputClick = (event) => {
hiddenInput.current.click();
};

return (
<div className="mx-auto">
<Stack direction="vertical" gap={2}>
<Stack direction="horizontal" gap={2}>
<Button disabled={adding} onClick={handleInputClick}>
{adding ? (
<Spinner
as="span"
animation="border"
size="sm"
role="status"
aria-hidden="true"
/>
) : (
showText
)}
</Button>
<input
type="file"
style={{ display: "none" }}
ref={hiddenInput}
accept="image/*"
capture="camera"
onChange={getIPFS}
/>
{cid && <Copy cid={cid} />}
</Stack>

{fileUrl && <PreviewImage srcUrl={fileUrl} />}
</Stack>
</div>
);
};

const Copy = ({ cid }) => {
const [isCopied, setIsCopied] = useState(false);

async function copyTextToClipboard(text) {
if ("clipboard" in navigator) {
return await navigator.clipboard.writeText(text);
} else {
return document.execCommand("copy", true, text);
}
}

const handleCopyClick = () => {
copyTextToClipboard(cid)
.then(() => {
setIsCopied(true);
setTimeout(() => {
setIsCopied(false);
}, 1500);
})
.catch((err) => {
console.log(err);
});
};

return (
<Stack gap={2} direction="horizontal">
<Button disabled variant="outline-dark">
{cid.substr(0, 5)}...{cid.substr(cid.length - 5, cid.length)}
</Button>
<Button variant="outline-dark" onClick={handleCopyClick}>
{isCopied ? "Copied" : "Copy"}
</Button>
</Stack>
);
};

const PreviewImage = ({ srcUrl }) => {
return (
<Card style={{ width: "18rem" }}>
<Card.Img variant="top" src={srcUrl} />
<Card.Body>
<Card.Title>Image Preview</Card.Title>
<Button href={srcUrl} target="_blank" variant="primary">
Visit on IPFS
</Button>
</Card.Body>
</Card>
);
};

export default IpfsAdder;
Loading