Commit af45edef by weiw

fix:收藏页面增加返回功能

parent 9807cfe4
...@@ -59,6 +59,18 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c ...@@ -59,6 +59,18 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
} }
if (type === 'collect') { if (type === 'collect') {
// 从URL中提取会话ID并存储到 sessionStorage
const pathParts = location.pathname.split('/')
// 从路径中提取会话ID,获取 chat/ 后面的值
// 查找 'chat' 在路径中的位置,然后获取其后一项作为会话ID
const chatIndex = pathParts.indexOf('chat')
const conversationId = chatIndex !== -1 && pathParts.length > chatIndex + 1
? pathParts[chatIndex + 1]
: null
if (conversationId) {
sessionStorage.setItem('currentConversationId', conversationId)
}
navigate('/collect') navigate('/collect')
} }
......
import type React from 'react' import type React from 'react'
import { motion } from 'framer-motion' import { motion } from 'framer-motion'
import { Outlet } from 'react-router-dom' import { Outlet, useLocation } from 'react-router-dom'
import { useEffect } from 'react' import { useEffect } from 'react'
import styles from './Home.module.less' import styles from './Home.module.less'
import { QuestionList } from './components/QuestionList' import { QuestionList } from './components/QuestionList'
...@@ -38,16 +38,27 @@ function getAnimationProps(delay: number) { ...@@ -38,16 +38,27 @@ function getAnimationProps(delay: number) {
export const Home: React.FC = () => { export const Home: React.FC = () => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const location = useLocation()
useEffect(() => { useEffect(() => {
// 在组件挂载时执行dispatch const fromCollect = location.state?.fromCollect
dispatch( // 在组件挂载时执行dispatch,但只执行一次
createConversation({ if (!fromCollect) {
conversationData: {}, dispatch(
shouldNavigate: true, createConversation({
shouldSendQuestion: '', conversationData: {},
}), shouldNavigate: true,
) shouldSendQuestion: '',
}, []) // 空依赖数组确保只在组件挂载时执行一次 }),
)
}
// 清除状态以避免下次影响
if (location.state?.fromCollect) {
// 使用 replace 替换当前历史记录,清除 state
window.history.replaceState({}, document.title, window.location.pathname)
}
}, [])
// const location = useLocation() // const location = useLocation()
// 检查当前路径是否包含 "/chat/" // 检查当前路径是否包含 "/chat/"
......
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