mirror of https://github.com/status-im/js-waku.git
Enable using commands
This commit is contained in:
parent
4c8a243c51
commit
2297d09d58
|
@ -67,12 +67,28 @@ export default function App() {
|
|||
}
|
||||
});
|
||||
|
||||
const commandHandler = (cmd: string) => {
|
||||
let commandResponse = 'internal error';
|
||||
switch (cmd) {
|
||||
case '/help':
|
||||
commandResponse = '/help Display this help';
|
||||
break;
|
||||
default:
|
||||
commandResponse = 'Unknown Command'
|
||||
}
|
||||
|
||||
setState(({waku, messages}) => {
|
||||
messages.push(new ChatMessage(new Date(), 'Command Response', commandResponse));
|
||||
return {waku, messages};
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='App'>
|
||||
<div className='chat-room'>
|
||||
<WakuContext.Provider value={{ waku: state.waku }}>
|
||||
<Paper>
|
||||
<Room lines={state.messages} />
|
||||
<Room lines={state.messages} commandHandler={commandHandler}/>
|
||||
</Paper>
|
||||
</WakuContext.Provider>
|
||||
</div>
|
||||
|
|
|
@ -15,6 +15,7 @@ import { useWaku } from './WakuContext';
|
|||
|
||||
interface Props {
|
||||
lines: ChatMessage[],
|
||||
commandHandler: (cmd: string) => void;
|
||||
}
|
||||
|
||||
export default function Room (props :Props) {
|
||||
|
@ -26,9 +27,13 @@ export default function Room (props :Props) {
|
|||
}
|
||||
|
||||
const sendMessage = async () => {
|
||||
const chatMessage = new ChatMessage(new Date(), 'web-chat', messageToSend);
|
||||
const wakuMsg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
|
||||
await waku!.relay.send(wakuMsg);
|
||||
if (messageToSend.startsWith('/')) {
|
||||
props.commandHandler(messageToSend)
|
||||
} else {
|
||||
const chatMessage = new ChatMessage(new Date(), 'web-chat', messageToSend);
|
||||
const wakuMsg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
|
||||
await waku!.relay.send(wakuMsg);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
Loading…
Reference in New Issue