feat: infite scroll

This commit is contained in:
金伟强 2023-05-25 16:04:27 +08:00
parent 99f7e4f277
commit eca16888bd
3 changed files with 51 additions and 20 deletions

View File

@ -80,6 +80,11 @@ const Main: FC<IMainProps> = () => {
setNewConversationInfo,
setExistConversationInfo
} = useConversation()
const [hasMore, setHasMore] = useState<boolean>(false)
const onMoreLoaded = ({ data: conversations, has_more }: any) => {
setHasMore(has_more)
setConversationList([...conversationList, ...conversations])
}
const [suggestedQuestionsAfterAnswerConfig, setSuggestedQuestionsAfterAnswerConfig] = useState<SuggestedQuestionsAfterAnswerConfig | null>(null)
const [conversationIdChangeBecauseOfNew, setConversationIdChangeBecauseOfNew, getConversationIdChangeBecauseOfNew] = useGetState(false)
@ -236,10 +241,10 @@ const Main: FC<IMainProps> = () => {
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<IMainProps> = () => {
}
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<IMainProps> = () => {
return (
<Sidebar
list={conversationList}
onMoreLoaded={onMoreLoaded}
isNoMore={!hasMore}
onCurrentIdChange={handleConversationIdChange}
currentId={currConversationId}
copyRight={siteInfo.copyright || siteInfo.title}

View File

@ -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 {
@ -9,41 +9,64 @@ import { ChatBubbleOvalLeftEllipsisIcon as ChatBubbleOvalLeftEllipsisSolidIcon,
import Button from '../../../base/button'
// import Card from './card'
import type { ConversationItem } 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[]
onMoreLoaded: (res: {data: ConversationItem[], has_more: boolean}) => void
isNoMore: boolean
}
const Sidebar: FC<ISidebarProps> = ({
copyRight,
currentId,
onCurrentIdChange,
list }) => {
list,
onMoreLoaded,
isNoMore,
}) => {
const { t } = useTranslation()
const listRef = useRef<HTMLDivElement>(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 (
<div
className="shrink-0 flex flex-col overflow-y-auto bg-white pc:w-[244px] tablet:w-[192px] mobile:w-[240px] border-r border-gray-200 tablet:h-[calc(100vh_-_3rem)] mobile:h-screen"
className="shrink-0 flex flex-col bg-white pc:w-[244px] tablet:w-[192px] mobile:w-[240px] border-r border-gray-200 tablet:h-[calc(100vh_-_3rem)] mobile:h-screen"
>
{list.length < MAX_CONVERSATION_LENTH && (
<div className="flex flex-shrink-0 p-4 !pb-0">
<Button
onClick={() => { onCurrentIdChange('-1') }}
className="group block w-full flex-shrink-0 !justify-start !h-9 text-primary-600 items-center text-sm">
<PencilSquareIcon className="mr-2 h-4 w-4" /> {t('share.chat.newChat')}
</Button>
</div>
)}
<div className="flex flex-shrink-0 p-4 !pb-0">
<Button
onClick={() => { onCurrentIdChange('-1') }}
className="group block w-full flex-shrink-0 !justify-start !h-9 text-primary-600 items-center text-sm">
<PencilSquareIcon className="mr-2 h-4 w-4" /> {t('share.chat.newChat')}
</Button>
</div>
<nav className="mt-4 flex-1 space-y-1 bg-white p-4 !pt-0">
<nav
ref={listRef}
className="mt-4 flex-1 space-y-1 bg-white p-4 !pt-0 overflow-y-auto"
>
{list.map((item) => {
const isCurrent = item.id === currentId
const ItemIcon

View File

@ -33,8 +33,8 @@ export const fetchAppInfo = async () => {
return get('/site')
}
export const fetchConversations = async () => {
return get('conversations', { params: { limit: 20, first_id: '' } })
export const fetchConversations = async (last_id?: string) => {
return get('conversations', { params: {...{ limit: 20 }, ...(last_id ? { last_id } : {}) } })
}
export const fetchChatList = async (conversationId: string) => {