2021-07-20 14:00:26 +03:00
|
|
|
|
import React, { useState } from "react";
|
|
|
|
|
import logo from "./logo.svg";
|
|
|
|
|
import "./App.scss";
|
|
|
|
|
|
|
|
|
|
import { createClient, FluenceClient } from "@fluencelabs/fluence";
|
|
|
|
|
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
|
|
|
|
import { sayHello } from "./_aqua/getting-started";
|
|
|
|
|
|
|
|
|
|
const relayNodes = [krasnodar[0], krasnodar[1], krasnodar[2]];
|
|
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
|
const [client, setClient] = useState<FluenceClient | null>(null);
|
2021-07-20 17:49:58 +03:00
|
|
|
|
const [helloMessage, setHelloMessage] = useState<string | null>(null);
|
2021-07-20 14:00:26 +03:00
|
|
|
|
|
|
|
|
|
const [peerIdInput, setPeerIdInput] = useState<string>("");
|
|
|
|
|
const [relayPeerIdInput, setRelayPeerIdInput] = useState<string>("");
|
|
|
|
|
|
|
|
|
|
const connect = (relayPeerId: string) => {
|
|
|
|
|
createClient(relayPeerId)
|
|
|
|
|
.then((client) => {
|
|
|
|
|
// Register handler for this call in aqua:
|
|
|
|
|
// HelloWorld.recieveHello(%init_peer_id%)
|
|
|
|
|
client.callServiceHandler.onEvent(
|
|
|
|
|
"HelloWorld",
|
|
|
|
|
"recieveHello",
|
|
|
|
|
(args) => {
|
2021-07-20 17:49:58 +03:00
|
|
|
|
// no computation is done inside the browser
|
2021-07-20 14:00:26 +03:00
|
|
|
|
const [msg] = args;
|
2021-07-20 17:49:58 +03:00
|
|
|
|
setHelloMessage(msg);
|
2021-07-20 14:00:26 +03:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
setClient(client);
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => console.log("Client initialization failed", err));
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-20 17:49:58 +03:00
|
|
|
|
const helloBtnOnClick = async () => {
|
2021-07-20 14:00:26 +03:00
|
|
|
|
if (client === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-20 17:49:58 +03:00
|
|
|
|
// Using aqua is as easy as calling a javascript funсtion
|
2021-07-20 14:00:26 +03:00
|
|
|
|
const res = await sayHello(client!, peerIdInput, relayPeerIdInput);
|
2021-07-20 17:49:58 +03:00
|
|
|
|
setHelloMessage(res);
|
2021-07-20 14:00:26 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-07-20 17:49:58 +03:00
|
|
|
|
const isConnected = client !== null;
|
|
|
|
|
|
2021-07-20 14:00:26 +03:00
|
|
|
|
return (
|
|
|
|
|
<div className="App">
|
|
|
|
|
<header>
|
|
|
|
|
<img src={logo} className="logo" alt="logo" />
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<div className="content">
|
|
|
|
|
{isConnected ? (
|
|
|
|
|
<>
|
|
|
|
|
<h1>Connected</h1>
|
|
|
|
|
<table>
|
2021-07-20 17:49:58 +03:00
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<td className="bold">Peer id:</td>
|
|
|
|
|
<td className="mono">{client!.selfPeerId}</td>
|
|
|
|
|
<td>
|
|
|
|
|
<button
|
|
|
|
|
className="btn-clipboard"
|
|
|
|
|
onClick={() => copyToClipboard(client!.selfPeerId)}
|
|
|
|
|
>
|
|
|
|
|
<i className="gg-clipboard"></i>
|
|
|
|
|
</button>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td className="bold">Relay peer id:</td>
|
|
|
|
|
<td className="mono">{client!.relayPeerId}</td>
|
|
|
|
|
<td>
|
|
|
|
|
<button
|
|
|
|
|
className="btn-clipboard"
|
|
|
|
|
onClick={() => copyToClipboard(client!.relayPeerId!)}
|
|
|
|
|
>
|
|
|
|
|
<i className="gg-clipboard"></i>
|
|
|
|
|
</button>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
2021-07-20 14:00:26 +03:00
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<h2>Say hello!</h2>
|
|
|
|
|
<p className="p">
|
|
|
|
|
Now try opening a new tab with the same application. Copy paste
|
|
|
|
|
the peer id and relay from the second tab and say hello!
|
|
|
|
|
</p>
|
|
|
|
|
<div className="row">
|
2021-07-20 17:49:58 +03:00
|
|
|
|
<label className="label bold">Target peer id</label>
|
2021-07-20 14:00:26 +03:00
|
|
|
|
<input
|
|
|
|
|
className="input"
|
|
|
|
|
type="text"
|
|
|
|
|
onChange={(e) => setPeerIdInput(e.target.value)}
|
|
|
|
|
value={peerIdInput}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="row">
|
2021-07-20 17:49:58 +03:00
|
|
|
|
<label className="label bold">Target relay</label>
|
2021-07-20 14:00:26 +03:00
|
|
|
|
<input
|
|
|
|
|
className="input"
|
|
|
|
|
type="text"
|
|
|
|
|
onChange={(e) => setRelayPeerIdInput(e.target.value)}
|
|
|
|
|
value={relayPeerIdInput}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="row">
|
2021-07-20 17:49:58 +03:00
|
|
|
|
<button className="btn btn-hello" onClick={helloBtnOnClick}>
|
2021-07-20 14:00:26 +03:00
|
|
|
|
say hello
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<h1>Pick a relay</h1>
|
|
|
|
|
<ul>
|
|
|
|
|
{relayNodes.map((x) => (
|
|
|
|
|
<li key={x.peerId}>
|
|
|
|
|
<span className="mono">{x.peerId}</span>
|
|
|
|
|
<button className="btn" onClick={() => connect(x.multiaddr)}>
|
|
|
|
|
Connect
|
|
|
|
|
</button>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
2021-07-20 17:49:58 +03:00
|
|
|
|
{helloMessage && (
|
2021-07-20 14:00:26 +03:00
|
|
|
|
<>
|
2021-07-20 17:49:58 +03:00
|
|
|
|
<h2>Message</h2>
|
|
|
|
|
<div> {helloMessage} </div>
|
2021-07-20 14:00:26 +03:00
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 17:49:58 +03:00
|
|
|
|
const copyToClipboard = (text: string) => {
|
|
|
|
|
navigator.clipboard.writeText(text);
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-20 14:00:26 +03:00
|
|
|
|
export default App;
|