1- import { useSearchParams } from 'react-router-dom' ;
21import { startTransition } from 'react' ;
3- import { router } from '@/router' ;
2+ import { useRouter } from '@sa/simple-router' ;
3+ import type { RouteKey } from '@elegant-router/types' ;
4+ import type { RouteLocationNamedRaw } from '@sa/simple-router' ;
5+
6+ interface RouterPushOptions {
7+ query ?: Record < string , string > ;
8+ params ?: Record < string , string > ;
9+ }
410
511/**
612 * Router push
713 *
814 * Jump to the specified route, it can replace function router.push
915 */
1016export function useRouterPush ( ) {
17+ const router = useRouter ( ) ;
18+
1119 const [ searchParams ] = useSearchParams ( ) ;
12- const { pathname } = useLocation ( ) ;
1320
14- const routerBack = ( ) => {
15- router . reactRouter . navigate ( - 1 ) ;
16- } ;
21+ const routerPush = router . push ;
1722
18- async function toHome ( ) {
19- return router . push ( '/' ) ;
20- }
23+ const routerBack = router . back ;
2124
22- function getQuery ( ) {
23- const query : Record < string , string > = { } ;
24- for ( const pairs of searchParams . entries ( ) ) {
25- query [ pairs [ 0 ] ] = pairs [ 1 ] ;
26- }
27- return query ;
28- }
25+ async function routerPushByKey ( key : RouteKey , options ?: RouterPushOptions ) {
26+ const { query, params } = options || { } ;
2927
30- /**
31- * Navigate to login page
32- *
33- * @param loginModule The login module
34- * @param redirectUrl The redirect url, if not specified, it will be the current route fullPath
35- */
36- async function toLogin ( loginModule ?: UnionKey . LoginModule , redirectUrl ?: string ) {
37- const module = loginModule || 'pwd-login' ;
28+ const routeLocation : RouteLocationNamedRaw = {
29+ name : key
30+ } ;
3831
39- const redirect = redirectUrl || pathname ;
32+ if ( Object . keys ( query || { } ) . length ) {
33+ routeLocation . query = query ;
34+ }
4035
41- return router . reactRouter . navigate ( { pathname : `/login/${ module } ` , search : `redirect=${ redirect } ` } ) ;
42- }
43- function objectToQueryParams ( obj : Record < string , string | number > ) {
44- const params = new URLSearchParams ( ) ;
45- for ( const key in obj ) {
46- if ( Object . hasOwn ( obj , key ) ) {
47- params . append ( key , obj [ key ] as string ) ;
48- }
36+ if ( Object . keys ( params || { } ) . length ) {
37+ routeLocation . params = params ;
4938 }
50- return params . toString ( ) ;
39+
40+ return routerPush ( routeLocation ) ;
5141 }
5242
5343 /**
@@ -67,41 +57,77 @@ export function useRouterPush() {
6757 return query ;
6858 } ;
6959
70- const menuPush = ( key : string ) => {
71- startTransition ( ( ) => {
72- const query = getRouteQueryOfMetaByKey ( key ) ;
73- router . push ( { name : key , query } ) ;
74- } ) ;
75- } ;
60+ function routerPushByKeyWithMetaQuery ( key : RouteKey ) {
61+ const query = getRouteQueryOfMetaByKey ( key ) ;
62+
63+ return routerPushByKey ( key , { query } ) ;
64+ }
65+
66+ async function toHome ( ) {
67+ return routerPush ( '/' ) ;
68+ }
69+
70+ /**
71+ * Navigate to login page
72+ *
73+ * @param loginModule The login module
74+ * @param redirectUrl The redirect url, if not specified, it will be the current route fullPath
75+ */
76+ async function toLogin ( loginModule ?: UnionKey . LoginModule , redirectUrl ?: string ) {
77+ const module = loginModule || 'pwd-login' ;
78+
79+ const options : RouterPushOptions = {
80+ params : {
81+ module
82+ }
83+ } ;
84+
85+ const redirect = redirectUrl || router . currentRoute . fullPath ;
86+
87+ options . query = {
88+ redirect
89+ } ;
90+
91+ return routerPushByKey ( 'login' , options ) ;
92+ }
7693
7794 /**
7895 * Toggle login module
7996 *
8097 * @param module
8198 */
82- function toggleLoginModule ( module : UnionKey . LoginModule ) {
83- const query = getQuery ( ) ;
99+ async function toggleLoginModule ( module : UnionKey . LoginModule ) {
100+ const query = router . currentRoute . query as Record < string , string > ;
101+
102+ return routerPushByKey ( `login_${ module } ` , { query } ) ;
103+ }
84104
85- router . reactRouter . navigate ( {
86- pathname : `/login/ ${ module } ` ,
87- search : objectToQueryParams ( query )
105+ function menuPush ( key : string ) {
106+ startTransition ( ( ) => {
107+ routerPushByKeyWithMetaQuery ( key as RouteKey ) ;
88108 } ) ;
89109 }
90110
91- /** Redirect from login */
92- function redirectFromLogin ( ) {
111+ /**
112+ * Redirect from login
113+ *
114+ * @param [needRedirect=true] Whether to redirect after login. Default is `true`
115+ */
116+ async function redirectFromLogin ( needRedirect = true ) {
93117 const redirect = searchParams . get ( 'redirect' ) ;
94118
95- if ( redirect ) {
96- router . reactRouter . navigate ( redirect ) ;
119+ if ( needRedirect && redirect ) {
120+ routerPush ( redirect ) ;
97121 } else {
98122 toHome ( ) ;
99123 }
100124 }
125+
101126 return {
102- routerPush : router ,
127+ routerPush,
103128 routerBack,
104129 toLogin,
130+ routerPushByKeyWithMetaQuery,
105131 menuPush,
106132 redirectFromLogin,
107133 toggleLoginModule
0 commit comments