summaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/route.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/demobank-ui/src/route.ts')
-rw-r--r--packages/demobank-ui/src/route.ts124
1 files changed, 10 insertions, 114 deletions
diff --git a/packages/demobank-ui/src/route.ts b/packages/demobank-ui/src/route.ts
index 72b405791..912ba274d 100644
--- a/packages/demobank-ui/src/route.ts
+++ b/packages/demobank-ui/src/route.ts
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
-import { useEffect, useState } from "preact/hooks";
+import { useNavigationContext } from "./context/navigation.js";
export function urlPattern<
T extends Record<string, string> = Record<string, never>,
@@ -24,29 +24,6 @@ export function urlPattern<
};
}
-// export function Router({
-// pageList,
-// onNotFound,
-// }: {
-// pageList: Array<PageEntry<unknown>>;
-// onNotFound: () => VNode;
-// }): VNode {
-// const current = useCurrentLocation<unknown>(pageList);
-// if (current !== undefined) {
-// const d = current.page.url
-// if (typeof current.page.url === "string") {
-// const p = current.page.url
-// return create(current.page.view, {});
-// }
-// const p = current.page.url
-// return create(current.page.view, current.values);
-// }
-// return onNotFound();
-// }
-// type PagesMap<T extends object> = {
-// [name in keyof T]: PageDefinition<any>;
-// };
-
export type RouteDefinition<T> = {
pattern: RegExp;
url: (p: T) => string;
@@ -72,17 +49,9 @@ export type RouteParamsType<
type Location<E, T extends RouteMap<E>, NAME extends keyof T> = {
parent: T;
name: NAME;
- // mapped values from params and url
values: RouteParamsType<T, NAME>;
};
-const STARTUP_SPA_LOCATION =
- typeof window !== "undefined" ? window.location.hash.substring(1) : "/";
-const STARTUP_SPA_PARAMS =
- typeof window !== "undefined"
- ? new URLSearchParams(window.location.search)
- : new URLSearchParams();
-
/**
* Search path in the pageList
* get the values from the path found
@@ -91,11 +60,11 @@ const STARTUP_SPA_PARAMS =
* @param path
* @param params
*/
-function doSync<DEF, RM extends RouteMap<DEF>, ROUTES extends keyof RM>(
+function findMatch<DEF, RM extends RouteMap<DEF>, ROUTES extends keyof RM>(
pagesMap: RM,
pageList: Array<ROUTES>,
path: string,
- params: URLSearchParams,
+ params: Record<string, string>,
): Location<DEF, RM, ROUTES> | undefined {
for (let idx = 0; idx < pageList.length; idx++) {
const name = pageList[idx];
@@ -103,9 +72,10 @@ function doSync<DEF, RM extends RouteMap<DEF>, ROUTES extends keyof RM>(
if (found !== null) {
const values =
found.groups === undefined ? {} : structuredClone(found.groups);
- params.forEach((v, k) => {
- values[k] = v;
- });
+
+ Object.entries(params).forEach(([key, value]) => {
+ values[key] = value
+ })
// @ts-expect-error values is a map string which is equivalent to the RouteParamsType
return { name, parent: pagesMap, values };
@@ -114,87 +84,13 @@ function doSync<DEF, RM extends RouteMap<DEF>, ROUTES extends keyof RM>(
return undefined;
}
-const PopStateEventType = "popstate";
-
export function useCurrentLocation<
DEF,
RM extends RouteMap<DEF>,
ROUTES extends keyof RM,
>(pagesMap: RM) {
const pageList = Object.keys(pagesMap) as Array<ROUTES>;
- const [currentLocation, setCurrentLocation] = useState<
- Location<DEF, RM, ROUTES> | undefined
- >(doSync(pagesMap, pageList, STARTUP_SPA_LOCATION, STARTUP_SPA_PARAMS));
- useEffect(() => {
- window.addEventListener(PopStateEventType, () => {
- const path = window.location.hash.substring(1);
- console.log("event", path);
- const l = doSync(
- pagesMap,
- pageList,
- path,
- new URLSearchParams(window.location.search),
- );
- setCurrentLocation(l);
- });
- }, []);
- function routeTo<N extends ROUTES>(
- n: N,
- values: RouteParamsType<RM, N>,
- ): void {
- setCurrentLocation({
- parent: pagesMap,
- name: n,
- values,
- });
- }
- return [currentLocation, routeTo] as const;
-}
-
-// function doestUrlMatchToRoute(
-// url: string,
-// route: string,
-// ): undefined | Record<string, string> {
-// const paramsPattern = /(?:\?([^#]*))?$/;
+ const { path, params } = useNavigationContext()
-// const urlSeg = url.replace(paramsPattern, "").split("/");
-// const routeSeg = route.split("/");
-// let max = Math.max(urlSeg.length, routeSeg.length);
-
-// const result: Record<string, string> = {};
-// for (let i = 0; i < max; i++) {
-// if (routeSeg[i] && routeSeg[i].charAt(0) === ":") {
-// const param = routeSeg[i].replace(/(^:|[+*?]+$)/g, "");
-
-// const flags = (routeSeg[i].match(/[+*?]+$/) || EMPTY)[0] || "";
-// const plus = ~flags.indexOf("+");
-// const star = ~flags.indexOf("*");
-// const val = urlSeg[i] || "";
-
-// if (!val && !star && (flags.indexOf("?") < 0 || plus)) {
-// return undefined;
-// }
-// result[param] = decodeURIComponent(val);
-// if (plus || star) {
-// result[param] = urlSeg.slice(i).map(decodeURIComponent).join("/");
-// break;
-// }
-// } else if (routeSeg[i] !== urlSeg[i]) {
-// return undefined;
-// }
-// }
-
-// const params = url.match(paramsPattern);
-// if (params && params[1]) {
-// const paramList = params[1].split("&");
-// for (let i = 0; i < paramList.length; i++) {
-// const idx = paramList[i].indexOf("=");
-// const name = paramList[i].substring(0, idx);
-// const value = paramList[i].substring(idx + 1);
-// result[decodeURIComponent(name)] = decodeURIComponent(value);
-// }
-// }
-
-// return result;
-// }
-// const EMPTY: Record<string, string> = {};
+ return findMatch(pagesMap, pageList, path, params);
+}