-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
182 lines (173 loc) · 5.63 KB
/
index.tsx
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {Key, parse, regexpToFunction, tokensToFunction, tokensToRegexp} from 'path-to-regexp';
import * as React from 'react';
export type Route<Params extends object> = {
path: string;
matchUrl: (url: Url) => Params | null;
testUrl: (path: string) => boolean;
toUrl: (params: Params) => string;
};
export function createRoute<Params extends object>(path: string): Route<Params> {
const keys: Key[] = [];
const tokens = parse(path);
const re = tokensToRegexp(tokens, keys);
const _match = regexpToFunction<Params>(re, keys, {decode: decodeURIComponent});
const _toUrl = tokensToFunction<Params>(tokens, {encode: encodeURIComponent});
const requiredFields = new Set(
tokens
.map(token => (typeof token !== 'string' ? String(token.name) : undefined))
.filter((t): t is string => t !== undefined),
);
const matchUrl = (url: Url) => {
const m = _match(url.path);
if (m === false) {
return null;
}
const params = m.params;
url.query.split('&').forEach(p => {
const r = p.split('=');
const key = decodeURIComponent(r[0]) as never;
const val = decodeURIComponent(r[1]) as never;
if (!(key in params)) {
params[key] = val;
}
});
return params;
};
const testUrl = (path: string) => {
return _match(path) !== false;
};
const toUrl = (params: Params) => {
let query = '';
for (const key in params) {
if (!requiredFields.has(key)) {
query += `${encodeURIComponent(key)}=${encodeURIComponent((params[key] as unknown) as string)}`;
}
}
return _toUrl(params) + (query === '' ? '' : `?${query}`);
};
return {
path: path,
matchUrl: matchUrl,
testUrl: testUrl,
toUrl: toUrl,
};
}
export type Router = {
url: Url;
push: (url: string) => void;
replace: (url: string) => void;
};
export type Url = {
path: string;
query: string;
state: object | undefined;
};
const createUrl = (): Url => ({
path: window.location.pathname,
query: window.location.search.substr(1),
state: window.history.state,
});
export const Router = React.memo(function Router(props: {
initialUrl?: string;
children: React.ReactNode;
scrollRestoration?: boolean;
onBeforeNextUrl?: (url: string) => object;
onAfterNextUrl?: (url: string) => object;
onBackForward?: (url: string) => object;
}) {
const {children, initialUrl, onBeforeNextUrl, onAfterNextUrl, onBackForward, scrollRestoration = true} = props;
const defaultUrl = (): Url => {
if (initialUrl === undefined) {
return createUrl();
}
const p = initialUrl.split('?');
return {
path: p[0],
query: p[1] || '',
state: undefined,
};
};
const [url, setUrl] = React.useState(defaultUrl);
const [fromBackForward, setBackForwardState] = React.useState(false);
const change = (type: 'pushState' | 'replaceState') => (url: string) => {
if (typeof window === 'object') {
const state = onBeforeNextUrl && onBeforeNextUrl(url);
window.history[type](state, '', url);
onAfterNextUrl && onAfterNextUrl(url);
setUrl(createUrl());
setBackForwardState(false);
}
};
const router: Router = {
push: change('pushState'),
replace: change('replaceState'),
url: url,
};
React.useEffect(() => {
if (scrollRestoration) {
window.history.scrollRestoration = 'auto';
}
const fn = () => {
onBackForward && onBackForward(window.location.href);
setUrl(createUrl());
setBackForwardState(true);
};
window.addEventListener('popstate', fn);
return () => window.removeEventListener('popstate', fn);
}, []);
React.useLayoutEffect(() => {
if (!fromBackForward && scrollRestoration) {
window.scrollTo(0, 0);
}
}, [fromBackForward, scrollRestoration]);
return <routerContext.Provider value={router}>{children}</routerContext.Provider>;
});
export const routerContext = React.createContext<Router>(null!);
export function Route<Params extends object>(props: {
route: Route<Params>;
children: React.ReactNode | ((params: Params) => React.ReactNode);
}) {
const {children, route} = props;
const router = React.useContext(routerContext);
if (typeof children === 'function') {
const res = route.matchUrl(router.url);
if (res !== null) {
return <>{children(res)}</>;
}
return null;
}
if (route.testUrl(router.url.path)) {
return <>{children}</>;
}
return null;
}
export function Link<Params extends object>(props: {
route: Route<Params>;
params: Params;
className?: string;
children: React.ReactNode;
}) {
const {children, params, route} = props;
const router = React.useContext(routerContext);
const url = route.toUrl(params);
return (
<a
href={url}
className={props.className}
onClick={e => {
if (!e.ctrlKey && !e.metaKey) {
e.preventDefault();
return router.push(url);
}
}}>
{children}
</a>
);
}
export function useRedirect() {
const router = React.useContext(routerContext);
return <Params extends object>(route: Route<Params>, params: Params) => {
const url = route.toUrl(params);
router.push(url);
};
}