Enable using commands

This commit is contained in:
Franck Royer 2021-04-22 16:12:28 +10:00
parent 4c8a243c51
commit 2297d09d58
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 25 additions and 4 deletions

View File

@ -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>

View File

@ -15,6 +15,7 @@ import { useWaku } from './WakuContext';
interface Props {
lines: ChatMessage[],
commandHandler: (cmd: string) => void;
}
export default function Room (props :Props) {
@ -26,10 +27,14 @@ export default function Room (props :Props) {
}
const sendMessage = async () => {
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 (
<Grid container spacing={2}>