|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
import useWebSocket, { ReadyState } from 'react-use-websocket';
|
|
|
|
|
|
|
|
|
|
import Unaboomer from '../img/unaboomer.png';
|
|
|
|
|
|
|
|
|
@ -53,4 +54,108 @@ export class StatsPanel extends React.Component {
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TrollboxPanel(props) {
|
|
|
|
|
const [showChat, setShowChat] = useState(false);
|
|
|
|
|
const [history, setHistory] = useState(() => {
|
|
|
|
|
const saved = localStorage.getItem('trollboxHistory');
|
|
|
|
|
return saved || JSON.stringify([])
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { sendJsonMessage, readyState } = useWebSocket(props.ws, {
|
|
|
|
|
onOpen: () => {
|
|
|
|
|
console.log('WebSocket connection established.');
|
|
|
|
|
},
|
|
|
|
|
onMessage: (msg) => {
|
|
|
|
|
console.log(JSON.parse(msg.data))
|
|
|
|
|
let h = JSON.parse(history);
|
|
|
|
|
if (h.length > 500) {
|
|
|
|
|
h = h.splice(-500);
|
|
|
|
|
}
|
|
|
|
|
h.push(JSON.parse(msg.data));
|
|
|
|
|
setHistory(JSON.stringify(h));
|
|
|
|
|
},
|
|
|
|
|
shouldReconnect: () => true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
localStorage.setItem('trollboxHistory', history);
|
|
|
|
|
}, [history]);
|
|
|
|
|
|
|
|
|
|
const connectionStatus = {
|
|
|
|
|
[ReadyState.CONNECTING]: 'Connecting',
|
|
|
|
|
[ReadyState.OPEN]: 'Open',
|
|
|
|
|
[ReadyState.CLOSING]: 'Closing',
|
|
|
|
|
[ReadyState.CLOSED]: 'Closed',
|
|
|
|
|
[ReadyState.UNINSTANTIATED]: 'Uninstantiated',
|
|
|
|
|
}[readyState];
|
|
|
|
|
|
|
|
|
|
function send(event) {
|
|
|
|
|
const message = (new FormData(event.target)).get("message");
|
|
|
|
|
if (message) {
|
|
|
|
|
sendJsonMessage({
|
|
|
|
|
from: props.address,
|
|
|
|
|
date: new Date(),
|
|
|
|
|
message
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
event.target.reset();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{props.ws && props.address && (
|
|
|
|
|
<div id="trollbox">
|
|
|
|
|
{readyState == ReadyState.OPEN && showChat && (
|
|
|
|
|
<>
|
|
|
|
|
<button className="button" onClick={() => {
|
|
|
|
|
setShowChat(false);
|
|
|
|
|
}}>hide chat</button>
|
|
|
|
|
<form id="sendForm" onSubmit={(ev) => {
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
send(ev);
|
|
|
|
|
}}>
|
|
|
|
|
<input type="text" name="message" minLength="1" />
|
|
|
|
|
<button className='doThing' type="submit">Send</button>
|
|
|
|
|
</form>
|
|
|
|
|
<ul className="nopad">
|
|
|
|
|
{JSON.parse(history).length > 0 && JSON.parse(history).map((message, idx) => (
|
|
|
|
|
<li key={idx} className="messageLine">
|
|
|
|
|
<span className="fromAddress nopad">
|
|
|
|
|
<a href={"https://etherscan.io/address/" + message.from} target="_blank">
|
|
|
|
|
{message.from.slice(-6)}
|
|
|
|
|
</a>
|
|
|
|
|
:</span>
|
|
|
|
|
<span className="messageText">{message.message}</span>
|
|
|
|
|
<span className="messageDate">
|
|
|
|
|
{new Date(message.date).getMonth() + 1}/
|
|
|
|
|
{new Date(message.date).getDate()} @
|
|
|
|
|
{new Date(message.date).getUTCHours()}:
|
|
|
|
|
{new Date(message.date).getUTCMinutes()} UTC
|
|
|
|
|
</span>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{readyState == ReadyState.OPEN && !showChat && (
|
|
|
|
|
<button className="button" onClick={() => {
|
|
|
|
|
setShowChat(!showChat);
|
|
|
|
|
}}>
|
|
|
|
|
{showChat && <>∨ hide chat</> || <>^ show chat</>}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{readyState == ReadyState.CONNECTING && (
|
|
|
|
|
<p>connecting to trollbox...</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|