2021-08-03 12:31:26 +10:00
|
|
|
import './App.css';
|
2021-09-02 15:01:52 +10:00
|
|
|
import { Waku } from 'js-waku';
|
2021-08-03 12:31:26 +10:00
|
|
|
import * as React from 'react';
|
2021-08-04 12:36:17 +10:00
|
|
|
import protons from 'protons';
|
2021-08-03 12:31:26 +10:00
|
|
|
|
2021-08-04 12:36:17 +10:00
|
|
|
const ContentTopic = '/toy-chat/2/huilong/proto';
|
2021-08-03 12:31:26 +10:00
|
|
|
|
2021-08-04 12:36:17 +10:00
|
|
|
const proto = protons(`
|
|
|
|
|
message ChatMessage {
|
|
|
|
|
uint64 timestamp = 1;
|
|
|
|
|
string nick = 2;
|
|
|
|
|
bytes text = 3;
|
|
|
|
|
}
|
|
|
|
|
`);
|
2021-08-03 12:31:26 +10:00
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
|
const [waku, setWaku] = React.useState(undefined);
|
|
|
|
|
const [wakuStatus, setWakuStatus] = React.useState('None');
|
2021-08-04 12:36:17 +10:00
|
|
|
const [messages, setMessages] = React.useState([]);
|
2021-08-03 12:31:26 +10:00
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!!waku) return;
|
|
|
|
|
if (wakuStatus !== 'None') return;
|
|
|
|
|
|
|
|
|
|
setWakuStatus('Starting');
|
|
|
|
|
|
2021-08-10 11:32:14 +10:00
|
|
|
Waku.create({ bootstrap: true }).then((waku) => {
|
2021-08-03 12:31:26 +10:00
|
|
|
setWaku(waku);
|
|
|
|
|
setWakuStatus('Connecting');
|
|
|
|
|
});
|
|
|
|
|
}, [waku, wakuStatus]);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-08-04 12:36:17 +10:00
|
|
|
if (!waku) return;
|
2021-08-10 11:32:14 +10:00
|
|
|
if (wakuStatus !== 'Connected to Store') return;
|
2021-08-04 12:36:17 +10:00
|
|
|
|
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
|
waku.store
|
|
|
|
|
.queryHistory([ContentTopic])
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
// We may not be connected to a store node just yet
|
|
|
|
|
console.log('Failed to retrieve messages', e);
|
|
|
|
|
})
|
|
|
|
|
.then((retrievedMessages) => {
|
|
|
|
|
const messages = retrievedMessages.map(decodeMessage).filter(Boolean);
|
2021-08-03 12:31:26 +10:00
|
|
|
|
2021-08-04 12:36:17 +10:00
|
|
|
setMessages(messages);
|
|
|
|
|
});
|
|
|
|
|
}, 10000);
|
2021-08-03 12:31:26 +10:00
|
|
|
|
2021-08-04 12:36:17 +10:00
|
|
|
return () => clearInterval(interval);
|
2021-08-10 11:32:14 +10:00
|
|
|
}, [waku, wakuStatus]);
|
2021-08-04 15:05:19 +10:00
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!waku) return;
|
|
|
|
|
|
|
|
|
|
// We do not handle disconnection/re-connection in this example
|
2021-08-10 11:32:14 +10:00
|
|
|
if (wakuStatus === 'Connected to Store') return;
|
2021-08-04 15:05:19 +10:00
|
|
|
|
2021-09-02 15:01:52 +10:00
|
|
|
waku.waitForConnectedPeer().then(() => {
|
|
|
|
|
// We are now connected to a store node
|
|
|
|
|
setWakuStatus('Connected to Store');
|
|
|
|
|
});
|
2021-08-10 11:32:14 +10:00
|
|
|
}, [waku, wakuStatus]);
|
2021-08-03 12:31:26 +10:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="App">
|
|
|
|
|
<header className="App-header">
|
|
|
|
|
<h2>{wakuStatus}</h2>
|
2021-08-04 12:36:17 +10:00
|
|
|
<h3>Messages</h3>
|
2021-08-03 12:31:26 +10:00
|
|
|
<ul>
|
2021-08-04 12:36:17 +10:00
|
|
|
<Messages messages={messages} />
|
2021-08-03 12:31:26 +10:00
|
|
|
</ul>
|
|
|
|
|
</header>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|
|
|
|
|
|
2021-08-04 12:36:17 +10:00
|
|
|
function decodeMessage(wakuMessage) {
|
|
|
|
|
if (!wakuMessage.payload) return;
|
|
|
|
|
|
|
|
|
|
const { timestamp, nick, text } = proto.ChatMessage.decode(
|
|
|
|
|
wakuMessage.payload
|
|
|
|
|
);
|
|
|
|
|
|
2021-08-05 16:01:25 +10:00
|
|
|
if (!timestamp || !text || !nick) return;
|
2021-08-04 12:36:17 +10:00
|
|
|
|
|
|
|
|
const time = new Date();
|
|
|
|
|
time.setTime(timestamp);
|
|
|
|
|
|
|
|
|
|
const utf8Text = Buffer.from(text).toString('utf-8');
|
|
|
|
|
|
|
|
|
|
return { text: utf8Text, timestamp: time, nick };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Messages(props) {
|
|
|
|
|
return props.messages.map(({ text, timestamp, nick }) => {
|
|
|
|
|
return (
|
|
|
|
|
<li>
|
|
|
|
|
({formatDate(timestamp)}) {nick}: {text}
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-08-03 12:31:26 +10:00
|
|
|
|
2021-08-04 12:36:17 +10:00
|
|
|
function formatDate(timestamp) {
|
|
|
|
|
return timestamp.toLocaleString([], {
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
hour: 'numeric',
|
|
|
|
|
minute: '2-digit',
|
|
|
|
|
hour12: false,
|
|
|
|
|
});
|
2021-08-03 12:31:26 +10:00
|
|
|
}
|