aboutsummaryrefslogtreecommitdiff
path: root/lib/vnlib.browser/src/helpers/lastPage.ts
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-01-01 12:32:14 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-01-01 12:32:14 -0500
commit0ca26fc63cc5311298575209b124516139f58206 (patch)
treeb7062317330da4b79a31d47cfd5a39b939fbc723 /lib/vnlib.browser/src/helpers/lastPage.ts
parent4890ffb8f4aafed897cabc368fa105a70615a2de (diff)
package updates, pageguard & dead exports
Diffstat (limited to 'lib/vnlib.browser/src/helpers/lastPage.ts')
-rw-r--r--lib/vnlib.browser/src/helpers/lastPage.ts25
1 files changed, 19 insertions, 6 deletions
diff --git a/lib/vnlib.browser/src/helpers/lastPage.ts b/lib/vnlib.browser/src/helpers/lastPage.ts
index 8be2116..b3c7103 100644
--- a/lib/vnlib.browser/src/helpers/lastPage.ts
+++ b/lib/vnlib.browser/src/helpers/lastPage.ts
@@ -1,4 +1,4 @@
-// Copyright (c) 2023 Vaughn Nugent
+// Copyright (c) 2024 Vaughn Nugent
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
@@ -50,6 +50,19 @@ export interface ILastPageStorage {
pop(): string | null;
}
+/**
+ * Represents a router-like object that can be used
+ * to navigate to a desired route
+ */
+export interface RouterLike{
+ currentRoute: {
+ value: {
+ fullPath: string;
+ }
+ }
+ push(route: object | string): void;
+}
+
const storageKey = "lastPage";
//Storage impl
@@ -79,29 +92,29 @@ const defaultStack = (): ILastPageStorage => {
* last page after login.
* @returns { gotoLastPage: Function }
*/
-export const useLastPage = (storage?: ILastPageStorage): ILastPage => {
+export const useLastPage = (storage?: ILastPageStorage, router?: RouterLike): ILastPage => {
//fallback to default storage
const _storage = defaultTo(storage, defaultStack());
//Get the current router instance
- const router = useRouter();
+ const _router = defaultTo(router, useRouter());
//Store the current page to the last page stack
- const push = () => _storage.push(router.currentRoute.value.fullPath);
+ const push = () => _storage.push(_router.currentRoute.value.fullPath);
const pushAndNavigate = (route: object) => {
//Store the current page to the last page stack
push();
//Navigate to the desired route
- router.push(route);
+ _router.push(route);
};
const gotoLastPage = () => {
//Get the last stored page and navigate to it
const lp = _storage.pop();
if (lp) {
- router.push(lp);
+ _router.push(lp);
}
};