Merge pull request #84 from waku-org/store-reactjs-chat
This commit is contained in:
commit
f230f1aa35
|
@ -1,60 +0,0 @@
|
||||||
const webpack = require('webpack');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
dev: (config) => {
|
|
||||||
// Override webpack 5 config from react-scripts to load polyfills
|
|
||||||
if (!config.resolve) config.resolve = {};
|
|
||||||
if (!config.resolve.fallback) config.resolve.fallback = {};
|
|
||||||
Object.assign(config.resolve.fallback, {
|
|
||||||
buffer: require.resolve('buffer'),
|
|
||||||
crypto: false,
|
|
||||||
stream: require.resolve('stream-browserify'),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!config.plugins) config.plugins = [];
|
|
||||||
config.plugins.push(
|
|
||||||
new webpack.DefinePlugin({
|
|
||||||
'process.env.ENV': JSON.stringify('dev'),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
config.plugins.push(
|
|
||||||
new webpack.ProvidePlugin({
|
|
||||||
process: 'process/browser.js',
|
|
||||||
Buffer: ['buffer', 'Buffer'],
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!config.ignoreWarnings) config.ignoreWarnings = [];
|
|
||||||
config.ignoreWarnings.push(/Failed to parse source map/);
|
|
||||||
|
|
||||||
return config;
|
|
||||||
},
|
|
||||||
prod: (config) => {
|
|
||||||
// Override webpack 5 config from react-scripts to load polyfills
|
|
||||||
if (!config.resolve) config.resolve = {};
|
|
||||||
if (!config.resolve.fallback) config.resolve.fallback = {};
|
|
||||||
Object.assign(config.resolve.fallback, {
|
|
||||||
buffer: require.resolve('buffer'),
|
|
||||||
crypto: false,
|
|
||||||
stream: require.resolve('stream-browserify'),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!config.plugins) config.plugins = [];
|
|
||||||
config.plugins.push(
|
|
||||||
new webpack.DefinePlugin({
|
|
||||||
'process.env.ENV': JSON.stringify('prod'),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
config.plugins.push(
|
|
||||||
new webpack.ProvidePlugin({
|
|
||||||
process: 'process/browser.js',
|
|
||||||
Buffer: ['buffer', 'Buffer'],
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!config.ignoreWarnings) config.ignoreWarnings = [];
|
|
||||||
config.ignoreWarnings.push(/Failed to parse source map/);
|
|
||||||
|
|
||||||
return config;
|
|
||||||
},
|
|
||||||
};
|
|
|
@ -2,26 +2,23 @@
|
||||||
"name": "store-reactjs-chat",
|
"name": "store-reactjs-chat",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"homepage": "/examples/store-reactjs-chat",
|
"homepage": "/store-reactjs-chat",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@testing-library/jest-dom": "^5.16.4",
|
"@testing-library/jest-dom": "^5.16.4",
|
||||||
"@testing-library/react": "^13.2.0",
|
"@testing-library/react": "^13.2.0",
|
||||||
"@testing-library/user-event": "^14.1.1",
|
"@testing-library/user-event": "^14.1.1",
|
||||||
"assert": "^2.0.0",
|
"js-waku": "0.24.0-e3bef47",
|
||||||
"buffer": "^6.0.3",
|
|
||||||
"js-waku": "^0.24.0",
|
|
||||||
"protons": "^2.0.3",
|
"protons": "^2.0.3",
|
||||||
"react": "^18.1.0",
|
"react": "^18.1.0",
|
||||||
"react-dom": "^18.1.0",
|
"react-dom": "^18.1.0",
|
||||||
"react-scripts": "5.0.1",
|
"react-scripts": "5.0.1",
|
||||||
"stream-browserify": "^3.0.0",
|
|
||||||
"web-vitals": "^2.1.4"
|
"web-vitals": "^2.1.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "cra-webpack-rewired start",
|
"start": "react-scripts start",
|
||||||
"build": "cra-webpack-rewired build",
|
"build": "react-scripts build",
|
||||||
"test": "cra-webpack-rewired test",
|
"test": "exit 0",
|
||||||
"eject": "cra-webpack-rewired eject"
|
"eject": "react-scripts eject"
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"extends": [
|
"extends": [
|
||||||
|
@ -45,8 +42,6 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@testing-library/dom": "^8.16.1",
|
"@testing-library/dom": "^8.16.1",
|
||||||
"cra-webpack-rewired": "^1.0.1",
|
|
||||||
"process": "^0.11.10",
|
|
||||||
"typescript": "^4.7.4"
|
"typescript": "^4.7.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,7 @@
|
||||||
import { Waku } from "js-waku";
|
import { waitForRemotePeer, utils } from "js-waku";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import protons from "protons";
|
import protons from "protons";
|
||||||
|
import { createWaku } from "js-waku/lib/create_waku";
|
||||||
|
|
||||||
const ContentTopic = "/toy-chat/2/huilong/proto";
|
const ContentTopic = "/toy-chat/2/huilong/proto";
|
||||||
|
|
||||||
|
@ -22,10 +23,12 @@ function App() {
|
||||||
|
|
||||||
setWakuStatus("Starting");
|
setWakuStatus("Starting");
|
||||||
|
|
||||||
Waku.create({ bootstrap: { default: true } }).then((waku) => {
|
createWaku({ defaultBootstrap: true }).then((waku) => {
|
||||||
|
waku.start().then(() => {
|
||||||
setWaku(waku);
|
setWaku(waku);
|
||||||
setWakuStatus("Connecting");
|
setWakuStatus("Connecting");
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}, [waku, wakuStatus]);
|
}, [waku, wakuStatus]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
@ -34,7 +37,7 @@ function App() {
|
||||||
// We do not handle disconnection/re-connection in this example
|
// We do not handle disconnection/re-connection in this example
|
||||||
if (wakuStatus === "Connected") return;
|
if (wakuStatus === "Connected") return;
|
||||||
|
|
||||||
waku.waitForRemotePeer().then(() => {
|
waitForRemotePeer(waku, ["store"]).then(() => {
|
||||||
// We are now connected to a store node
|
// We are now connected to a store node
|
||||||
setWakuStatus("Connected");
|
setWakuStatus("Connected");
|
||||||
});
|
});
|
||||||
|
@ -55,6 +58,9 @@ function App() {
|
||||||
// 7 days/week, 24 hours/day, 60min/hour, 60secs/min, 100ms/sec
|
// 7 days/week, 24 hours/day, 60min/hour, 60secs/min, 100ms/sec
|
||||||
startTime.setTime(startTime.getTime() - 7 * 24 * 60 * 60 * 1000);
|
startTime.setTime(startTime.getTime() - 7 * 24 * 60 * 60 * 1000);
|
||||||
|
|
||||||
|
// TODO: Remove this timeout once https://github.com/status-im/js-waku/issues/913 is done
|
||||||
|
setTimeout(
|
||||||
|
() =>
|
||||||
waku.store
|
waku.store
|
||||||
.queryHistory([ContentTopic], {
|
.queryHistory([ContentTopic], {
|
||||||
callback: processMessages,
|
callback: processMessages,
|
||||||
|
@ -62,7 +68,10 @@ function App() {
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
console.log("Failed to retrieve messages", e);
|
console.log("Failed to retrieve messages", e);
|
||||||
});
|
setWakuStatus("Error Encountered");
|
||||||
|
}),
|
||||||
|
200
|
||||||
|
);
|
||||||
}, [waku, wakuStatus]);
|
}, [waku, wakuStatus]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -92,15 +101,20 @@ function decodeMessage(wakuMessage) {
|
||||||
const time = new Date();
|
const time = new Date();
|
||||||
time.setTime(timestamp);
|
time.setTime(timestamp);
|
||||||
|
|
||||||
const utf8Text = Buffer.from(text).toString("utf-8");
|
const utf8Text = utils.bytesToUtf8(text);
|
||||||
|
|
||||||
return { text: utf8Text, timestamp: time, nick };
|
return {
|
||||||
|
text: utf8Text,
|
||||||
|
timestamp: time,
|
||||||
|
nick,
|
||||||
|
timestampInt: wakuMessage.timestamp,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function Messages(props) {
|
function Messages(props) {
|
||||||
return props.messages.map(({ text, timestamp, nick }) => {
|
return props.messages.map(({ text, timestamp, nick, timestampInt }) => {
|
||||||
return (
|
return (
|
||||||
<li>
|
<li key={timestampInt}>
|
||||||
({formatDate(timestamp)}) {nick}: {text}
|
({formatDate(timestamp)}) {nick}: {text}
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import React from 'react';
|
import React from "react";
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from "react-dom/client";
|
||||||
import './index.css';
|
import "./index.css";
|
||||||
import App from './App';
|
import App from "./App";
|
||||||
|
|
||||||
ReactDOM.render(
|
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||||
|
root.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<App />
|
<App />
|
||||||
</React.StrictMode>,
|
</React.StrictMode>
|
||||||
document.getElementById('root')
|
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue