Commit 552559f6 by Liu

fix:历史纪录到会话逻辑

parent 0bdbb145
...@@ -46,7 +46,11 @@ export const HistoryBarList: React.FC<HistoryBarListProps> = ({ searchValue, onS ...@@ -46,7 +46,11 @@ export const HistoryBarList: React.FC<HistoryBarListProps> = ({ searchValue, onS
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
} }
// 直接导航到历史记录,不设置shouldSendQuestion // 直接导航到历史记录,不设置shouldSendQuestion
navigate(`/chat/${conversation.conversationId}`) navigate(`/chat/${conversation.conversationId}`, {
state: {
toolId: conversation.toolId || null,
},
})
} }
const handleFilter = useDebounceFn(() => { const handleFilter = useDebounceFn(() => {
......
import React, { useCallback, useEffect, useRef, useState } from 'react' import React, { useCallback, useEffect, useRef, useState } from 'react'
import { useParams } from 'react-router-dom' import { useLocation, useParams } from 'react-router-dom'
import { Button } from '@heroui/react' import { Button } from '@heroui/react'
import { motion } from 'framer-motion' import { motion } from 'framer-motion'
import { useScroll } from 'ahooks' import { useScroll } from 'ahooks'
...@@ -23,6 +23,8 @@ import SdreamLoading from '@/components/SdreamLoading' ...@@ -23,6 +23,8 @@ import SdreamLoading from '@/components/SdreamLoading'
export const Chat: React.FC = () => { export const Chat: React.FC = () => {
const { id } = useParams<{ id: string }>() const { id } = useParams<{ id: string }>()
const location = useLocation()
const initialToolId = (location.state as { toolId?: string | null } | null)?.toolId
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
const [allItems, setAllItems] = useState<ChatRecord[]>([]) const [allItems, setAllItems] = useState<ChatRecord[]>([])
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
...@@ -32,7 +34,19 @@ export const Chat: React.FC = () => { ...@@ -32,7 +34,19 @@ export const Chat: React.FC = () => {
const currentIdRef = useRef<string | undefined>(id) const currentIdRef = useRef<string | undefined>(id)
const lastSentQuestionRef = useRef<string>('') const lastSentQuestionRef = useRef<string>('')
const abortControllerRef = useRef<AbortController | null>(null) const abortControllerRef = useRef<AbortController | null>(null)
const [currentToolName, setCurrentToolName] = useState<string>('通用模式') const [currentToolName, setCurrentToolName] = useState<string | undefined>(undefined)
// 历史记录点击时将 toolId 通过路由 state 传入,优先使用该值快速同步高亮
useEffect(() => {
if (typeof initialToolId === 'undefined')
return
if (initialToolId) {
dispatch(setCurrentToolId(initialToolId))
}
else {
dispatch(clearCurrentToolId())
}
}, [dispatch, initialToolId])
/** 处理正常stream的数据 */ /** 处理正常stream的数据 */
const handleStreamMesageData = (msg: any, question: string) => { const handleStreamMesageData = (msg: any, question: string) => {
...@@ -330,23 +344,26 @@ export const Chat: React.FC = () => { ...@@ -330,23 +344,26 @@ export const Chat: React.FC = () => {
// 根据 currentToolId 获取对应的 toolName // 根据 currentToolId 获取对应的 toolName
useEffect(() => { useEffect(() => {
if (!currentToolId)
return
const getToolNameFromToolId = async () => { const getToolNameFromToolId = async () => {
try { if (currentToolId) {
// 使用统一的方法获取 userRoles(先同步路由到 localStorage,然后读取) try {
const userRoles = getUserRolesForApi() // 使用统一的方法获取 userRoles(先同步路由到 localStorage,然后读取)
const res = await fetchToolList({ userRoles }) const userRoles = getUserRolesForApi()
if (res?.data) { const res = await fetchToolList({ userRoles })
const tool = res.data.find((t: any) => t.toolId === currentToolId) if (res?.data) {
if (tool?.toolName) { const tool = res.data.find((t: any) => t.toolId === currentToolId)
setCurrentToolName(tool.toolName) if (tool?.toolName) {
setCurrentToolName(tool.toolName)
}
} }
} }
catch (error) {
console.error('获取工具列表失败:', error)
}
} }
catch (error) { else {
console.error('获取工具列表失败:', error) // 通用模式
setCurrentToolName('通用模式')
} }
} }
getToolNameFromToolId() getToolNameFromToolId()
......
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