diff options
Diffstat (limited to 'packages/aml-backoffice-ui/src/route.ts')
-rw-r--r-- | packages/aml-backoffice-ui/src/route.ts | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/packages/aml-backoffice-ui/src/route.ts b/packages/aml-backoffice-ui/src/route.ts index 9176ab5e4..091b92d5c 100644 --- a/packages/aml-backoffice-ui/src/route.ts +++ b/packages/aml-backoffice-ui/src/route.ts @@ -79,48 +79,17 @@ type Location = { path: string; values: Record<string, string>; }; -export function useCurrentLocation(pageList: Array<PageEntry<any>>) { - const [currentLocation, setCurrentLocation] = useState<Location>(); - /** - * Search path in the pageList - * get the values from the path found - * add params from searchParams - * - * @param path - * @param params - */ - function doSync(path: string, params: URLSearchParams) { - let result: typeof currentLocation; - for (let idx = 0; idx < pageList.length; idx++) { - const page = pageList[idx]; - if (typeof page.url === "string") { - if (page.url === path) { - const values: Record<string, string> = {}; - params.forEach((v, k) => { - values[k] = v; - }); - result = { page, values, path }; - break; - } - } else { - const values = doestUrlMatchToRoute(path, page.url.pattern); - if (values !== undefined) { - params.forEach((v, k) => { - values[k] = v; - }); - result = { page, values, path }; - break; - } - } - } - setCurrentLocation(result); - } +export function useCurrentLocation(pageList: Array<PageEntry<any>>): Location | undefined { + const [currentLocation, setCurrentLocation] = useState<Location | null | undefined>(null); useEffect(() => { - doSync(window.location.hash, new URLSearchParams(window.location.search)); return history.listen(() => { - doSync(window.location.hash, new URLSearchParams(window.location.search)); + const result = doSync(window.location.hash, new URLSearchParams(window.location.search), pageList); + setCurrentLocation(result); }); }, []); + if (currentLocation === null) { + return doSync(window.location.hash, new URLSearchParams(window.location.search), pageList); + } return currentLocation; } @@ -134,6 +103,38 @@ export function useChangeLocation() { return location; } +/** + * Search path in the pageList + * get the values from the path found + * add params from searchParams + * + * @param path + * @param params + */ +export function doSync(path: string, params: URLSearchParams, pageList: Array<PageEntry<any>>): Location | undefined { + for (let idx = 0; idx < pageList.length; idx++) { + const page = pageList[idx]; + if (typeof page.url === "string") { + if (page.url === path) { + const values: Record<string, string> = {}; + params.forEach((v, k) => { + values[k] = v; + }); + return { page, values, path }; + } + } else { + const values = doestUrlMatchToRoute(path, page.url.pattern); + if (values !== undefined) { + params.forEach((v, k) => { + values[k] = v; + }); + return { page, values, path }; + } + } + } + return undefined; +} + function doestUrlMatchToRoute( url: string, route: string, |