diff options
Diffstat (limited to 'packages/demobank-ui/src/context/navigation.ts')
-rw-r--r-- | packages/demobank-ui/src/context/navigation.ts | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/packages/demobank-ui/src/context/navigation.ts b/packages/demobank-ui/src/context/navigation.ts index fc1460c02..acdac4364 100644 --- a/packages/demobank-ui/src/context/navigation.ts +++ b/packages/demobank-ui/src/context/navigation.ts @@ -35,17 +35,19 @@ const Context = createContext<Type>(undefined); export const useNavigationContext = (): Type => useContext(Context); function getPathAndParamsFromWindow() { - const path = typeof window !== "undefined" ? window.location.hash.substring(1) : "/"; - const params: Record<string, string> = {} + const path = + typeof window !== "undefined" ? window.location.hash.substring(1) : "/"; + const params: Record<string, string> = {}; if (typeof window !== "undefined") { for (const [key, value] of new URLSearchParams(window.location.search)) { params[key] = value; } } - return { path, params } + return { path, params }; } -const { path: initialPath, params: initialParams } = getPathAndParamsFromWindow() +const { path: initialPath, params: initialParams } = + getPathAndParamsFromWindow(); // there is a posibility that if the browser does a redirection // (which doesn't go through navigatTo function) and that exectued @@ -53,26 +55,35 @@ const { path: initialPath, params: initialParams } = getPathAndParamsFromWindow( // into account const PopStateEventType = "popstate"; -export const BrowserHashNavigationProvider = ({ children }: { children: ComponentChildren }): VNode => { - const [{ path, params }, setState] = useState({ path: initialPath, params: initialParams }) +export const BrowserHashNavigationProvider = ({ + children, +}: { + children: ComponentChildren; +}): VNode => { + const [{ path, params }, setState] = useState({ + path: initialPath, + params: initialParams, + }); if (typeof window === "undefined") { - throw Error("Can't use BrowserHashNavigationProvider if there is no window object") + throw Error( + "Can't use BrowserHashNavigationProvider if there is no window object", + ); } function navigateTo(path: string) { - const { params } = getPathAndParamsFromWindow() - setState({ path, params }) - window.location.href = path + const { params } = getPathAndParamsFromWindow(); + setState({ path, params }); + window.location.href = path; } useEffect(() => { function eventListener() { - setState(getPathAndParamsFromWindow()) + setState(getPathAndParamsFromWindow()); } window.addEventListener(PopStateEventType, eventListener); return () => { - window.removeEventListener(PopStateEventType, eventListener) - } - }, []) + window.removeEventListener(PopStateEventType, eventListener); + }; + }, []); return h(Context.Provider, { value: { path, params, navigateTo }, children, |