mirror of
https://github.com/waku-org/js-waku.git
synced 2025-01-27 12:45:21 +00:00
Remove ChatMessage
The aim of this class was to remove the items in the chat list with duplicate keys. However, the implementation does not achieve this goal.
This commit is contained in:
parent
e91f7933c9
commit
83c17fdcc2
@ -2,8 +2,7 @@ import { multiaddr } from 'multiaddr';
|
|||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
import { ChatMessage } from './ChatMessage';
|
import { ChatMessage } from 'waku/chat_message';
|
||||||
import { ChatMessage as WakuChatMessage } from 'waku/chat_message';
|
|
||||||
import { WakuMessage } from 'waku/waku_message';
|
import { WakuMessage } from 'waku/waku_message';
|
||||||
import { StoreCodec } from 'waku/waku_store';
|
import { StoreCodec } from 'waku/waku_store';
|
||||||
import handleCommand from './command';
|
import handleCommand from './command';
|
||||||
@ -53,9 +52,7 @@ export default function App() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleRelayMessage = (wakuMsg: WakuMessage) => {
|
const handleRelayMessage = (wakuMsg: WakuMessage) => {
|
||||||
if (wakuMsg.payload) {
|
if (wakuMsg.payload) {
|
||||||
const chatMsg = ChatMessage.fromWakuChatMessage(
|
const chatMsg = ChatMessage.decode(wakuMsg.payload);
|
||||||
WakuChatMessage.decode(wakuMsg.payload)
|
|
||||||
);
|
|
||||||
if (chatMsg) {
|
if (chatMsg) {
|
||||||
setNewMessages([chatMsg]);
|
setNewMessages([chatMsg]);
|
||||||
}
|
}
|
||||||
@ -77,10 +74,7 @@ export default function App() {
|
|||||||
const messages = response
|
const messages = response
|
||||||
.map((wakuMsg) => wakuMsg.payload)
|
.map((wakuMsg) => wakuMsg.payload)
|
||||||
.filter((payload) => !!payload)
|
.filter((payload) => !!payload)
|
||||||
.map((payload) => WakuChatMessage.decode(payload as Uint8Array))
|
.map((payload) => ChatMessage.decode(payload as Uint8Array));
|
||||||
.map((wakuChatMessage) =>
|
|
||||||
ChatMessage.fromWakuChatMessage(wakuChatMessage)
|
|
||||||
);
|
|
||||||
setArchivedMessages(messages);
|
setArchivedMessages(messages);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -132,7 +126,7 @@ export default function App() {
|
|||||||
setNick
|
setNick
|
||||||
);
|
);
|
||||||
const commandMessages = response.map((msg) => {
|
const commandMessages = response.map((msg) => {
|
||||||
return new ChatMessage(new Date(), new Date(), command, msg);
|
return ChatMessage.fromUtf8String(new Date(), command, msg);
|
||||||
});
|
});
|
||||||
setNewMessages(commandMessages);
|
setNewMessages(commandMessages);
|
||||||
}}
|
}}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { ChatMessage } from './ChatMessage';
|
import { ChatMessage } from 'waku/chat_message';
|
||||||
import {
|
import {
|
||||||
Message,
|
Message,
|
||||||
MessageText,
|
MessageText,
|
||||||
@ -43,14 +43,14 @@ export default function ChatList(props: Props) {
|
|||||||
{currentMessageGroup.map((currentMessage) => (
|
{currentMessageGroup.map((currentMessage) => (
|
||||||
<Message
|
<Message
|
||||||
key={
|
key={
|
||||||
currentMessage.receivedTimestampMs.valueOf() +
|
currentMessage.timestamp.valueOf() +
|
||||||
currentMessage.nick +
|
currentMessage.nick +
|
||||||
currentMessage.message
|
currentMessage.payloadAsUtf8
|
||||||
}
|
}
|
||||||
authorName={currentMessage.nick}
|
authorName={currentMessage.nick}
|
||||||
date={formatDisplayDate(currentMessage)}
|
date={formatDisplayDate(currentMessage)}
|
||||||
>
|
>
|
||||||
<MessageText>{currentMessage.message}</MessageText>
|
<MessageText>{currentMessage.payloadAsUtf8}</MessageText>
|
||||||
</Message>
|
</Message>
|
||||||
))}
|
))}
|
||||||
</MessageGroup>
|
</MessageGroup>
|
||||||
@ -84,7 +84,7 @@ function groupMessagesBySender(messageArray: ChatMessage[]): ChatMessage[][] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function formatDisplayDate(message: ChatMessage): string {
|
function formatDisplayDate(message: ChatMessage): string {
|
||||||
return message.sentTimestamp.toLocaleString([], {
|
return message.timestamp.toLocaleString([], {
|
||||||
month: 'short',
|
month: 'short',
|
||||||
day: 'numeric',
|
day: 'numeric',
|
||||||
hour: 'numeric',
|
hour: 'numeric',
|
||||||
@ -127,14 +127,14 @@ function copyMergeUniqueReplace(
|
|||||||
copy.push(msg);
|
copy.push(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
copy.sort((a, b) => a.sentTimestamp.valueOf() - b.sentTimestamp.valueOf());
|
copy.sort((a, b) => a.timestamp.valueOf() - b.timestamp.valueOf());
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isEqual(lhs: ChatMessage, rhs: ChatMessage): boolean {
|
function isEqual(lhs: ChatMessage, rhs: ChatMessage): boolean {
|
||||||
return (
|
return (
|
||||||
lhs.nick === rhs.nick &&
|
lhs.nick === rhs.nick &&
|
||||||
lhs.message === rhs.message &&
|
lhs.payloadAsUtf8 === rhs.payloadAsUtf8 &&
|
||||||
lhs.sentTimestamp.toString() === rhs.sentTimestamp.toString()
|
lhs.timestamp.toString() === rhs.timestamp.toString()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
import { ChatMessage as WakuChatMessage } from 'waku/chat_message';
|
|
||||||
|
|
||||||
export class ChatMessage {
|
|
||||||
constructor(
|
|
||||||
public receivedTimestampMs: Date,
|
|
||||||
public sentTimestamp: Date,
|
|
||||||
public nick: string,
|
|
||||||
public message: string
|
|
||||||
) {}
|
|
||||||
|
|
||||||
static fromWakuChatMessage(wakuChatMessage: WakuChatMessage): ChatMessage {
|
|
||||||
return new ChatMessage(
|
|
||||||
new Date(),
|
|
||||||
wakuChatMessage.timestamp,
|
|
||||||
wakuChatMessage.nick,
|
|
||||||
wakuChatMessage.message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
import { ChatMessage } from './ChatMessage';
|
import { ChatMessage } from 'waku/chat_message';
|
||||||
import { ChatMessage as WakuChatMessage } from 'waku/chat_message';
|
|
||||||
import { WakuMessage } from 'waku/waku_message';
|
import { WakuMessage } from 'waku/waku_message';
|
||||||
import { ChatContentTopic } from './App';
|
import { ChatContentTopic } from './App';
|
||||||
import ChatList from './ChatList';
|
import ChatList from './ChatList';
|
||||||
@ -54,7 +53,7 @@ async function handleMessage(
|
|||||||
if (message.startsWith('/')) {
|
if (message.startsWith('/')) {
|
||||||
commandHandler(message);
|
commandHandler(message);
|
||||||
} else {
|
} else {
|
||||||
const chatMessage = new WakuChatMessage(new Date(), nick, message);
|
const chatMessage = ChatMessage.fromUtf8String(new Date(), nick, message);
|
||||||
const wakuMsg = WakuMessage.fromBytes(
|
const wakuMsg = WakuMessage.fromBytes(
|
||||||
chatMessage.encode(),
|
chatMessage.encode(),
|
||||||
ChatContentTopic
|
ChatContentTopic
|
||||||
|
Loading…
x
Reference in New Issue
Block a user