Fix loading messages (#97)

This commit is contained in:
Szymon Szlachtowicz 2021-10-26 09:16:07 +02:00 committed by GitHub
parent 7a289e5eab
commit f957aa76cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 5 deletions

View File

@ -129,7 +129,7 @@ export function Chat({
showMembers={showMembers}
community={communityData}
showCommunity={!showChannels}
loadPrevDay={() => loadPrevDay(activeChannel.name)}
loadPrevDay={() => loadPrevDay(activeChannel.id)}
onCommunityClick={showModal}
fetchMetadata={fetchMetadata}
loadingMessages={loadingMessages}

View File

@ -3,6 +3,7 @@ import styled from "styled-components";
import { ChatMessage } from "../../models/ChatMessage";
import { Metadata } from "../../models/Metadata";
import { equalDate } from "../../utils";
import { LoadingIcon } from "../Icons/LoadingIcon";
import { UserIcon } from "../Icons/UserIcon";
import { LinkModal } from "../Modals/LinkModal";
@ -26,7 +27,7 @@ export function ChatMessages({
}: ChatMessagesProps) {
const [scrollOnBot, setScrollOnBot] = useState(true);
const ref = useRef<HTMLHeadingElement>(null);
const today = useMemo(() => new Date().getDay(), []);
const today = useMemo(() => new Date(), []);
useEffect(() => {
if (ref && ref.current && scrollOnBot) {
ref.current.scrollTop = ref.current.scrollHeight;
@ -38,6 +39,7 @@ export function ChatMessages({
if (
(ref?.current?.clientHeight ?? 0) >= (ref?.current?.scrollHeight ?? 0)
) {
setScrollOnBot(true);
loadPrevDay();
}
}
@ -84,10 +86,9 @@ export function ChatMessages({
return (
<MessageOuterWrapper key={message.date.getTime()}>
{(idx === 0 ||
messages[idx - 1].date.getDay() !=
messages[idx].date.getDay()) && (
!equalDate(messages[idx - 1].date, message.date)) && (
<DateSeparator>
{message.date.getDay() === today
{equalDate(message.date, today)
? "Today"
: message.date.toLocaleDateString()}
</DateSeparator>

View File

@ -0,0 +1,7 @@
export function equalDate(a: Date, b: Date) {
return (
a.getDate() === b.getDate() &&
a.getMonth() === b.getMonth() &&
a.getFullYear() === b.getFullYear()
);
}

View File

@ -9,3 +9,4 @@ export {
} from "./identityStorage";
export { reduceString } from "./reduceString";
export { uintToImgUrl } from "./uintToImgUrl";
export { equalDate } from "./equalDate";