Commit 48cf7ad7 by weiw

fix:修复收藏页点击新建时的bug

parent 80f76e0f
...@@ -5,7 +5,7 @@ import { useNavigate } from 'react-router-dom' ...@@ -5,7 +5,7 @@ import { useNavigate } from 'react-router-dom'
import { useClickAway, useSessionStorageState } from 'ahooks' import { useClickAway, useSessionStorageState } from 'ahooks'
import styles from './Navbar.module.less' import styles from './Navbar.module.less'
import { NavBarItem } from './components/NavBarItem' import { NavBarItem } from './components/NavBarItem'
import { clearNavigationFlag } from '@/store/conversationSlice' import { clearNavigationFlag, createConversation } from '@/store/conversationSlice'
import type { WithAuthProps } from '@/auth/withAuth' import type { WithAuthProps } from '@/auth/withAuth'
import { withAuth } from '@/auth/withAuth' import { withAuth } from '@/auth/withAuth'
import { NAV_BAR_ITEMS } from '@/config/nav' import { NAV_BAR_ITEMS } from '@/config/nav'
...@@ -24,13 +24,13 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c ...@@ -24,13 +24,13 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
const { currentConversationId, shouldNavigateToNewConversation } = useAppSelector(state => state.conversation) const { currentConversationId, shouldNavigateToNewConversation } = useAppSelector(state => state.conversation)
// const handleCreateConversation = () => { const handleCreateConversation = () => {
// dispatch(createConversation({ dispatch(createConversation({
// conversationData: {}, conversationData: {},
// shouldNavigate: true, shouldNavigate: true,
// shouldSendQuestion: '', shouldSendQuestion: '',
// })) }))
// } }
const [isH5NavVisible, setIsH5NavVisible] = useState(isMobile()) const [isH5NavVisible, setIsH5NavVisible] = useState(isMobile())
...@@ -51,8 +51,12 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c ...@@ -51,8 +51,12 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
} }
if (type === 'add') { if (type === 'add') {
// handleCreateConversation() if (location.pathname.includes('/chat')) {
handleCreateConversation()
}
else {
navigate('/') navigate('/')
}
onSetHistoryVisible(false) onSetHistoryVisible(false)
} }
......
import { useEffect, useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { Button, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, Tooltip } from '@heroui/react' import { Button, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, Tooltip } from '@heroui/react'
import { motion } from 'framer-motion' import { motion } from 'framer-motion'
import { useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
...@@ -17,6 +17,7 @@ import SdreamLoading from '@/components/SdreamLoading' ...@@ -17,6 +17,7 @@ import SdreamLoading from '@/components/SdreamLoading'
export const Collect: React.FC = () => { export const Collect: React.FC = () => {
const navigate = useNavigate() const navigate = useNavigate()
const hasLoaded = useRef(false)
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
const [isOpen, setIsOpen] = useState(false) const [isOpen, setIsOpen] = useState(false)
...@@ -27,20 +28,31 @@ export const Collect: React.FC = () => { ...@@ -27,20 +28,31 @@ export const Collect: React.FC = () => {
const [total, setTotal] = useState(0) const [total, setTotal] = useState(0)
const getCollectList = async () => { const getCollectList = async () => {
// 防止重复调用
if (hasLoaded.current) {
return
}
hasLoaded.current = true
setIsLoading(true) setIsLoading(true)
const params = { const params = {
pageNum, pageNum,
pageSize, pageSize,
} }
try {
const res = await fetchQueryCollectionList(params) const res = await fetchQueryCollectionList(params)
let timer = null as any // 使用函数式更新确保获取最新的 collectList 值
timer = setTimeout(() => { setCollectList((prevList: any) => [...prevList, ...res.data.records])
setCollectList([...collectList, ...res.data.records])
setTotal(res.data.total) setTotal(res.data.total)
}
catch (error) {
console.error('获取收藏列表失败:', error)
hasLoaded.current = false // 出错时重置标志位
}
finally {
setIsLoading(false) setIsLoading(false)
clearTimeout(timer) }
timer = null
}, 500)
} }
const handleDelete = (item: any) => { const handleDelete = (item: any) => {
...@@ -79,7 +91,11 @@ export const Collect: React.FC = () => { ...@@ -79,7 +91,11 @@ export const Collect: React.FC = () => {
useEffect(() => { useEffect(() => {
getCollectList() getCollectList()
}, [pageNum]) // 清理函数,在组件卸载时重置标志位
return () => {
hasLoaded.current = false
}
}, [])
return ( return (
<div className={styles.scrollView}> <div className={styles.scrollView}>
<div className={`${styles.collectPage} relative`}> <div className={`${styles.collectPage} relative`}>
......
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