summaryrefslogtreecommitdiff
path: root/packages/web-util/src/context/navigation.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/web-util/src/context/navigation.ts')
-rw-r--r--packages/web-util/src/context/navigation.ts46
1 files changed, 29 insertions, 17 deletions
diff --git a/packages/web-util/src/context/navigation.ts b/packages/web-util/src/context/navigation.ts
index a2fe3ff12..c2f2bbbc1 100644
--- a/packages/web-util/src/context/navigation.ts
+++ b/packages/web-util/src/context/navigation.ts
@@ -16,17 +16,13 @@
import { ComponentChildren, createContext, h, VNode } from "preact";
import { useContext, useEffect, useState } from "preact/hooks";
-import { AppLocation, ObjectOf, Location, findMatch, RouteDefinition } from "../utils/route.js";
-
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-export function useCurrentLocation<T extends ObjectOf<RouteDefinition<any>>>(
- pagesMap: T,
-): Location<T> | undefined {
- const pageList = Object.keys(pagesMap as object) as Array<keyof T>;
- const { path, params } = useNavigationContext();
-
- return findMatch(pagesMap, pageList, path, params);
-}
+import {
+ AppLocation,
+ ObjectOf,
+ Location,
+ findMatch,
+ RouteDefinition,
+} from "../utils/route.js";
/**
*
@@ -35,7 +31,7 @@ export function useCurrentLocation<T extends ObjectOf<RouteDefinition<any>>>(
export type Type = {
path: string;
- params: Record<string, string>;
+ params: Record<string, string[]>;
navigateTo: (path: AppLocation) => void;
// addNavigationListener: (listener: (path: string, params: Record<string, string>) => void) => (() => void);
};
@@ -45,13 +41,29 @@ const Context = createContext<Type>(undefined);
export const useNavigationContext = (): Type => useContext(Context);
-function getPathAndParamsFromWindow() {
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export function useCurrentLocation<T extends ObjectOf<RouteDefinition<any>>>(
+ pagesMap: T,
+): Location<T> | undefined {
+ const pageList = Object.keys(pagesMap as object) as Array<keyof T>;
+ const { path, params } = useNavigationContext();
+
+ return findMatch(pagesMap, pageList, path, params);
+}
+
+function getPathAndParamsFromWindow(): {
+ path: string;
+ params: Record<string, string[]>;
+} {
const path =
typeof window !== "undefined" ? window.location.hash.substring(1) : "/";
- const params: Record<string, string> = {};
+ const params: Record<string, string[]> = {};
if (typeof window !== "undefined") {
for (const [key, value] of new URLSearchParams(window.location.search)) {
- params[key] = value;
+ if (!params[key]) {
+ params[key] = [];
+ }
+ params[key].push(value);
}
}
return { path, params };
@@ -80,14 +92,14 @@ export const BrowserHashNavigationProvider = ({
"Can't use BrowserHashNavigationProvider if there is no window object",
);
}
- function navigateTo(path: string) {
+ function navigateTo(path: string): void {
const { params } = getPathAndParamsFromWindow();
setState({ path, params });
window.location.href = path;
}
useEffect(() => {
- function eventListener() {
+ function eventListener(): void {
setState(getPathAndParamsFromWindow());
}
window.addEventListener(PopStateEventType, eventListener);