feat: chat panel UE done

This commit is contained in:
butterfly
2024-04-18 12:27:44 +08:00
parent 51a1d9f92a
commit b3559f99a2
39 changed files with 953 additions and 447 deletions

View File

@@ -1,11 +1,17 @@
import { RefObject, useEffect, useState } from "react";
import { RefObject, useEffect, useRef, useState } from "react";
export default function useScrollToBottom(
scrollRef: RefObject<HTMLDivElement>,
detach: boolean = false,
) {
// for auto-scroll
const detach = scrollRef?.current
? Math.abs(
scrollRef.current.scrollHeight -
(scrollRef.current.scrollTop + scrollRef.current.clientHeight),
) <= 1
: false;
const initScrolled = useRef(false);
// for auto-scroll
const [autoScroll, setAutoScroll] = useState(true);
function scrollDomToBottom() {
const dom = scrollRef.current;
@@ -19,10 +25,11 @@ export default function useScrollToBottom(
// auto scroll
useEffect(() => {
if (autoScroll && !detach) {
if (autoScroll && !detach && !initScrolled.current) {
scrollDomToBottom();
initScrolled.current = true;
}
});
}, [autoScroll, detach]);
return {
scrollRef,