Skip to content

Commit bde9a5a

Browse files
author
Alan Shaw
committed
feat: loading spinner
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 2c85e1e commit bde9a5a

File tree

8 files changed

+90
-68
lines changed

8 files changed

+90
-68
lines changed

package-lock.json

Lines changed: 32 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dependencies": {
77
"cytoscape": "^3.6.0",
88
"cytoscape-dagre": "^2.2.2",
9-
"ipfs": "^0.36.0",
9+
"ipfs": "github:ipfs/js-ipfs#fix/file-support",
1010
"ipfs-css": "^0.12.0",
1111
"ipld-dag-pb": "^0.17.0",
1212
"parcel": "^1.12.3",

src/App.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-env browser */
2-
import React, { useState, useEffect } from 'react'
2+
import React, { useState, useEffect, Fragment } from 'react'
33
import Header from './Header'
44
import Controls from './Controls'
55
import Dag from './Dag'
6-
import { Buffer } from 'ipfs'
76
import { ipfsAdd } from './lib/ipfs'
87
import DropTarget from './DropTarget'
98
import NodeInfo from './NodeInfo'
9+
import Spinner from './Spinner'
1010

1111
export default function App () {
1212
const [files, setFiles] = useState([])
@@ -17,19 +17,21 @@ export default function App () {
1717
const [layerRepeat, setLayerRepeat] = useState(4)
1818
const [rootCid, setRootCid] = useState(null)
1919
const [focusedNode, setFocusedNode] = useState(null)
20+
const [loading, setLoading] = useState(false)
2021

2122
useEffect(() => {
2223
if (!files.length) return
23-
ipfsAdd({ files, chunker, rawLeaves, strategy, maxChildren, layerRepeat })
24-
.then(setRootCid)
24+
const addFiles = async () => {
25+
setRootCid(null)
26+
setLoading(true)
27+
const cid = await ipfsAdd({ files, chunker, rawLeaves, strategy, maxChildren, layerRepeat })
28+
setRootCid(cid)
29+
}
30+
addFiles()
2531
}, [files, chunker, rawLeaves, strategy, maxChildren, layerRepeat])
2632

2733
const onFileChange = file => {
28-
const fileReader = new FileReader()
29-
fileReader.onload = e => {
30-
setFiles(files.concat({ path: file.name, content: Buffer.from(e.target.result) }))
31-
}
32-
fileReader.readAsArrayBuffer(file)
34+
setFiles(files.concat({ path: file.name, content: file }))
3335
}
3436

3537
const onReset = () => {
@@ -65,8 +67,12 @@ export default function App () {
6567
<DropTarget onFileDrop={onFileChange} className='h-100'>
6668
{files.length ? (
6769
<div className='flex flex-column h-100'>
68-
<div className='flex-auto'>
69-
<Dag rootCid={rootCid} onNodeFocus={setFocusedNode} />
70+
<div className='flex-auto relative'>
71+
<Spinner show={loading} />
72+
<Dag
73+
rootCid={rootCid}
74+
onNodeFocus={setFocusedNode}
75+
onGraphRender={() => setLoading(false)} />
7076
</div>
7177
<div className='flex-none'>
7278
<NodeInfo info={focusedNode} />

src/Dag.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class Dag extends Component {
2727

2828
async _updateGraph () {
2929
if (this._graph) this._graph.destroy()
30+
if (this.props.onNodeFocus) this.props.onNodeFocus(null)
3031

3132
const { rootCid } = this.props
3233
if (!rootCid) return
@@ -54,9 +55,8 @@ export default class Dag extends Component {
5455

5556
cy.layout(DagGraphOptions.layout).run()
5657

57-
if (this.props.onNodeFocus) {
58-
focusElement(cy.getElementById(rootCid))
59-
}
58+
if (this.props.onNodeFocus) focusElement(cy.getElementById(rootCid))
59+
if (this.props.onGraphRender) this.props.onGraphRender()
6060
}
6161

6262
async _getGraphNodes (cid, nodeMap = new Map()) {

src/NodeInfo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Fragment } from 'react'
22

33
const NodeContainer = ({ children }) => (
4-
<div className='pa3' style={{ minHeight: 60 }}>{children}</div>
4+
<div className='pa3' style={{ minHeight: 90 }}>{children}</div>
55
)
66

77
const CidColumn = ({ cid }) => (

src/Spinner.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
3+
export default function Spinner ({ show }) {
4+
return (
5+
<div className='w-100 h-100 absolute' style={{ display: show ? 'block' : 'none', zIndex: 1 }}>
6+
<div className='h-100 flex items-center justify-center'>
7+
<div className='lolo' />
8+
</div>
9+
</div>
10+
)
11+
}

src/index.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
@import tachyons;
22
@import ipfs-css;
3+
4+
@keyframes lolo {
5+
0%, 100% {
6+
transform: translateX(30px) scaleX(0.14);
7+
}
8+
25%, 75% {
9+
transform: scaleX(1);
10+
}
11+
50% {
12+
transform: translateX(-30px) scaleX(0.14);
13+
}
14+
}
15+
.lolo {
16+
display: inline-block;
17+
width: 70px;
18+
height: 6px;
19+
}
20+
.lolo::after {
21+
content:'';
22+
display:block;
23+
background: #0b3a53;
24+
width: 70px;
25+
height: 100%;
26+
animation: lolo 1.5s cubic-bezier(0.25,0.1,0.25,1) infinite;
27+
}

src/lib/ipfs.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ export async function ipfsAdd ({ files, chunker, rawLeaves, strategy, maxChildre
1818

1919
console.log('adding', { files, chunker, strategy, maxChildren, layerRepeat })
2020

21-
// ...because IPFS mutates the array and it's contents
22-
files = files.map(({ path, content }) => ({ path, content }))
23-
2421
const res = await ipfs.add(files, {
2522
chunker,
2623
rawLeaves,

0 commit comments

Comments
 (0)