This repository has been archived by the owner on Aug 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
65 lines (55 loc) · 1.88 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { DefaultQuery, RouterProps } from "next/router";
import {
createOafRouter,
defaultSettings as oafRoutingDefaultSettings,
RouterSettings,
} from "oaf-routing";
// tslint:disable-next-line: no-commented-code
// tslint:disable: no-expression-statement
// tslint:disable: object-literal-sort-keys
// tslint:disable: no-object-mutation
export { RouterSettings } from "oaf-routing";
export const defaultSettings: RouterSettings<string> = {
...oafRoutingDefaultSettings,
// TODO use `history` to track route IDs and action (POP vs PUSH vs REPLACE)?
restorePageStateOnPop: false,
// We're not restoring page state ourselves so leave this enabled.
disableAutoScrollRestoration: false,
};
export const wrapRouter = <Q = DefaultQuery>(
router: RouterProps<Q>,
settingsOverrides?: Partial<RouterSettings<string>>,
): (() => void) => {
const settings = {
...defaultSettings,
...settingsOverrides,
};
const oafRouter = createOafRouter(settings, url => {
// TODO https://caniuse.com/#feat=url
// return new URL(url).hash;
const a = document.createElement("a");
a.href = url;
return a.hash;
});
oafRouter.handleFirstPageLoad(router.route);
// tslint:disable-next-line: no-let
let previousRoute: string = router.route;
const handleLocationChanged = (url: string) => {
// TODO use `history` to track route IDs and action (POP vs PUSH vs REPLACE)?
const currentLocationKey = undefined;
const action = undefined;
oafRouter.handleLocationChanged(
previousRoute,
url,
currentLocationKey,
action,
);
previousRoute = url;
};
router.events.on("routeChangeComplete", handleLocationChanged);
router.events.on("hashChangeComplete", handleLocationChanged);
return () => {
router.events.off("routeChangeComplete", handleLocationChanged);
router.events.off("hashChangeComplete", handleLocationChanged);
};
};