import * as React from 'react'; import { useState, useEffect, useRef } from 'react'; import { Alert, Center, Container, Stack, px } from '@mantine/core'; import { ScrollArea } from '@mantine/core'; import { Button } from '@mantine/core'; import { useListState, useResizeObserver, useTimeout, useViewportSize } from '@mantine/hooks'; import { IconPlayerStop, IconRotateDot } from '@tabler/icons-react'; import messageUtil from '@/util/MessageUtil'; import { useAppDispatch, useAppSelector } from '@/views/hooks'; import CurrentMessage from "@/views/CurrentMessage"; import { reGenerating, stopGenerating, startResponsing, happendError, selectGenerating, selectErrorMessage, selectIsBottom, selectIsLastPage, onMessagesBottom, onMessagesTop, onMessagesMiddle, fetchHistoryMessages, } from './chatSlice'; import InputMessage from './InputMessage'; import MessageContainer from './MessageContainer'; const RegenerationButton = () => { const dispatch = useAppDispatch(); return (); }; const StopButton = () => { const dispatch = useAppDispatch(); return ( ); }; const chatPanel = () => { const dispatch = useAppDispatch(); const generating = useAppSelector(selectGenerating); const isBottom = useAppSelector(selectIsBottom); const isLastPage = useAppSelector(selectIsLastPage); const errorMessage = useAppSelector(selectErrorMessage); const [chatContainerRef, chatContainerRect] = useResizeObserver(); const scrollViewport = useRef(null); const { height, width } = useViewportSize(); const scrollToBottom = () => scrollViewport?.current?.scrollTo({ top: scrollViewport.current.scrollHeight, behavior: 'smooth' }); const timer = useTimeout(() => { if (isBottom) { scrollToBottom(); } }, 1000); const onScrollPositionChange = ({ x, y }) => { const sh = scrollViewport.current?.scrollHeight || 0; const vh = scrollViewport.current?.clientHeight || 0; const gap = sh - vh - y; const isBottom = sh < vh ? true : gap < 100; const isTop = y === 0; // console.log(`sh:${sh},vh:${vh},x:${x},y:${y},gap:${gap}`); if (isBottom) { dispatch(onMessagesBottom()); } else if (isTop) { dispatch(onMessagesTop()); if (!isLastPage) { //TODO: Data loading flickers and has poor performance, so I temporarily disabled the loading logic. // dispatch(fetchHistoryMessages({ pageIndex: pageIndex + 1 })); } } else { dispatch(onMessagesMiddle()); } }; useEffect(() => { dispatch(fetchHistoryMessages({ pageIndex: 0 })); messageUtil.registerHandler('receiveMessagePartial', (message: { text: string; }) => { dispatch(startResponsing(message.text)); timer.start(); }); messageUtil.registerHandler('receiveMessage', (message: { text: string; isError: boolean, hash }) => { dispatch(stopGenerating({ hasDone: true, message: message })); if (message.isError) { dispatch(happendError(message.text)); } }); timer.start(); return () => { timer.clear(); }; }, []); return ( {errorMessage && {errorMessage} } {generating &&
} {errorMessage &&
}
); }; export default chatPanel;