From eca16888bdc6d7bb7335c5a0b734fde3ea64fc57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E4=BC=9F=E5=BC=BA?= Date: Thu, 25 May 2023 16:04:27 +0800 Subject: [PATCH] feat: infite scroll --- web/app/components/share/chat/index.tsx | 14 +++-- .../components/share/chat/sidebar/index.tsx | 53 +++++++++++++------ web/service/share.ts | 4 +- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/web/app/components/share/chat/index.tsx b/web/app/components/share/chat/index.tsx index f3ce070f38..96baa73fa2 100644 --- a/web/app/components/share/chat/index.tsx +++ b/web/app/components/share/chat/index.tsx @@ -80,6 +80,11 @@ const Main: FC = () => { setNewConversationInfo, setExistConversationInfo } = useConversation() + const [hasMore, setHasMore] = useState(false) + const onMoreLoaded = ({ data: conversations, has_more }: any) => { + setHasMore(has_more) + setConversationList([...conversationList, ...conversations]) + } const [suggestedQuestionsAfterAnswerConfig, setSuggestedQuestionsAfterAnswerConfig] = useState(null) const [conversationIdChangeBecauseOfNew, setConversationIdChangeBecauseOfNew, getConversationIdChangeBecauseOfNew] = useGetState(false) @@ -236,10 +241,10 @@ const Main: FC = () => { const prompt_template = tempIsPublicVersion ? model_config.pre_prompt : '' // handle current conversation id - const { data: conversations } = conversationData as { data: ConversationItem[] } + const { data: conversations, has_more } = conversationData as { data: ConversationItem[], has_more: boolean } const _conversationId = getConversationIdFromStorage(appId) const isNotNewConversation = conversations.some(item => item.id === _conversationId) - + setHasMore(has_more) // fetch new conversation info const { user_input_form, opening_statement: introduction, suggested_questions_after_answer }: any = appParams const prompt_variables = userInputsFormToPromptVariables(user_input_form) @@ -379,7 +384,8 @@ const Main: FC = () => { } let currChatList = conversationList if (getConversationIdChangeBecauseOfNew()) { - const { data: conversations }: any = await fetchConversations() + const { data: conversations, has_more }: any = await fetchConversations() + setHasMore(has_more) setConversationList(conversations as ConversationItem[]) currChatList = conversations } @@ -424,6 +430,8 @@ const Main: FC = () => { return ( void list: ConversationItem[] + onMoreLoaded: (res: {data: ConversationItem[], has_more: boolean}) => void + isNoMore: boolean } const Sidebar: FC = ({ copyRight, currentId, onCurrentIdChange, - list }) => { + list, + onMoreLoaded, + isNoMore, +}) => { const { t } = useTranslation() + const listRef = useRef(null) + + useInfiniteScroll( + async () => { + if(!isNoMore) { + const lastId = list[list.length - 1].id + const { data: conversations, has_more }: any = await fetchConversations(lastId) + onMoreLoaded({ data: conversations, has_more }) + } + return {list: []} + }, + { + target: listRef, + isNoMore: () => isNoMore, + }, + ) + return (
- {list.length < MAX_CONVERSATION_LENTH && ( -
- -
- )} +
+ +
-