Commit 12db6782 by HoMeTown

feat: 新建会话&会话名称更新

parent 662bca48
......@@ -25,7 +25,6 @@ export const Chat: React.FC = () => {
let ignore = false
const { id } = useParams<{ id: string }>()
const [isLoading, setIsLoading] = useState(false)
const [isNewConversation, setIsNewConversation] = useState(false)
const [allItems, setAllItems] = useState<ChatRecord[]>([])
const dispatch = useAppDispatch()
const shouldSendQuestion = useAppSelector((state: RootState) => state.conversation.shouldSendQuestion)
......@@ -33,6 +32,7 @@ export const Chat: React.FC = () => {
const position = useScroll(scrollableRef)
const handleSubmitQuestion = async (question: string) => {
const isNew = allItems.length === 0
dispatch(setIsAsking(true))
// 添加用户提问的问题
setAllItems(prevItems => [
......@@ -88,7 +88,7 @@ export const Chat: React.FC = () => {
})
}
if (msg.type === 'END') {
if (isNewConversation) {
if (isNew) {
setTimeout(() => {
dispatch(fetchConversations())
}, 2000)
......@@ -102,7 +102,6 @@ export const Chat: React.FC = () => {
setIsLoading(true)
try {
const res = await fetchUserQaRecordPage(conversationId)
setIsNewConversation(res.data.length === 0)
const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(res.data)]
setAllItems(messages) // 假设 API 返回的数据结构符合 ChatRecord[]
if (shouldSendQuestion) {
......
......@@ -5,6 +5,8 @@ import { AnimatePresence, motion } from 'framer-motion'
import { useCallback, useEffect, useState } from 'react'
import Refresh from '@/assets/svg/refresh.svg?react'
import { type WithAuthProps, withAuth } from '@/auth/withAuth'
import { useAppDispatch } from '@/store/hook'
import { createConversation } from '@/store/conversationSlice'
interface QuestionListProps {
title: string
......@@ -50,6 +52,7 @@ function getRandomIndices(total: number, count: number): number[] {
const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({ checkAuth, questions, dotColor, title, iconImg, showRefresh = true, displayCount = 4 }) => {
const [isRotating, setIsRotating] = useState(false)
const [displayedItems, setDisplayedItems] = useState<string[]>([])
const dispatch = useAppDispatch()
const updateDisplayedItems = useCallback(() => {
const indices = getRandomIndices(questions.length, Math.min(displayCount, questions.length))
......@@ -61,8 +64,14 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({ checkAu
updateDisplayedItems()
setIsRotating(false)
}
const handleClick = () => {
checkAuth()
const handleClick = (item: string) => {
if (checkAuth()) {
dispatch(createConversation({
conversationData: {},
shouldNavigate: true,
shouldSendQuestion: item,
}))
}
}
useEffect(() => {
......@@ -122,7 +131,7 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({ checkAu
layout
className="w-full"
>
<Button onClick={handleClick} color="primary" variant="light" className="text-left bg-[#F7FCFF] w-full text-[#333] rounded-[23px] data-[hover=true]:bg-[#E5F6FF] data-[hover=true]:text-primary">
<Button onClick={() => handleClick(item)} color="primary" variant="light" className="text-left bg-[#F7FCFF] w-full text-[#333] rounded-[23px] data-[hover=true]:bg-[#E5F6FF] data-[hover=true]:text-primary">
<div className="w-full text-nowrap text-ellipsis overflow-hidden">
<span style={{ color: dotColor }}>·</span>
<span className="ml-[8px]">{item}</span>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment