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
59926472
Commit
59926472
authored
Aug 09, 2024
by
HoMeTown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 添加路由守卫
parent
30e06255
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
13 deletions
+37
-13
src/pages/Chat/Chat.tsx
+9
-11
src/routes/AppRoutes.tsx
+4
-1
src/routes/RouteChangeHandler.tsx
+20
-0
src/store/conversationSlice.ts
+4
-1
No files found.
src/pages/Chat/Chat.tsx
View file @
59926472
import
React
,
{
useEffect
}
from
'react'
import
React
from
'react'
import
{
useParams
}
from
'react-router-dom'
import
{
useParams
}
from
'react-router-dom'
import
{
useAppDispatch
,
useAppSelector
}
from
'@/store/hook'
import
{
setCurrentConversation
}
from
'@/store/conversationSlice'
export
const
Chat
:
React
.
FC
=
()
=>
{
export
const
Chat
:
React
.
FC
=
()
=>
{
const
{
id
}
=
useParams
<
{
id
:
string
}
>
()
const
{
id
}
=
useParams
<
{
id
:
string
}
>
()
const
dispatch
=
useAppDispatch
()
//
const dispatch = useAppDispatch()
const
{
currentConversationId
}
=
useAppSelector
(
state
=>
state
.
conversation
)
//
const { currentConversationId } = useAppSelector(state => state.conversation)
useEffect
(()
=>
{
//
useEffect(() => {
if
(
id
&&
id
!==
currentConversationId
)
{
//
if (id && id !== currentConversationId) {
dispatch
(
setCurrentConversation
(
id
))
//
dispatch(setCurrentConversation(id))
// dispatch(fetchConversationDetails(id))
//
// dispatch(fetchConversationDetails(id))
}
//
}
},
[
id
,
currentConversationId
,
dispatch
])
//
}, [id, currentConversationId, dispatch])
return
(
return
(
<
h1
>
<
h1
>
聊天页面 - Chat ID:
聊天页面 - Chat ID:
...
...
src/routes/AppRoutes.tsx
View file @
59926472
...
@@ -2,8 +2,9 @@ import React from 'react'
...
@@ -2,8 +2,9 @@ import React from 'react'
import
{
Route
,
Routes
}
from
'react-router-dom'
import
{
Route
,
Routes
}
from
'react-router-dom'
import
{
Home
}
from
'../pages/Home'
import
{
Home
}
from
'../pages/Home'
import
{
Chat
}
from
'../pages/Chat'
import
{
Chat
}
from
'../pages/Chat'
import
{
withRouteChangeHandler
}
from
'./RouteChangeHandler'
export
const
AppRoutes
:
React
.
FC
=
()
=>
{
const
AppRoutesComponent
:
React
.
FC
=
()
=>
{
return
(
return
(
<
Routes
>
<
Routes
>
<
Route
path=
"/"
element=
{
<
Home
/>
}
/>
<
Route
path=
"/"
element=
{
<
Home
/>
}
/>
...
@@ -11,3 +12,5 @@ export const AppRoutes: React.FC = () => {
...
@@ -11,3 +12,5 @@ export const AppRoutes: React.FC = () => {
</
Routes
>
</
Routes
>
)
)
}
}
export
const
AppRoutes
=
withRouteChangeHandler
(
AppRoutesComponent
)
src/routes/RouteChangeHandler.tsx
0 → 100644
View file @
59926472
import
React
,
{
useEffect
}
from
'react'
import
{
useLocation
}
from
'react-router-dom'
import
{
clearCurrentConversation
}
from
'@/store/conversationSlice'
import
{
useAppDispatch
}
from
'@/store/hook'
export
function
withRouteChangeHandler
(
WrappedComponent
:
React
.
ComponentType
)
{
return
(
props
:
any
)
=>
{
const
location
=
useLocation
()
const
dispatch
=
useAppDispatch
()
useEffect
(()
=>
{
if
(
location
.
pathname
===
'/'
)
{
dispatch
(
clearCurrentConversation
())
}
// 这里可以添加其他路由相关的逻辑
},
[
location
,
dispatch
])
return
<
WrappedComponent
{
...
props
}
/>
}
}
src/store/conversationSlice.ts
View file @
59926472
...
@@ -54,6 +54,9 @@ const conversationSlice = createSlice({
...
@@ -54,6 +54,9 @@ const conversationSlice = createSlice({
setCurrentConversation
:
(
state
,
action
:
PayloadAction
<
string
>
)
=>
{
setCurrentConversation
:
(
state
,
action
:
PayloadAction
<
string
>
)
=>
{
state
.
currentConversationId
=
action
.
payload
state
.
currentConversationId
=
action
.
payload
},
},
clearCurrentConversation
:
(
state
)
=>
{
state
.
currentConversationId
=
null
},
addConversation
:
(
state
,
action
:
PayloadAction
<
Conversation
>
)
=>
{
addConversation
:
(
state
,
action
:
PayloadAction
<
Conversation
>
)
=>
{
state
.
conversations
.
unshift
(
action
.
payload
)
state
.
conversations
.
unshift
(
action
.
payload
)
},
},
...
@@ -97,6 +100,6 @@ const conversationSlice = createSlice({
...
@@ -97,6 +100,6 @@ const conversationSlice = createSlice({
},
},
})
})
export
const
{
setCurrentConversation
,
clearNavigationFlag
}
=
conversationSlice
.
actions
export
const
{
setCurrentConversation
,
clear
CurrentConversation
,
clear
NavigationFlag
}
=
conversationSlice
.
actions
export
default
conversationSlice
.
reducer
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