1
1
import { defineStore , storeToRefs } from 'pinia' ;
2
2
import { ref } from 'vue' ;
3
3
4
+ import type { RouteLocationNormalized } from 'vue-router' ;
5
+
6
+ import { storage } from '~/utils/browser/browser-storage.utils' ;
7
+
4
8
const parseQuery = ( location : Location ) => {
5
9
const params = new URLSearchParams ( location . href . split ( '?' ) . at ( 1 ) ?. replace ( / # .* $ / , '' ) ) ;
6
10
if ( params . has ( 'code' ) ) {
@@ -14,10 +18,24 @@ const parseQuery = (location: Location) => {
14
18
export const useRouterStore = defineStore ( 'router' , ( ) => {
15
19
const initialLocation = ref ( { ...window . location } ) ;
16
20
const routeParam = ref < Record < string , string > | undefined > ( parseQuery ( initialLocation . value ) ) ;
21
+ const lastRoute = ref < RouteLocationNormalized > ( ) ;
22
+
17
23
const setRouteParam = ( params ?: Record < string , string > ) => {
18
24
routeParam . value = params ;
19
25
} ;
20
26
27
+ const setLastRoute = ( _route : RouteLocationNormalized ) => {
28
+ lastRoute . value = _route ;
29
+ return storage . local . set ( 'app.state.last-route' , _route ) ;
30
+ } ;
31
+
32
+ const restoreLastRoute = async ( ) => {
33
+ const _route = await storage . local . get < RouteLocationNormalized > ( 'app.state.last-route' ) ;
34
+ if ( _route ) lastRoute . value = _route ;
35
+ console . info ( 'router-store' , 'restored last route' , JSON . parse ( JSON . stringify ( lastRoute . value ) ) ) ;
36
+ return _route ;
37
+ } ;
38
+
21
39
const baseName = ref ( '' ) ;
22
40
const setBaseName = ( name : string ) => {
23
41
baseName . value = name ;
@@ -28,7 +46,7 @@ export const useRouterStore = defineStore('router', () => {
28
46
baseUrl . value = url ;
29
47
} ;
30
48
31
- return { baseName, setBaseName, baseUrl, setBaseUrl, initialLocation, routeParam, setRouteParam } ;
49
+ return { baseName, setBaseName, baseUrl, setBaseUrl, initialLocation, routeParam, setRouteParam, lastRoute , setLastRoute , restoreLastRoute } ;
32
50
} ) ;
33
51
34
52
export const useRouterStoreRefs = ( ) => storeToRefs ( useRouterStore ( ) ) ;
0 commit comments