diff options
author | Sebastian <sebasjm@gmail.com> | 2023-11-20 12:38:16 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-11-20 12:38:16 -0300 |
commit | 6138846050563e0dca95b0b6d792776925e4c35f (patch) | |
tree | b33cd36acf4b38d3a016506d4f7fa681c83beb63 /packages/aml-backoffice-ui/src/route.ts | |
parent | 7ed3e78f790837479fc2bb2eb6ddc40c78ce59b5 (diff) | |
download | wallet-core-6138846050563e0dca95b0b6d792776925e4c35f.tar.xz |
new forms api
Diffstat (limited to 'packages/aml-backoffice-ui/src/route.ts')
-rw-r--r-- | packages/aml-backoffice-ui/src/route.ts | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/packages/aml-backoffice-ui/src/route.ts b/packages/aml-backoffice-ui/src/route.ts index 091b92d5c..f515a590a 100644 --- a/packages/aml-backoffice-ui/src/route.ts +++ b/packages/aml-backoffice-ui/src/route.ts @@ -1,8 +1,20 @@ import { TranslatedString } from "@gnu-taler/taler-util"; import { createHashHistory } from "history"; -import { h as create, VNode } from "preact"; -import { useEffect, useState } from "preact/hooks"; -const history = createHashHistory(); +import { ComponentChildren, h as create, createContext, VNode } from "preact"; +import { useContext, useEffect, useState } from "preact/hooks"; + +type ContextType = { + onChange: (listener: () => void) => VoidFunction +} +const nullChangeListener = { onChange: () => () => { } } +const Context = createContext<ContextType>(nullChangeListener); + +export const usePathChangeContext = (): ContextType => useContext(Context); + +export function HashPathProvider({ children }: { children: ComponentChildren }): VNode { + const history = createHashHistory(); + return create(Context.Provider, { value: { onChange: history.listen }, children }, children) +} type PageDefinition<DynamicPart extends Record<string, string>> = { pattern: string; @@ -81,8 +93,9 @@ type Location = { }; export function useCurrentLocation(pageList: Array<PageEntry<any>>): Location | undefined { const [currentLocation, setCurrentLocation] = useState<Location | null | undefined>(null); + const path = usePathChangeContext(); useEffect(() => { - return history.listen(() => { + return path.onChange(() => { const result = doSync(window.location.hash, new URLSearchParams(window.location.search), pageList); setCurrentLocation(result); }); @@ -95,8 +108,9 @@ export function useCurrentLocation(pageList: Array<PageEntry<any>>): Location | export function useChangeLocation() { const [location, setLocation] = useState(window.location.hash) + const path = usePathChangeContext() useEffect(() => { - return history.listen(() => { + return path.onChange(() => { setLocation(window.location.hash) }); }, []); |