Implement /nick command

This commit is contained in:
Franck Royer 2021-04-23 14:43:39 +10:00
parent 5ebceecf92
commit f5edd09fa9
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 18 additions and 2 deletions

View File

@ -15,6 +15,7 @@ export const ChatContentTopic = 'dingpu';
export default function App() {
let [stateMessages, setMessages] = useState<ChatMessage[]>([]);
let [stateWaku, setWaku] = useState<Waku | undefined>(undefined);
let [nick, setNick] = useState<string>('web-chat');
useEffect(() => {
async function initWaku() {
@ -80,11 +81,21 @@ export default function App() {
} else {
switch (cmd) {
case '/help':
commandResponses.push('/nick <nickname>: set a new nickname');
commandResponses.push(
'/connect <Multiaddr>: connect to the given peer'
);
commandResponses.push('/help: Display this help');
break;
case '/nick':
const arg = args.shift();
if (!arg) {
commandResponses.push('No nick provided');
} else {
setNick(arg);
commandResponses.push(`New nick: ${arg}`);
}
break;
case '/connect':
const peer = args.shift();
if (!peer) {
@ -138,7 +149,11 @@ export default function App() {
<div className="chat-room">
<WakuContext.Provider value={{ waku: stateWaku }}>
<Paper>
<Room lines={stateMessages} commandHandler={commandHandler} />
<Room
nick={nick}
lines={stateMessages}
commandHandler={commandHandler}
/>
</Paper>
</WakuContext.Provider>
</div>

View File

@ -9,6 +9,7 @@ import { useWaku } from './WakuContext';
interface Props {
lines: ChatMessage[];
commandHandler: (cmd: string) => void;
nick: string;
}
export default function Room(props: Props) {
@ -25,7 +26,7 @@ export default function Room(props: Props) {
} else {
const chatMessage = new ChatMessage(
new Date(),
'web-chat',
props.nick,
messageToSend
);
const wakuMsg = WakuMessage.fromBytes(