Commit af79aca0 by Liu

fix:常见问题换一换时更新

parent 35fc01e1
......@@ -77,6 +77,42 @@ export const Home: React.FC = () => {
}
}
// 刷新问题列表
const handleRefreshQuestions = useCallback(async () => {
// 先清空旧数据
setOtherQuestions((prev: any) => ({
...prev,
content: [],
}))
setIsDataLoaded(false) // 重置加载状态
try {
// 获取当前的 toolId,优先从 sessionStorage 获取,其次从 Redux 获取
const sessionToolId = safeSessionStorageGetItem('currentToolId') || ''
const searchParams = new URLSearchParams(location.search)
const urlToolId = searchParams.get('toolId') || ''
const finalToolId = sessionToolId || urlToolId
// 调用接口重新获取问题列表
const res = await fetchEfficiencyQuestionList({
toolId: finalToolId,
})
if (res && res.data && res.data.questions) {
setOtherQuestions((prev: any) => ({
...prev,
content: res.data.questions || [],
}))
}
}
catch (error) {
console.error('刷新问题列表失败:', error)
throw error // 抛出错误,让 QuestionList 组件处理
}
finally {
setIsDataLoaded(true) // 无论成功失败都标记为已加载
}
}, [location.search])
// 处理工具按钮点击
const _handleToolClick = useCallback(async (isToolBtn: boolean, toolId?: string, ignoreUrlToolId?: boolean) => {
// 提质增效模式 / 数据助手 / 通用模式:都先清空数据,重新拉常见问题
......@@ -286,6 +322,7 @@ export const Home: React.FC = () => {
iconImg={HomeIcon2}
isToolBtn={shouldChangeStyle}
isLoaded={isDataLoaded}
onRefresh={handleRefreshQuestions}
/>
</motion.div>
{shouldChangeStyle && (
......
......@@ -6,7 +6,6 @@ import { useCallback, useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import Refresh from '@/assets/svg/refresh.svg?react'
import { type WithAuthProps, withAuth } from '@/auth/withAuth'
import { fetchEfficiencyQuestionList } from '@/api/home'
import { useAppDispatch, useAppSelector } from '@/store/hook'
import { createConversation, setCurrentConversation, setShouldSendQuestion } from '@/store/conversationSlice'
import { safeSessionStorageGetItem } from '@/lib/utils'
......@@ -23,6 +22,7 @@ interface QuestionListProps {
height?: string
isToolBtn?: boolean
isLoaded?: boolean
onRefresh?: () => Promise<void>
}
const containerVariants = {
hidden: {},
......@@ -70,11 +70,11 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
displayCount: _displayCount = 6,
isToolBtn = false,
isLoaded = false,
onRefresh,
}) => {
const [isRotating, setIsRotating] = useState(false)
const [displayedItems, setDisplayedItems] = useState<string[]>([])
const [isClicking, setIsClicking] = useState(false)
const [fetchedQuestions, setFetchedQuestions] = useState<string[] | null>(null)
const dispatch = useAppDispatch()
const navigate = useNavigate()
const { currentConversationId, conversations, currentToolId } = useAppSelector(state => state.conversation)
......@@ -82,36 +82,25 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
// 根据 isToolBtn 动态设置 displayCount
const actualDisplayCount = isToolBtn ? 6 : 4
const updateDisplayedItems = useCallback(
(customQuestions?: string[]) => {
const sourceQuestions = (customQuestions && customQuestions.length > 0)
? customQuestions
: (fetchedQuestions && fetchedQuestions.length > 0 ? fetchedQuestions : questions)
if (!sourceQuestions.length) {
return
}
const indices = getRandomIndices(sourceQuestions.length, Math.min(actualDisplayCount, sourceQuestions.length))
setDisplayedItems(indices.map(index => sourceQuestions[index]))
},
[questions, fetchedQuestions, actualDisplayCount],
)
const updateDisplayedItems = useCallback(() => {
const indices = getRandomIndices(questions.length, Math.min(actualDisplayCount, questions.length))
setDisplayedItems(indices.map(index => questions[index]))
}, [questions, actualDisplayCount])
const handleRefresh = async () => {
// 如果没有提供 onRefresh 回调,则不执行任何操作
if (!onRefresh) {
return
}
setIsRotating(true)
const toolId = safeSessionStorageGetItem('currentToolId') || ''
try {
const res = await fetchEfficiencyQuestionList({ toolId })
const newQuestions = res?.data?.questions as string[] | undefined
if (newQuestions?.length) {
setFetchedQuestions(newQuestions)
updateDisplayedItems(newQuestions)
return
}
updateDisplayedItems()
// 重新调用接口获取新问题
await onRefresh()
}
catch (error) {
console.error('刷新常见问题失败:', error)
updateDisplayedItems()
console.error('刷新问题列表失败:', error)
// 接口调用失败时不回退到随机选择,保持当前状态
}
finally {
setIsRotating(false)
......@@ -158,6 +147,9 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
updateDisplayedItems()
}, [updateDisplayedItems])
// 当上游将 questions 清空时,立即清空展示项,避免短暂显示旧数据
const itemsToRender = questions.length === 0 ? [] : displayedItems
return (
<div
className="bg-white box-border px-[20px] py-[12px] w-full sm:w-[300px] md:w-[300px]"
......@@ -207,7 +199,7 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
animate="visible"
className="mt-[8px] flex flex-col gap-[8px] w-full"
>
{displayedItems.map((item, index) => (
{itemsToRender.map((item, index) => (
<motion.li
key={`question-${item}`}
custom={index}
......
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