|
2 | 2 | import React, { useState, useEffect } from 'react' |
3 | 3 | import Header from './Header' |
4 | 4 | import Controls from './Controls' |
5 | | -import Dag from './Dag' |
6 | | -import { ipfsAdd } from './lib/ipfs' |
| 5 | +import Blocks from './Blocks' |
| 6 | +import { getIpfs, ipfsAdd, ipfsGet, resetRepo, removeUnpinned, unpinAll, getBlockInfo } from './lib/ipfs' |
7 | 7 | import DropTarget from './DropTarget' |
8 | 8 | import NodeInfo from './NodeInfo' |
9 | | -import Spinner from './Spinner' |
10 | 9 |
|
11 | 10 | export default function App () { |
12 | 11 | const [files, setFiles] = useState([]) |
13 | | - const [chunker, setChunker] = useState('size-512') |
14 | | - const [rawLeaves, setRawLeaves] = useState(false) |
15 | | - const [strategy, setStrategy] = useState('balanced') |
16 | | - const [maxChildren, setMaxChildren] = useState(11) |
17 | | - const [layerRepeat, setLayerRepeat] = useState(4) |
| 12 | + const [chunker, setChunker] = useState('size-1024') |
18 | 13 | const [rootCid, setRootCid] = useState(null) |
19 | 14 | const [focusedNode, setFocusedNode] = useState(null) |
| 15 | + const [blockInfo, setBlockInfo] = useState(null) |
20 | 16 | const [loading, setLoading] = useState(false) |
| 17 | + const [cidToGet, setCidToGet] = useState('') |
21 | 18 |
|
22 | 19 | useEffect(() => { |
23 | 20 | if (!files.length) return |
24 | 21 | const addFiles = async () => { |
25 | 22 | setRootCid(null) |
26 | 23 | setLoading(true) |
27 | | - const cid = await ipfsAdd({ files, chunker, rawLeaves, strategy, maxChildren, layerRepeat }) |
| 24 | + const cid = await ipfsAdd({ files, chunker }) |
| 25 | + setLoading(false) |
28 | 26 | setRootCid(cid) |
29 | 27 | } |
30 | 28 | addFiles() |
31 | | - }, [files, chunker, rawLeaves, strategy, maxChildren, layerRepeat]) |
| 29 | + }, [files, chunker]) |
32 | 30 |
|
33 | | - const onFileChange = file => { |
34 | | - setFiles(files.concat({ path: file.name, content: file })) |
| 31 | + // update block info when focusedNode changes |
| 32 | + useEffect(() => { |
| 33 | + console.log('update block info effect') |
| 34 | + if (!focusedNode) return |
| 35 | + const fn = async () => { |
| 36 | + const res = await getBlockInfo(focusedNode) |
| 37 | + console.log('updating info for focusedNode', focusedNode, res.id) |
| 38 | + if (focusedNode === res.id) { |
| 39 | + setBlockInfo(res) |
| 40 | + } |
| 41 | + } |
| 42 | + fn() |
| 43 | + }, [focusedNode]) |
| 44 | + |
| 45 | + const onFilesChange = files => { |
| 46 | + setFiles(files.map(file => ({ path: file.name, content: file }))) |
| 47 | + } |
| 48 | + |
| 49 | + const onCidSubmit = async e => { |
| 50 | + setRootCid(null) |
| 51 | + setLoading(true) |
| 52 | + await ipfsGet(cidToGet) |
| 53 | + setRootCid(cidToGet) |
35 | 54 | } |
36 | 55 |
|
37 | | - const onReset = () => { |
| 56 | + const onReset = async () => { |
| 57 | + setLoading(true) |
38 | 58 | setFiles([]) |
39 | | - setChunker('size-512') |
40 | | - setStrategy('balanced') |
41 | | - setMaxChildren(11) |
42 | | - setLayerRepeat(4) |
| 59 | + setChunker('size-1024') |
43 | 60 | setRootCid(null) |
44 | 61 | setFocusedNode(null) |
| 62 | + await resetRepo() |
| 63 | + await getIpfs() |
| 64 | + setLoading(false) |
| 65 | + console.log('reset complete') |
| 66 | + } |
| 67 | + |
| 68 | + const onUnpinAll = async () => { |
| 69 | + setLoading(true) |
| 70 | + await unpinAll() |
| 71 | + setLoading(false) |
| 72 | + } |
| 73 | + |
| 74 | + const onRemoveUnpinned = async () => { |
| 75 | + console.log('remove unpinned') |
| 76 | + setLoading(true) |
| 77 | + await removeUnpinned() |
| 78 | + setLoading(false) |
| 79 | + console.log('remove unpinned done') |
45 | 80 | } |
46 | 81 |
|
47 | 82 | return ( |
48 | 83 | <div className='avenir flex flex-column h-100'> |
49 | 84 | <div className='flex-none'> |
50 | | - <Header /> |
| 85 | + <Header> |
| 86 | + <div className='dn'> |
| 87 | + <form onSubmit={onCidSubmit} className='flex items-center'> |
| 88 | + <input type='text' value={cidToGet} className='mh3 br2 ba b--silver pa2 f5 dib w5' placeholder='QmHash' /> |
| 89 | + <button |
| 90 | + type='submit' |
| 91 | + className='mr2 transition-all sans-serif dib v-mid fw5 nowrap lh-copy bn br1 ph4 pv1 pointer focus-outline bg-green-muted hover-bg-green white' |
| 92 | + title='ipfs.get a CID'> |
| 93 | + Get |
| 94 | + </button> |
| 95 | + </form> |
| 96 | + </div> |
| 97 | + </Header> |
51 | 98 | </div> |
52 | 99 | <div className='flex-none'> |
53 | 100 | <Controls |
54 | 101 | chunker={chunker} |
55 | 102 | onChunkerChange={setChunker} |
56 | | - rawLeaves={rawLeaves} |
57 | | - onRawLeavesChange={setRawLeaves} |
58 | | - strategy={strategy} |
59 | | - onStrategyChange={setStrategy} |
60 | | - maxChildren={maxChildren} |
61 | | - onMaxChildrenChange={setMaxChildren} |
62 | | - layerRepeat={layerRepeat} |
63 | | - onLayerRepeatChange={setLayerRepeat} |
64 | | - onReset={onReset} /> |
| 103 | + onReset={onReset} |
| 104 | + onGc={onRemoveUnpinned} |
| 105 | + onUnpinAll={onUnpinAll} |
| 106 | + loading={loading} /> |
65 | 107 | </div> |
66 | 108 | <div className='flex-auto'> |
67 | | - <DropTarget onFileDrop={onFileChange} className='h-100'> |
68 | | - {files.length ? ( |
69 | | - <div className='flex flex-column h-100'> |
70 | | - <div className='flex-auto relative'> |
71 | | - <Spinner show={loading} /> |
72 | | - <Dag |
73 | | - rootCid={rootCid} |
74 | | - onNodeFocus={setFocusedNode} |
75 | | - onGraphRender={() => setLoading(false)} /> |
76 | | - </div> |
77 | | - <div className='flex-none'> |
78 | | - <NodeInfo info={focusedNode} /> |
79 | | - </div> |
| 109 | + <DropTarget onFileDrop={onFilesChange} className='h-100'> |
| 110 | + <div className='flex flex-column h-100'> |
| 111 | + <div className='flex-auto relative'> |
| 112 | + <Blocks onNodeFocus={setFocusedNode} /> |
| 113 | + </div> |
| 114 | + <div className='flex-none'> |
| 115 | + <NodeInfo info={blockInfo} /> |
80 | 116 | </div> |
81 | | - ) : null} |
| 117 | + </div> |
82 | 118 | </DropTarget> |
83 | 119 | </div> |
84 | 120 | </div> |
|
0 commit comments