-
Notifications
You must be signed in to change notification settings - Fork 96
/
QueryParamProvider.tsx
224 lines (206 loc) · 6.03 KB
/
QueryParamProvider.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as React from 'react';
import { HistoryLocation, PushReplaceHistory } from './types';
import { LocationProvider } from './LocationProvider';
import { ExtendedStringifyOptions } from 'serialize-query-params';
import shallowEqual from './shallowEqual';
// we use a lazy caching solution to prevent #46 from happening
let cachedWindowHistory: History | undefined;
let cachedAdaptedWindowHistory: PushReplaceHistory | undefined;
/**
* Adapts standard DOM window history to work with our
* { replace, push } interface.
*
* @param history Standard history provided by DOM
*/
function adaptWindowHistory(history: History): PushReplaceHistory {
if (history === cachedWindowHistory && cachedAdaptedWindowHistory != null) {
return cachedAdaptedWindowHistory;
}
const adaptedWindowHistory = {
replace(location: Location) {
history.replaceState(
(location as any).state,
'',
`${location.protocol}//${location.host}${location.pathname}${location.search}`
);
},
push(location: Location) {
history.pushState(
(location as any).state,
'',
`${location.protocol}//${location.host}${location.pathname}${location.search}`
);
},
get location() {
return window.location;
},
};
cachedWindowHistory = history;
cachedAdaptedWindowHistory = adaptedWindowHistory;
return adaptedWindowHistory;
}
/**
* Subset of a @reach/router history object. We only
* care about the navigate function.
*/
interface ReachHistory {
navigate: (
to: string,
options?: {
state?: any;
replace?: boolean;
}
) => void;
location: Location;
}
// we use a lazy caching solution to prevent #46 from happening
let cachedReachHistory: ReachHistory | undefined;
let cachedAdaptedReachHistory: PushReplaceHistory | undefined;
/**
* Adapts @reach/router history to work with our
* { replace, push } interface.
*
* @param history globalHistory from @reach/router
*/
function adaptReachHistory(history: ReachHistory): PushReplaceHistory {
if (history === cachedReachHistory && cachedAdaptedReachHistory != null) {
return cachedAdaptedReachHistory;
}
const adaptedReachHistory = {
replace(location: Location) {
history.navigate(
`${location.protocol}//${location.host}${location.pathname}${location.search}`,
{ replace: true }
);
},
push(location: Location) {
history.navigate(
`${location.protocol}//${location.host}${location.pathname}${location.search}`,
{ replace: false }
);
},
get location() {
return history.location;
},
};
cachedReachHistory = history;
cachedAdaptedReachHistory = adaptedReachHistory;
return adaptedReachHistory;
}
/**
* Helper to produce the context value falling back to
* window history and location if not provided.
*/
export function getLocationProps({
history,
location,
}: Partial<HistoryLocation> = {}): HistoryLocation {
const hasWindow = typeof window !== 'undefined';
if (hasWindow) {
if (!history) {
history = adaptWindowHistory(window.history);
}
if (!location) {
location = window.location;
}
}
if (!location) {
throw new Error(`
Could not read the location. Is the router wired up correctly?
`);
}
return { history, location };
}
/**
* Props for the Provider component, used to hook the active routing
* system into our controls.
*/
interface QueryParamProviderProps {
/** Main app goes here */
children: React.ReactNode;
/** `Route` from react-router */
ReactRouterRoute?: React.ComponentClass | React.FunctionComponent;
/** `globalHistory` from @reach/router */
reachHistory?: ReachHistory;
/** Manually provided history that meets the { replace, push } interface */
history?: PushReplaceHistory;
/**
* Override location object, otherwise window.location or the
* location provided by the active routing system is used.
*/
location?: Location;
/**
* Options to customize the stringifying of the query string
* These are passed directly to query-string.stringify, with
* the exception of transformSearchString which runs on the result
* of stringify if provided.
*/
stringifyOptions?: ExtendedStringifyOptions;
}
/**
* Context provider for query params to have access to the
* active routing system, enabling updates to the URL.
*/
export function QueryParamProvider({
children,
ReactRouterRoute,
reachHistory,
history,
location,
stringifyOptions,
}: QueryParamProviderProps) {
// cache the stringify options object so we users can just do
// <QueryParamProvider stringifyOptions={{ encode: false }} />
const stringifyOptionsRef = React.useRef(stringifyOptions);
const hasNewStringifyOptions = !shallowEqual(
stringifyOptionsRef.current,
stringifyOptions
);
const stringifyOptionsCached = hasNewStringifyOptions
? stringifyOptions
: stringifyOptionsRef.current;
React.useEffect(() => {
stringifyOptionsRef.current = stringifyOptionsCached;
}, [stringifyOptionsCached]);
// if we have React Router, use it to get the context value
if (ReactRouterRoute) {
return (
<ReactRouterRoute>
{(routeProps: any) => {
return (
<LocationProvider
stringifyOptions={stringifyOptionsCached}
{...getLocationProps(routeProps)}
>
{children}
</LocationProvider>
);
}}
</ReactRouterRoute>
);
}
// if we are using reach router, use its history
if (reachHistory) {
return (
<LocationProvider
stringifyOptions={stringifyOptionsCached}
{...getLocationProps({
history: adaptReachHistory(reachHistory),
location,
})}
>
{children}
</LocationProvider>
);
}
// neither reach nor react-router, so allow manual overrides
return (
<LocationProvider
stringifyOptions={stringifyOptionsCached}
{...getLocationProps({ history, location })}
>
{children}
</LocationProvider>
);
}
export default QueryParamProvider;