From 48549bf4de298dfbc7e097e6685bf3513245c34c Mon Sep 17 00:00:00 2001 From: Carter Date: Fri, 6 Dec 2024 10:14:09 -0600 Subject: [PATCH] chat automatically adjusts scrollbar so as to not effect the chat view when more chats are loaded --- src/chat/Chat.tsx | 15 ++++++++------- src/chat/ChatDrawer.tsx | 2 +- src/chat/ChatMessage.tsx | 2 +- src/chat/ChatOutput.tsx | 39 +++++++++++++++++---------------------- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/chat/Chat.tsx b/src/chat/Chat.tsx index 2fb66dc..8384572 100644 --- a/src/chat/Chat.tsx +++ b/src/chat/Chat.tsx @@ -1,14 +1,12 @@ import { Box, CircularProgress } from "@mui/material"; -import { useGlobalState, useNoteAPI, useTeamAPI } from "providers"; +import { useGlobalState, useNoteAPI } from "providers"; import React from "react"; -import { useCallback } from "react"; import { useEffect } from "react"; import { useState } from "react"; import ChatInput from "./ChatInput"; import ChatOutput from "./ChatOutput"; import { Note } from "models"; import { pond } from "protobuf-ts/pond"; -import moment from "moment-timezone"; interface Props { objectKey: string; @@ -24,17 +22,20 @@ export default function Chat(props: Props) { const [totalMessages, setTotalMessages] = useState(0); const [scrollPos, setScrollPos] = useState(0); const [{ user }] = useGlobalState(); - const [attachmentMap, setAttachmentMap] = useState>(new Map()); + // const [attachmentMap, setAttachmentMap] = useState>(new Map()); useEffect(() => { - console.log(totalMessages) - }, [totalMessages]) + console.log(scrollPos) + }, [scrollPos]) const loadChats = () => { setLoading(true) noteAPI.listChats(10, chats.length, "desc", "timestamp", objectKey).then(resp => { + // if (loaded) setScrollPos(chats.length-10) + // setScrollPos(chats.length) setChats(resp.data.chats ? resp.data.chats.reverse().concat(chats) : []) + setScrollPos(resp.data.chats.reverse().concat(chats).length) setTotalMessages(resp.data.total) }).finally(() => { setLoading(false) @@ -90,7 +91,7 @@ export default function Chat(props: Props) { {team.name()} Chat - + diff --git a/src/chat/ChatMessage.tsx b/src/chat/ChatMessage.tsx index b3aa977..ac389ab 100644 --- a/src/chat/ChatMessage.tsx +++ b/src/chat/ChatMessage.tsx @@ -86,7 +86,7 @@ export default function ChatMessage(props: Props) { } return perms } -const permissions = generatePermissions() + const permissions = generatePermissions() const deleteMessage = () => { if (!props.chat?.note?.settings) return diff --git a/src/chat/ChatOutput.tsx b/src/chat/ChatOutput.tsx index 24b30b6..8b659ad 100644 --- a/src/chat/ChatOutput.tsx +++ b/src/chat/ChatOutput.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef } from "react"; +import { useEffect, useRef, useState } from "react"; import ChatMessage from "./ChatMessage"; import { Box, Button, ListItemButton } from "@mui/material"; @@ -19,7 +19,11 @@ interface Props { * @returns List of chat messages */ export default function ChatOutput(props: Props) { - const ScrollTo = () => { + const [disableScrollTo, setDisableScrollTo] = useState(false); + const [scrollOffset, setScrollOffset] = useState(0); + const containerRef = useRef(null); + + const ScrollToBottom = () => { const elementRef = useRef(null); useEffect(() => elementRef?.current?.scrollIntoView({ @@ -30,23 +34,17 @@ export default function ChatOutput(props: Props) { return elem; }; - // const LoadMore = () => { - // const elementRef = useRef(null); - - // let elem = - //
- // - //
; - - // if(CheckVisible(elementRef, "0px")) - // { - // props.loadMore() - // } - - // return elem; - // } + useEffect(() => { + if (!containerRef.current) return; + const container = containerRef.current; + container.scrollTop = container.scrollHeight - scrollOffset + }, [props.messages]) const seeMore = () => { + if (!containerRef.current) return; + setDisableScrollTo(true) + const container = containerRef.current; + setScrollOffset(container.scrollHeight - container.scrollTop) props.loadMore(); }; @@ -59,15 +57,13 @@ export default function ChatOutput(props: Props) { // attachments={props.attachmentMap?.get(curMessage.note?.settings?.key)} removeNoteMethod={props.removeNoteMethod} /> - {/* {props.scrollPos !== 0 && index === props.scrollPos && } */} - )); return display; }; return ( - @@ -77,8 +73,7 @@ export default function ChatOutput(props: Props) { )} {displayMessages()} - {props.scrollPos === 0 && } - {/* */} + {!disableScrollTo && } ); }