Skip to content

Commit d5da030

Browse files
committed
feat(web): fix web routing and adds last route restore
1 parent 1a0da58 commit d5da030

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

src/router/create-router.ts

+10
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,15 @@ export const createRouter = ({ baseName = '', baseUrl = import.meta.env.BASE_URL
5353
}
5454
});
5555

56+
const { restoreLastRoute, setLastRoute } = useRouterStore();
57+
58+
router.afterEach(async to => {
59+
await setLastRoute(to);
60+
});
61+
62+
restoreLastRoute().then(async _route => {
63+
await router.push(_route?.name ? _route : { name: Route.Progress });
64+
});
65+
5666
return router;
5767
};

src/stores/router.store.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { defineStore, storeToRefs } from 'pinia';
22
import { ref } from 'vue';
33

4+
import type { RouteLocationNormalized } from 'vue-router';
5+
6+
import { storage } from '~/utils/browser/browser-storage.utils';
7+
48
const parseQuery = (location: Location) => {
59
const params = new URLSearchParams(location.href.split('?').at(1)?.replace(/#.*$/, ''));
610
if (params.has('code')) {
@@ -14,10 +18,24 @@ const parseQuery = (location: Location) => {
1418
export const useRouterStore = defineStore('router', () => {
1519
const initialLocation = ref({ ...window.location });
1620
const routeParam = ref<Record<string, string> | undefined>(parseQuery(initialLocation.value));
21+
const lastRoute = ref<RouteLocationNormalized>();
22+
1723
const setRouteParam = (params?: Record<string, string>) => {
1824
routeParam.value = params;
1925
};
2026

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+
2139
const baseName = ref('');
2240
const setBaseName = (name: string) => {
2341
baseName.value = name;
@@ -28,7 +46,7 @@ export const useRouterStore = defineStore('router', () => {
2846
baseUrl.value = url;
2947
};
3048

31-
return { baseName, setBaseName, baseUrl, setBaseUrl, initialLocation, routeParam, setRouteParam };
49+
return { baseName, setBaseName, baseUrl, setBaseUrl, initialLocation, routeParam, setRouteParam, lastRoute, setLastRoute, restoreLastRoute };
3250
});
3351

3452
export const useRouterStoreRefs = () => storeToRefs(useRouterStore());

src/utils/browser/browser.utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ export const createTab = (options: chrome.tabs.CreateProperties) => (chrome?.tab
1717
* @see [chrome.runtime.id](https://developer.chrome.com/docs/extensions/reference/runtime/#property-id)
1818
*/
1919
export const chromeRuntimeId = chrome?.runtime?.id;
20+
21+
/**
22+
* The i18n API for the current browser.
23+
* @see [chrome.i18n](https://developer.chrome.com/docs/extensions/reference/i18n/)
24+
*/
25+
export const chromeI18n = chrome?.i18n;

src/utils/i18n.utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import type { Locale } from '~/models/i18n.model';
33
import { useI18nStore } from '~/stores/i18n.store';
44
import { useRouterStore } from '~/stores/router.store';
55
import { useI18nTranslate } from '~/utils/browser/browser-i18n.utils';
6+
import { chromeI18n } from '~/utils/browser/browser.utils';
67

78
/**
89
* Setup i18n function to either use chrome i18n resolver or a local store (for web use).
910
* @param roots modules names
1011
* @see chrome.i18n.getMessage
1112
*/
1213
export const useI18n = (...roots: string[]): ReturnType<typeof useI18nTranslate> => {
13-
if (!window?.chrome?.i18n) {
14+
if (!chromeI18n) {
1415
const store = useI18nStore();
1516
const router = useRouterStore();
1617

src/web/init-services.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const initServices = async () => {
1515

1616
TraktService.listen();
1717

18-
await syncRestoreAllUsers();
19-
2018
setAppReady(true);
19+
20+
await syncRestoreAllUsers();
2121
};

0 commit comments

Comments
 (0)