mirror of https://github.com/waku-org/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 (
|
return (
|
||||||
<div className='App'>
|
<div className='App'>
|
||||||
<div className='chat-room'>
|
<div className='chat-room'>
|
||||||
<WakuContext.Provider value={{ waku: state.waku }}>
|
<WakuContext.Provider value={{ waku: state.waku }}>
|
||||||
<Paper>
|
<Paper>
|
||||||
<Room lines={state.messages} />
|
<Room lines={state.messages} commandHandler={commandHandler}/>
|
||||||
</Paper>
|
</Paper>
|
||||||
</WakuContext.Provider>
|
</WakuContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -15,6 +15,7 @@ import { useWaku } from './WakuContext';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
lines: ChatMessage[],
|
lines: ChatMessage[],
|
||||||
|
commandHandler: (cmd: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Room (props :Props) {
|
export default function Room (props :Props) {
|
||||||
|
@ -26,10 +27,14 @@ export default function Room (props :Props) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendMessage = async () => {
|
const sendMessage = async () => {
|
||||||
|
if (messageToSend.startsWith('/')) {
|
||||||
|
props.commandHandler(messageToSend)
|
||||||
|
} else {
|
||||||
const chatMessage = new ChatMessage(new Date(), 'web-chat', messageToSend);
|
const chatMessage = new ChatMessage(new Date(), 'web-chat', messageToSend);
|
||||||
const wakuMsg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
|
const wakuMsg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
|
||||||
await waku!.relay.send(wakuMsg);
|
await waku!.relay.send(wakuMsg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid container spacing={2}>
|
<Grid container spacing={2}>
|
||||||
|
|
Loading…
Reference in New Issue