diff --git a/examples/web-chat/src/App.tsx b/examples/web-chat/src/App.tsx index 63c41f5b68..ff697af13f 100644 --- a/examples/web-chat/src/App.tsx +++ b/examples/web-chat/src/App.tsx @@ -45,6 +45,29 @@ const themes = { export const ChatContentTopic = 'dingpu'; +async function retrieveStoreMessages( + waku: Waku, + peerId: PeerId, + setArchivedMessages: (value: ChatMessage[]) => void +): Promise { + const callback = (wakuMessages: WakuMessage[]): void => { + const messages = wakuMessages + .map((wakuMsg) => wakuMsg.payload) + .filter((payload) => !!payload) + .map((payload) => ChatMessage.decode(payload as Uint8Array)); + setArchivedMessages(messages); + }; + + const res = await waku.store.queryHistory({ + peerId, + contentTopics: [ChatContentTopic], + pageSize: 5, + callback, + }); + + return res ? res.length : 0; +} + export default function App() { let [newMessages, setNewMessages] = useState([]); let [archivedMessages, setArchivedMessages] = useState([]); @@ -61,6 +84,7 @@ export default function App() { } }; + // TODO: Split this const handleProtocolChange = async ( waku: Waku, { peerId, protocols }: { peerId: PeerId; protocols: string[] } @@ -68,18 +92,12 @@ export default function App() { if (protocols.includes(StoreCodec)) { console.log(`${peerId.toB58String()}: retrieving archived messages}`); try { - const response = await waku.store.queryHistory({ + const length = await retrieveStoreMessages( + waku, peerId, - contentTopics: [ChatContentTopic], - }); - console.log(`${peerId.toB58String()}: messages retrieved:`, response); - if (response) { - const messages = response - .map((wakuMsg) => wakuMsg.payload) - .filter((payload) => !!payload) - .map((payload) => ChatMessage.decode(payload as Uint8Array)); - setArchivedMessages(messages); - } + setArchivedMessages + ); + console.log(`${peerId.toB58String()}: messages retrieved:`, length); } catch (e) { console.log( `${peerId.toB58String()}: error encountered when retrieving archived messages`, diff --git a/examples/web-chat/src/ChatList.tsx b/examples/web-chat/src/ChatList.tsx index 9957d9df55..00273671e7 100644 --- a/examples/web-chat/src/ChatList.tsx +++ b/examples/web-chat/src/ChatList.tsx @@ -14,10 +14,11 @@ interface Props { export default function ChatList(props: Props) { const [messages, setMessages] = useState([]); + const [groupedMessages, setGroupedMessages] = useState([]); let updatedMessages; if (IsThereNewMessages(props.newMessages, messages)) { - updatedMessages = messages.slice().concat(props.newMessages); + updatedMessages = messages.concat(props.newMessages); if (IsThereNewMessages(props.archivedMessages, updatedMessages)) { updatedMessages = copyMergeUniqueReplace( props.archivedMessages, @@ -34,32 +35,31 @@ export default function ChatList(props: Props) { } if (updatedMessages) { + setGroupedMessages(groupMessagesBySender(updatedMessages)); setMessages(updatedMessages); } - const messagesGroupedBySender = groupMessagesBySender(messages).map( - (currentMessageGroup) => ( - - {currentMessageGroup.map((currentMessage) => ( - - {currentMessage.payloadAsUtf8} - - ))} - - ) - ); + const renderedGroupedMessages = groupedMessages.map((currentMessageGroup) => ( + + {currentMessageGroup.map((currentMessage) => ( + + {currentMessage.payloadAsUtf8} + + ))} + + )); return ( - {messagesGroupedBySender} + {renderedGroupedMessages} );