js-waku/web-chat/src/Send.tsx

30 lines
754 B
TypeScript
Raw Normal View History

2021-04-19 11:34:13 +10:00
import React from 'react';
2021-04-21 15:51:52 +10:00
import { ChatMessage } from 'waku-chat/chat_message';
import { WakuMessage } from 'waku/waku_message';
import { ChatContentTopic } from './App';
2021-04-19 11:34:13 +10:00
import { useWaku } from './WakuContext';
2021-04-19 13:33:23 +10:00
import { Button } from '@material-ui/core';
2021-04-19 11:34:13 +10:00
interface Props {
2021-04-19 12:55:33 +10:00
message: string
2021-04-19 11:34:13 +10:00
}
2021-04-19 12:55:33 +10:00
const Send = (props: Props) => {
2021-04-19 11:34:13 +10:00
const { waku } = useWaku();
2021-04-19 13:33:23 +10:00
const handleClick = async () => {
2021-04-21 15:51:52 +10:00
const chatMessage = new ChatMessage(new Date(), 'web-chat', props.message);
const wakuMsg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
await waku!.relay.send(wakuMsg);
2021-04-19 13:33:23 +10:00
};
2021-04-19 11:34:13 +10:00
return (
2021-04-19 14:54:39 +10:00
<Button variant="contained" color="primary" size="large" onClick={handleClick}>
2021-04-19 13:33:23 +10:00
Send
</Button>
2021-04-19 11:34:13 +10:00
);
};
export default Send;