Commit 704a4770 by Liu

feat: 调整首页工具逻辑

parent 934534e1
......@@ -5,12 +5,11 @@ import { Outlet, useLocation } from 'react-router-dom'
import { useLocalStorageState } from 'ahooks'
import styles from './Home.module.less'
import { QuestionList } from './components/QuestionList'
import HomeIcon1 from '@/assets/homeIcon1.png'
import HomeIcon2 from '@/assets/homeIcon2.png'
import SmartIce from '@/assets/smart-ice.png'
import { clearCurrentToolId, createConversation, fetchConversations, setCurrentToolId } from '@/store/conversationSlice'
import { useAppDispatch } from '@/store/hook'
import { fetchEfficiencyQuestionList, fetchQuestionList } from '@/api/home'
import { fetchEfficiencyQuestionList } from '@/api/home'
import SdreamLoading from '@/components/SdreamLoading'
import { fetchLoginByToken, fetchLoginByUid } from '@/api/common'
......@@ -42,56 +41,22 @@ function getAnimationProps(delay: number) {
export const Home: React.FC = () => {
const viteOutputObj = import.meta.env.VITE_OUTPUT_OBJ || 'open'
const [isLoading, setIsLoading] = useState(false)
const [isLoading, _setIsLoading] = useState(false)
const [isDataLoaded, setIsDataLoaded] = useState(false)
const dispatch = useAppDispatch()
const location = useLocation()
const hasFetched = useRef(false)
// 使用 useState
const [productQuestions, setProductQuestions] = useState<any>({ content: [] })
const [otherQuestions, setOtherQuestions] = useState<any>({ content: [] })
const [isToolBtnActive, setIsToolBtnActive] = useState<boolean>(false)
const [shouldChangeStyle, setShouldChangeStyle] = useState<boolean>(false)
const [shouldChangeStyle, setShouldChangeStyle] = useState<boolean>(true)
// 保存原始的configType为07的数据
const [originalOtherQuestions, setOriginalOtherQuestions] = useState<any>({ content: [] })
const [originalOtherQuestions, _setOriginalOtherQuestions] = useState<any>({ content: [] })
const [token, setToken] = useLocalStorageState<string | undefined>('__TOKEN__', {
defaultValue: '',
})
/** 获取qa记录 */
const getQuestionList = useCallback(async () => {
setIsLoading(true)
try {
const param = {
configTypeList: ['06', '07'],
}
const res = await fetchQuestionList(param)
if (res && res.data) {
for (let index = 0; index < res.data.length; index++) {
const element = res.data[index]
if (element.configType === '06') {
element.content = JSON.parse(element.content)
setProductQuestions(element)
}
if (element.configType === '07') {
element.content = JSON.parse(element.content)
setOtherQuestions(element)
setOriginalOtherQuestions(element) // 保存原始数据
}
}
}
}
catch (error) {
// 可以在这里添加错误处理逻辑
console.error('Failed to fetch chat records:', error)
}
finally {
setIsLoading(false)
setIsDataLoaded(true)
}
}, [])
const initConversation = () => {
const fromCollect = location.state?.fromCollect
// 只有在访问首页时才创建新对话,如果已经在聊天页面则不创建
......@@ -113,7 +78,7 @@ finally {
// 处理工具按钮点击
const _handleToolClick = useCallback(async (isToolBtn: boolean, toolId?: string) => {
if (!isToolBtn && toolId) {
// if (!isToolBtn && toolId) {
// 提质增效模式:只修改左侧页面内容,加载工具相关问题
// 先清空数据,确保显示空数据样式
setOtherQuestions((prev: any) => ({
......@@ -136,31 +101,19 @@ finally {
finally {
setIsDataLoaded(true) // 无论成功失败都标记为已加载
}
}
else if (isToolBtn) {
// 通用模式:恢复刷新时的状态,包括左侧内容也要恢复到初始状态
setOtherQuestions(originalOtherQuestions)
setIsDataLoaded(true) // 恢复原始数据时标记为已加载
}
// }
// else if (isToolBtn) {
// setIsDataLoaded(true) // 恢复原始数据时标记为已加载
// }
}, [originalOtherQuestions])
// 监听工具按钮点击事件
useEffect(() => {
const handleToolClickEvent = (event: CustomEvent) => {
const { isToolBtn, toolId, shouldChangeStyle: shouldChangeStyleParam } = event.detail
// eslint-disable-next-line no-console
console.log('🔧 [Home] 工具按钮点击事件触发:', {
isToolBtn,
toolId,
shouldChangeStyle: shouldChangeStyleParam,
当前isToolBtnActive: isToolBtnActive,
当前shouldChangeStyle: shouldChangeStyle,
})
const { isToolBtn, toolId } = event.detail
setIsToolBtnActive(isToolBtn)
// 更新样式控制状态
if (shouldChangeStyleParam !== undefined) {
setShouldChangeStyle(shouldChangeStyleParam)
}
setShouldChangeStyle(true)
// 保存当前选择的 toolId 到 Redux
if (!isToolBtn && toolId) {
......@@ -210,13 +163,11 @@ finally {
storageArea: localStorage,
}),
)
await getQuestionList()
initConversation()
dispatch(fetchConversations())
}
}
else {
await getQuestionList()
initConversation()
dispatch(fetchConversations())
}
......@@ -236,7 +187,6 @@ finally {
storageArea: localStorage,
}),
)
await getQuestionList()
initConversation()
dispatch(fetchConversations())
}
......@@ -246,6 +196,7 @@ finally {
// 修改 useEffect
useEffect(() => {
login()
_handleToolClick(false, '')
}, []) // 依赖数组为空,只在组件挂载时执行一次
return (
......@@ -265,7 +216,7 @@ finally {
className="flex flex-col gap-[20px] items-center overflow-y-auto scrollbar-hide"
style={{ height: shouldChangeStyle ? 'calc(-64px + 100vh)' : '500px', background: shouldChangeStyle ? 'linear-gradient(180deg, #F0F8FF 0%, #FFFFFF 50%, #FFFFFF 100%)' : '', borderRadius: '24px' }}
>
{!shouldChangeStyle && (
{/* {!shouldChangeStyle && (
<motion.div className="w-full sm:w-auto" {...getAnimationProps(2)}>
<QuestionList
questions={productQuestions.content}
......@@ -277,7 +228,7 @@ finally {
isLoaded={isDataLoaded}
/>
</motion.div>
)}
)} */}
<motion.div className="w-full sm:w-auto" {...getAnimationProps(3)}>
<QuestionList
questions={otherQuestions.content}
......
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