Load new messages when reaching top (#57)
This commit is contained in:
parent
c52a7963d2
commit
771392c960
|
@ -115,6 +115,7 @@ export function ChatBody({
|
||||||
messenger ? (
|
messenger ? (
|
||||||
<ChatMessages
|
<ChatMessages
|
||||||
messages={messages}
|
messages={messages}
|
||||||
|
loadNextDay={loadNextDay}
|
||||||
fetchMetadata={fetchMetadata}
|
fetchMetadata={fetchMetadata}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
|
|
|
@ -10,11 +10,16 @@ import { ChatMessageContent } from "./ChatMessageContent";
|
||||||
|
|
||||||
type ChatMessagesProps = {
|
type ChatMessagesProps = {
|
||||||
messages: ChatMessage[];
|
messages: ChatMessage[];
|
||||||
|
loadNextDay: () => void;
|
||||||
fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
|
fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function ChatMessages({ messages, fetchMetadata }: ChatMessagesProps) {
|
export function ChatMessages({
|
||||||
const [scrollOnBot, setScrollOnBot] = useState(false);
|
messages,
|
||||||
|
loadNextDay,
|
||||||
|
fetchMetadata,
|
||||||
|
}: ChatMessagesProps) {
|
||||||
|
const [scrollOnBot, setScrollOnBot] = useState(true);
|
||||||
const ref = useRef<HTMLHeadingElement>(null);
|
const ref = useRef<HTMLHeadingElement>(null);
|
||||||
const today = useMemo(() => new Date().getDay(), []);
|
const today = useMemo(() => new Date().getDay(), []);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -26,6 +31,9 @@ export function ChatMessages({ messages, fetchMetadata }: ChatMessagesProps) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const setScroll = () => {
|
const setScroll = () => {
|
||||||
if (ref && ref.current) {
|
if (ref && ref.current) {
|
||||||
|
if (ref.current.scrollTop <= 0) {
|
||||||
|
loadNextDay();
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
ref.current.scrollTop + ref.current.clientHeight ==
|
ref.current.scrollTop + ref.current.clientHeight ==
|
||||||
ref.current.scrollHeight
|
ref.current.scrollHeight
|
||||||
|
|
Loading…
Reference in New Issue