This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathApp.js
87 lines (78 loc) · 2.43 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { useState, useEffect } from 'react';
import useIpfsFactory from './hooks/use-ipfs-factory.js'
import useIpfs from './hooks/use-ipfs.js'
import logo from './logo.svg';
import ipfsLogo from './ipfs-logo.svg'
import './App.css';
function App() {
const { ipfs, ipfsInitError } = useIpfsFactory({ commands: ['id'] })
const id = useIpfs(ipfs, 'id')
const [version, setVersion] = useState(null)
useEffect(() => {
if (!ipfs) return;
const getVersion = async () => {
const nodeId = await ipfs.version();
setVersion(nodeId);
}
getVersion();
}, [ipfs])
return (
<div className='sans-serif'>
<header className='flex items-center pa3 bg-navy bb bw3 b--aqua'>
<a href='https://ipfs.io' title='home'>
<img alt='IPFS logo' src={ipfsLogo} style={{ height: 50 }} className='v-top' />
</a>
<img src={logo} className="react-logo" alt="logo" style={{ height: 50 }} />
<h1 className='flex-auto ma0 tr f3 fw2 montserrat aqua'>IPFS React</h1>
</header>
<main>
{ipfsInitError && (
<div className='bg-red pa3 mw7 center mv3 white'>
Error: {ipfsInitError.message || ipfsInitError}
</div>
)}
{(id || version) &&
<section className='bg-snow mw7 center mt5'>
<h1 className='f3 fw4 ma0 pv3 aqua montserrat tc' data-test='title'>Connected to IPFS</h1>
<div className='pa4'>
{id && <IpfsId obj={id} keys={['id', 'agentVersion']}/>}
{version && <IpfsId obj={version} keys={['version']}/>}
</div>
</section>
}
</main>
<footer className="react-header">
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="react-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</footer>
</div>
);
}
const Title = ({ children }) => {
return (
<h2 className='f5 ma0 pb2 aqua fw4 montserrat'>{children}</h2>
)
}
const IpfsId = ({keys, obj}) => {
if (!obj || !keys || keys.length === 0) return null
return (
<>
{keys?.map((key) => (
<div className='mb4' key={key}>
<Title>{key}</Title>
<div className='bg-white pa2 br2 truncate monospace' data-test={key}>{obj[key]}</div>
</div>
))}
</>
)
}
export default App;