diff --git a/web/app/components/share/chat/index.tsx b/web/app/components/share/chat/index.tsx index ca0e6a2520..8e5db6d850 100644 --- a/web/app/components/share/chat/index.tsx +++ b/web/app/components/share/chat/index.tsx @@ -83,6 +83,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) @@ -250,10 +255,10 @@ const Main: FC = ({ setIsPublicVersion(tempIsPublicVersion) const prompt_template = '' // 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) @@ -395,7 +400,8 @@ const Main: FC = ({ } let currChatList = conversationList if (getConversationIdChangeBecauseOfNew()) { - const { data: conversations }: any = await fetchConversations(isInstalledApp, installedAppInfo?.id) + const { data: conversations, has_more }: any = await fetchConversations(isInstalledApp, installedAppInfo?.id) + setHasMore(has_more) setConversationList(conversations as ConversationItem[]) currChatList = conversations } @@ -440,10 +446,13 @@ const Main: FC = ({ return ( ) diff --git a/web/app/components/share/chat/sidebar/index.tsx b/web/app/components/share/chat/sidebar/index.tsx index d1515d5314..51a6bb7493 100644 --- a/web/app/components/share/chat/sidebar/index.tsx +++ b/web/app/components/share/chat/sidebar/index.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useEffect, useRef } from 'react' import type { FC } from 'react' import { useTranslation } from 'react-i18next' import { @@ -10,20 +10,23 @@ import Button from '../../../base/button' import AppInfo from '@/app/components/share/chat/sidebar/app-info' // import Card from './card' import type { ConversationItem, SiteInfo } from '@/models/share' +import { useInfiniteScroll } from 'ahooks' +import { fetchConversations } from '@/service/share' function classNames(...classes: any[]) { return classes.filter(Boolean).join(' ') } -const MAX_CONVERSATION_LENTH = 20 - export type ISidebarProps = { copyRight: string currentId: string onCurrentIdChange: (id: string) => void list: ConversationItem[] isInstalledApp: boolean + installedAppId?: string siteInfo: SiteInfo + onMoreLoaded: (res: {data: ConversationItem[], has_more: boolean}) => void + isNoMore: boolean } const Sidebar: FC = ({ @@ -32,15 +35,35 @@ const Sidebar: FC = ({ onCurrentIdChange, list, isInstalledApp, + installedAppId, siteInfo, + 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(isInstalledApp, installedAppId, lastId) + onMoreLoaded({ data: conversations, has_more }) + } + return {list: []} + }, + { + target: listRef, + isNoMore: () => isNoMore, + }, + ) + return (
@@ -52,17 +75,18 @@ const Sidebar: FC = ({ icon_background={siteInfo.icon_background} /> )} - {list.length < MAX_CONVERSATION_LENTH && ( -
- -
- )} +
+ +
-