Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sdream-ai-fe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
侯明涛
sdream-ai-fe
Commits
584fe21f
Commit
584fe21f
authored
Dec 23, 2025
by
Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:新建会话时机
parent
fb2b017f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
13 deletions
+52
-13
src/pages/Home/HomeNew.tsx
+41
-11
src/routes/RouteChangeHandler.tsx
+7
-1
src/store/conversationSlice.ts
+4
-1
No files found.
src/pages/Home/HomeNew.tsx
View file @
584fe21f
...
...
@@ -7,12 +7,13 @@ import styles from './Home.module.less'
import
{
QuestionList
}
from
'./components/QuestionList'
import
HomeIcon2
from
'@/assets/homeIcon2.png'
import
SmartIce
from
'@/assets/smart-ice.png'
import
{
clearCurrentToolId
,
createConversation
,
fetchConversations
,
setCurrent
ToolId
}
from
'@/store/conversationSlice'
import
{
clearCurrentToolId
,
createConversation
,
fetchConversations
,
setCurrent
Conversation
,
setCurrentToolId
,
setNavigationFlag
}
from
'@/store/conversationSlice'
import
{
useAppDispatch
}
from
'@/store/hook'
import
{
fetchEfficiencyQuestionList
}
from
'@/api/home'
import
SdreamLoading
from
'@/components/SdreamLoading'
import
{
fetchLoginByToken
,
fetchLoginByUid
}
from
'@/api/common'
import
{
getUserRolesFromRoute
,
safeSessionStorageGetItem
,
safeSessionStorageRemoveItem
}
from
'@/lib/utils'
import
{
getUserRolesFromRoute
,
safeLocalStorageGetItem
,
safeSessionStorageGetItem
,
safeSessionStorageRemoveItem
,
safeSessionStorageSetItem
}
from
'@/lib/utils'
import
type
{
Conversation
}
from
'@/types/conversation'
function
getAnimationProps
(
delay
:
number
)
{
return
{
...
...
@@ -58,17 +59,46 @@ export const Home: React.FC = () => {
defaultValue
:
''
,
})
const
initConversation
=
()
=>
{
const
initConversation
=
async
()
=>
{
const
fromCollect
=
location
.
state
?.
fromCollect
// 只有在访问首页时才
创建新对话,如果已经在聊天页面则不创建
// 只有在访问首页时才
处理会话,如果已经在聊天页面则不处理
if
(
!
fromCollect
&&
(
location
.
pathname
===
'/'
||
location
.
pathname
===
'/home'
))
{
dispatch
(
createConversation
({
conversationData
:
{},
shouldNavigate
:
true
,
shouldSendQuestion
:
''
,
}),
)
// 从 localStorage 读取上次的 conversationId
const
savedConversationId
=
safeLocalStorageGetItem
(
'currentConversationId'
)
if
(
savedConversationId
)
{
// 如果 localStorage 中有 conversationId,恢复它而不是创建新会话
dispatch
(
setCurrentConversation
(
savedConversationId
))
// 设置导航标志,触发自动导航到聊天页面
dispatch
(
setNavigationFlag
(
true
))
// 获取会话列表(用于后续验证和同步 toolId)
const
fetchResult
=
await
dispatch
(
fetchConversations
())
const
conversationsList
=
fetchResult
.
payload
as
Conversation
[]
// 同步会话的 toolId(如果存在)
const
restoredConversation
=
conversationsList
?.
find
((
conv
:
Conversation
)
=>
conv
.
conversationId
===
savedConversationId
)
if
(
restoredConversation
?.
toolId
)
{
dispatch
(
setCurrentToolId
(
restoredConversation
.
toolId
))
// 同步到 sessionStorage,确保 ChatEditor 等组件能正确识别 toolId
safeSessionStorageSetItem
(
'currentToolId'
,
restoredConversation
.
toolId
)
}
else
{
dispatch
(
clearCurrentToolId
())
// 清除 sessionStorage 中的 toolId
safeSessionStorageRemoveItem
(
'currentToolId'
)
}
}
else
{
// 如果 localStorage 中没有 conversationId,创建新会话
dispatch
(
fetchConversations
())
dispatch
(
createConversation
({
conversationData
:
{},
shouldNavigate
:
true
,
shouldSendQuestion
:
''
,
}),
)
}
}
// 清除状态以避免下次影响
if
(
location
.
state
?.
fromCollect
)
{
...
...
src/routes/RouteChangeHandler.tsx
View file @
584fe21f
...
...
@@ -5,6 +5,7 @@ import { clearCurrentTacticsConversation, setCurrentTacticsConversation } from '
import
{
useAppDispatch
,
useAppSelector
}
from
'@/store/hook'
import
{
setIsAsking
}
from
'@/store/chatSlice'
import
type
{
RootState
}
from
'@/store'
import
{
safeLocalStorageGetItem
}
from
'@/lib/utils'
export
function
withRouteChangeHandler
(
WrappedComponent
:
React
.
ComponentType
)
{
let
beforeLocationPathName
=
''
...
...
@@ -35,7 +36,12 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) {
}
if
(
location
.
pathname
===
'/'
)
{
dispatch
(
clearCurrentConversation
())
// 如果 localStorage 中有 conversationId,不清除,让恢复逻辑处理
// 这样可以保留上一次的历史记录
const
savedConversationId
=
safeLocalStorageGetItem
(
'currentConversationId'
)
if
(
!
savedConversationId
)
{
dispatch
(
clearCurrentConversation
())
}
}
else
if
(
location
.
pathname
.
startsWith
(
'/chat/'
))
{
const
conversationId
=
location
.
pathname
.
split
(
'/'
)[
2
]
...
...
src/store/conversationSlice.ts
View file @
584fe21f
...
...
@@ -100,6 +100,9 @@ const conversationSlice = createSlice({
clearNavigationFlag
:
(
state
)
=>
{
state
.
shouldNavigateToNewConversation
=
false
},
setNavigationFlag
:
(
state
,
action
:
PayloadAction
<
boolean
>
)
=>
{
state
.
shouldNavigateToNewConversation
=
action
.
payload
},
setCurrentToolId
:
(
state
,
action
:
PayloadAction
<
string
|
undefined
>
)
=>
{
state
.
currentToolId
=
action
.
payload
},
...
...
@@ -151,6 +154,6 @@ const conversationSlice = createSlice({
},
})
export
const
{
setCurrentConversation
,
clearCurrentConversation
,
clearNavigationFlag
,
clearShouldSendQuestion
,
setShouldSendQuestion
,
setCurrentToolId
,
clearCurrentToolId
}
=
conversationSlice
.
actions
export
const
{
setCurrentConversation
,
clearCurrentConversation
,
clearNavigationFlag
,
setNavigationFlag
,
clearShouldSendQuestion
,
setShouldSendQuestion
,
setCurrentToolId
,
clearCurrentToolId
}
=
conversationSlice
.
actions
export
default
conversationSlice
.
reducer
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment