diff --git a/examples/eth-dm/src/App.tsx b/examples/eth-dm/src/App.tsx index b9c48b5ce1..70f1d194fe 100644 --- a/examples/eth-dm/src/App.tsx +++ b/examples/eth-dm/src/App.tsx @@ -13,6 +13,7 @@ import { } from './crypto'; import * as EthCrypto from 'eth-crypto'; import { DirectMessage, PublicKeyMessage } from './messages'; +import { Message, Messages } from './Messages'; const PublicKeyContentTopic = '/eth-dm/1/public-key/json'; const DirectMessageContentTopic = '/eth-dm/1/direct-message/json'; @@ -25,7 +26,7 @@ function App() { const [ethDmKeyPair, setEthDmKeyPair] = useState(); const [publicKeyMsg, setPublicKeyMsg] = useState(); const [publicKeys, setPublicKeys] = useState>(new Map()); - const [messages, setMessages] = useState([]); + const [messages, setMessages] = useState([]); useEffect(() => { if (provider) return; @@ -155,7 +156,7 @@ function App() { > Send Direct Message - {messages} + ); @@ -222,22 +223,27 @@ function handlePublicKeyMessage( } async function handleDirectMessage( - setter: Dispatch>, + setter: Dispatch>, privateKey: string, wakuMsg: WakuMessage ) { console.log('Waku Message received:', wakuMsg); if (!wakuMsg.payload) return; const directMessage: DirectMessage = decode(wakuMsg.payload); - const msg = await EthCrypto.decryptWithPrivateKey( + const text = await EthCrypto.decryptWithPrivateKey( privateKey, directMessage.encMessage ); - console.log('Message decrypted:', msg); - setter((prevMsgs: string[]) => { + const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date(); + + console.log('Message decrypted:', text); + setter((prevMsgs: Message[]) => { const copy = prevMsgs.slice(); - copy.push(msg); + copy.push({ + text: text, + timestamp: timestamp, + }); return copy; }); } diff --git a/examples/eth-dm/src/Messages.tsx b/examples/eth-dm/src/Messages.tsx new file mode 100644 index 0000000000..d12756f91a --- /dev/null +++ b/examples/eth-dm/src/Messages.tsx @@ -0,0 +1,30 @@ +export interface Message { + text: string; + timestamp: Date; +} + +export interface Props { + messages: Message[]; +} + +export function Messages(props: Props) { + const messages = props.messages.map((msg) => { + return ( +
  • + {formatDisplayDate(msg.timestamp)} {msg.text} +
  • + ); + }); + + return
      {messages}
    ; +} + +function formatDisplayDate(timestamp: Date): string { + return timestamp.toLocaleString([], { + month: 'short', + day: 'numeric', + hour: 'numeric', + minute: '2-digit', + hour12: false, + }); +}