Commit 12db6782 by HoMeTown

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

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