Load last day messages (#43)

This commit is contained in:
Szymon Szlachtowicz 2021-10-06 11:40:05 +02:00 committed by GitHub
parent 655399b03d
commit b40ce941d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 6 deletions

View File

@ -29,6 +29,8 @@ export function Chat({ theme, community }: ChatProps) {
sendMessage, sendMessage,
notifications, notifications,
clearNotifications, clearNotifications,
loadNextDay,
lastMessage,
} = useMessenger( } = useMessenger(
activeChannel.name, activeChannel.name,
channels.map((channel) => channel.name) channels.map((channel) => channel.name)
@ -59,6 +61,8 @@ export function Chat({ theme, community }: ChatProps) {
showMembers={showMembers} showMembers={showMembers}
community={community} community={community}
showCommunity={!showChannels} showCommunity={!showChannels}
loadNextDay={() => loadNextDay(activeChannel.name)}
lastMessage={lastMessage}
/> />
) : ( ) : (
<Loading>Connecting to waku</Loading> <Loading>Connecting to waku</Loading>

View File

@ -28,6 +28,8 @@ interface ChatBodyProps {
notifications: { [id: string]: number }; notifications: { [id: string]: number };
setActiveChannel: (val: ChannelData) => void; setActiveChannel: (val: ChannelData) => void;
activeChannelId: number; activeChannelId: number;
loadNextDay: () => void;
lastMessage: Date;
} }
export function ChatBody({ export function ChatBody({
@ -42,6 +44,8 @@ export function ChatBody({
notifications, notifications,
setActiveChannel, setActiveChannel,
activeChannelId, activeChannelId,
loadNextDay,
lastMessage,
}: ChatBodyProps) { }: ChatBodyProps) {
const narrow = useNarrow(); const narrow = useNarrow();
const [showChannelsList, setShowChannelsList] = useState(false); const [showChannelsList, setShowChannelsList] = useState(false);
@ -90,7 +94,9 @@ export function ChatBody({
</ChatTopbar> </ChatTopbar>
{!showChannelsList && !showMembersList && ( {!showChannelsList && !showMembersList && (
<> <>
{" "} <button onClick={loadNextDay}>
Last message date {lastMessage.toDateString()}
</button>{" "}
{messages.length > 0 ? ( {messages.length > 0 ? (
<ChatMessages messages={messages} theme={theme} /> <ChatMessages messages={messages} theme={theme} />
) : ( ) : (

View File

@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useRef } from "react"; import React, { useEffect, useMemo, useRef, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
@ -14,13 +14,34 @@ type ChatMessagesProps = {
}; };
export function ChatMessages({ messages, theme }: ChatMessagesProps) { export function ChatMessages({ messages, theme }: ChatMessagesProps) {
const [scrollOnBot, setScrollOnBot] = useState(false);
const ref = useRef<HTMLHeadingElement>(null); const ref = useRef<HTMLHeadingElement>(null);
const today = useMemo(() => new Date().getDay(), []); const today = useMemo(() => new Date().getDay(), []);
useEffect(() => { useEffect(() => {
if (ref && ref.current) { if (ref && ref.current && scrollOnBot) {
ref.current.scrollTop = ref.current.scrollHeight; ref.current.scrollTop = ref.current.scrollHeight;
} }
}, [messages, messages.length]); }, [messages, messages.length, scrollOnBot]);
useEffect(() => {
const setScroll = () => {
if (ref && ref.current) {
if (
ref.current.scrollTop + ref.current.clientHeight ==
ref.current.scrollHeight
) {
setScrollOnBot(true);
} else {
if (scrollOnBot == true) {
setScrollOnBot(false);
}
}
}
};
ref.current?.addEventListener("scroll", setScroll);
return () => ref.current?.removeEventListener("scroll", setScroll);
}, [ref, scrollOnBot]);
return ( return (
<MessagesWrapper ref={ref}> <MessagesWrapper ref={ref}>
{messages.map((message, idx) => { {messages.map((message, idx) => {

View File

@ -37,6 +37,9 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
const [notifications, setNotifications] = useState<{ const [notifications, setNotifications] = useState<{
[chatId: string]: number; [chatId: string]: number;
}>({}); }>({});
const [lastLoadTime, setLastLoadTime] = useState<{
[chatId: string]: Date;
}>({});
const clearNotifications = useCallback((id: string) => { const clearNotifications = useCallback((id: string) => {
setNotifications((prevNotifications) => { setNotifications((prevNotifications) => {
@ -111,10 +114,20 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
Promise.all( Promise.all(
chatIdList.map(async (id) => { chatIdList.map(async (id) => {
const today = new Date();
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
yesterday.setHours(0, 0, 0, 0);
setLastLoadTime((prev) => {
return {
...prev,
[id]: yesterday,
};
});
await messenger.retrievePreviousMessages( await messenger.retrievePreviousMessages(
id, id,
new Date(0), yesterday,
new Date(), today,
(messages) => messages.forEach((msg) => addNewMessage(msg, id)) (messages) => messages.forEach((msg) => addNewMessage(msg, id))
); );
clearNotifications(id); clearNotifications(id);
@ -126,6 +139,29 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
createMessenger(); createMessenger();
}, []); }, []);
const loadNextDay = useCallback(
(id: string) => {
console.log(id);
if (messenger) {
const endTime = lastLoadTime[id];
const startTime = new Date();
startTime.setDate(endTime.getDate() - 1);
startTime.setHours(0, 0, 0, 0);
messenger.retrievePreviousMessages(id, startTime, endTime, (messages) =>
messages.forEach((msg) => addNewMessage(msg, id))
);
setLastLoadTime((prev) => {
return {
...prev,
[id]: startTime,
};
});
}
},
[lastLoadTime, messenger]
);
const sendMessage = useCallback( const sendMessage = useCallback(
async (messageText: string) => { async (messageText: string) => {
await messenger?.sendMessage(messageText, chatId); await messenger?.sendMessage(messageText, chatId);
@ -149,5 +185,7 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
sendMessage, sendMessage,
notifications, notifications,
clearNotifications, clearNotifications,
loadNextDay,
lastMessage: lastLoadTime[chatId],
}; };
} }