1- import { useCallback , useState } from 'react' ;
1+ import { useMemoizedFn } from 'ahooks' ;
2+ import { useState } from 'react' ;
23import type { RouteConfig } from '../../config/route-config.js' ;
34
45export type AppView =
@@ -50,7 +51,7 @@ export const useAppNavigation = (): UseAppNavigationReturn => {
5051 const [ navigationState , setNavigationState ] =
5152 useState < NavigationState > ( defaultNavigationState ) ;
5253
53- const navigate = useCallback ( ( view : AppView , options : NavigationOptions = { } ) => {
54+ const navigate = useMemoizedFn ( ( view : AppView , options : NavigationOptions = { } ) => {
5455 const { replace = false , preserveHistory = false } = options ;
5556
5657 setNavigationState ( ( prevState ) => {
@@ -80,9 +81,9 @@ export const useAppNavigation = (): UseAppNavigationReturn => {
8081 if ( routeConfig ?. onNavigate ) {
8182 routeConfig . onNavigate ( view , options ) ;
8283 }
83- } , [ ] ) ;
84+ } ) ;
8485
85- const goBack = useCallback ( ( ) => {
86+ const goBack = useMemoizedFn ( ( ) => {
8687 setNavigationState ( ( prevState ) => {
8788 if ( prevState . history . length <= 1 ) {
8889 return prevState ; // 无法返回
@@ -98,26 +99,26 @@ export const useAppNavigation = (): UseAppNavigationReturn => {
9899 canGoForward : true ,
99100 } ;
100101 } ) ;
101- } , [ ] ) ;
102+ } ) ;
102103
103- const goForward = useCallback ( ( ) => {
104+ const goForward = useMemoizedFn ( ( ) => {
104105 setNavigationState ( ( prevState ) => {
105106 // 这里简化处理,实际应该维护一个前进历史栈
106107 return prevState ; // 暂时无法前进
107108 } ) ;
108- } , [ ] ) ;
109+ } ) ;
109110
110- const goToHome = useCallback ( ( ) => {
111+ const goToHome = useMemoizedFn ( ( ) => {
111112 navigate ( 'main' , { replace : true } ) ;
112- } , [ navigate ] ) ;
113+ } ) ;
113114
114- const registerRoute = useCallback ( ( route : RouteConfig ) => {
115+ const registerRoute = useMemoizedFn ( ( route : RouteConfig ) => {
115116 routeRegistry . set ( route . path as AppView , route ) ;
116- } , [ ] ) ;
117+ } ) ;
117118
118- const getRouteConfig = useCallback ( ( view : AppView ) : RouteConfig | undefined => {
119+ const getRouteConfig = useMemoizedFn ( ( view : AppView ) : RouteConfig | undefined => {
119120 return routeRegistry . get ( view ) ;
120- } , [ ] ) ;
121+ } ) ;
121122
122123 return {
123124 currentView : navigationState . currentView ,
@@ -152,31 +153,28 @@ export const useNavigationHistory = () => {
152153export const useDeepLink = ( ) => {
153154 const navigate = useAppNavigation ( ) . navigate ;
154155
155- const handleDeepLink = useCallback (
156- ( url : string ) => {
157- try {
158- const parsedUrl = new URL ( url ) ;
159- const path = parsedUrl . pathname . slice ( 1 ) ; // 去掉开头的'/'
160- const params = Object . fromEntries ( parsedUrl . searchParams ) ;
161-
162- // 解析路径并导航
163- if ( [ 'settings' , 'help' , 'logs' , 'tools' , 'chat' , 'config' ] . includes ( path ) ) {
164- navigate ( path as AppView , { state : params } ) ;
165- } else {
166- // 处理特殊的深度链接格式
167- const [ view , ...rest ] = path . split ( '/' ) ;
168- if ( [ 'settings' , 'help' , 'logs' , 'tools' , 'chat' , 'config' ] . includes ( view ) ) {
169- navigate ( view as AppView , {
170- state : { section : rest . join ( '/' ) , ...params } ,
171- } ) ;
172- }
156+ const handleDeepLink = useMemoizedFn ( ( url : string ) => {
157+ try {
158+ const parsedUrl = new URL ( url ) ;
159+ const path = parsedUrl . pathname . slice ( 1 ) ; // 去掉开头的'/'
160+ const params = Object . fromEntries ( parsedUrl . searchParams ) ;
161+
162+ // 解析路径并导航
163+ if ( [ 'settings' , 'help' , 'logs' , 'tools' , 'chat' , 'config' ] . includes ( path ) ) {
164+ navigate ( path as AppView , { state : params } ) ;
165+ } else {
166+ // 处理特殊的深度链接格式
167+ const [ view , ...rest ] = path . split ( '/' ) ;
168+ if ( [ 'settings' , 'help' , 'logs' , 'tools' , 'chat' , 'config' ] . includes ( view ) ) {
169+ navigate ( view as AppView , {
170+ state : { section : rest . join ( '/' ) , ...params } ,
171+ } ) ;
173172 }
174- } catch ( error ) {
175- console . error ( '解析深度链接失败:' , error ) ;
176173 }
177- } ,
178- [ navigate ]
179- ) ;
174+ } catch ( error ) {
175+ console . error ( '解析深度链接失败:' , error ) ;
176+ }
177+ } ) ;
180178
181179 return { handleDeepLink } ;
182180} ;
@@ -185,7 +183,7 @@ export const useDeepLink = () => {
185183export const useRouteGuard = ( ) => {
186184 const navigate = useAppNavigation ( ) . navigate ;
187185
188- const requireAuth = useCallback ( ( targetView : AppView ) => {
186+ const requireAuth = useMemoizedFn ( ( targetView : AppView ) => {
189187 const isAuthenticated = false ; // 这里应该从认证状态获取
190188
191189 if ( ! isAuthenticated ) {
@@ -195,9 +193,9 @@ export const useRouteGuard = () => {
195193 }
196194
197195 return true ;
198- } , [ ] ) ;
196+ } ) ;
199197
200- const requirePermission = useCallback ( ( permission : string , targetView : AppView ) => {
198+ const requirePermission = useMemoizedFn ( ( permission : string , targetView : AppView ) => {
201199 const hasPermission = true ; // 这里应该从权限系统获取
202200
203201 if ( ! hasPermission ) {
@@ -206,9 +204,9 @@ export const useRouteGuard = () => {
206204 }
207205
208206 return true ;
209- } , [ ] ) ;
207+ } ) ;
210208
211- const navigateWithGuard = useCallback (
209+ const navigateWithGuard = useMemoizedFn (
212210 ( view : AppView , options ?: NavigationOptions ) => {
213211 const routeConfig = routeRegistry . get ( view ) ;
214212
@@ -223,8 +221,7 @@ export const useRouteGuard = () => {
223221 }
224222
225223 navigate ( view , options ) ;
226- } ,
227- [ navigate ]
224+ }
228225 ) ;
229226
230227 return {
0 commit comments