-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClientProvider.mjs
272 lines (240 loc) · 8.31 KB
/
ClientProvider.mjs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// @ts-check
import CacheContext from "graphql-react/CacheContext.mjs";
import HydrationTimeStampContext from "graphql-react/HydrationTimeStampContext.mjs";
import LoadingContext from "graphql-react/LoadingContext.mjs";
import {
createElement as h,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import Effect from "./Effect.mjs";
import HeadManagerContext from "./HeadManagerContext.mjs";
import NavigateContext from "./NavigateContext.mjs";
import RouteContext from "./RouteContext.mjs";
import scrollToHash from "./scrollToHash.mjs";
/**
* A React component for use on the client to provide all the React context
* required to enable the entire Ruck API.
* @param {object} props Props.
* @param {number} props.hydrationTimeStamp Milliseconds since the
* [performance time origin](https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin)
* (when the client JavaScript environment started running).
* @param {import("./HeadManager.mjs").default} props.headManager Head tag
* manager.
* @param {import("graphql-react/Cache.mjs").default} props.cache Data cache
* store.
* @param {import("graphql-react/Loading.mjs").default} props.loading Loading
* store.
* @param {import("./serve.mjs").Router} props.router Router.
* @param {import("./serve.mjs").Route} props.initialRoute The initial route
* that was server side rendered.
* @param {() => void} props.onEffectsDone Callback that runs after initial
* effects in children are done.
* @param {import("react").ReactNode} [props.children] React children.
*/
export default function ClientProvider({
hydrationTimeStamp,
headManager,
cache,
loading,
router,
initialRoute,
onEffectsDone,
children,
}) {
const [route, setRoute] = useState(initialRoute);
const navigationAbortControllerRef = useRef(
/** @type {AbortController | null} */ (null),
);
const navigate = useCallback(
/** @type {Navigate} */
async ({
url,
updateHistory = true,
abortController = new AbortController(),
}) => {
if (typeof url !== "string" && !(url instanceof URL)) {
throw new TypeError("Option `url` must be a string or `URL` instance.");
}
if (typeof updateHistory !== "boolean") {
throw new TypeError("Option `updateHistory` must be a boolean.");
}
if (!(abortController instanceof AbortController)) {
throw new TypeError(
"Option `abortController` must be an `AbortController` instance.",
);
}
if (!abortController.signal.aborted) {
const oldUrl = route.url;
// Normalize a possibly absolute or relative `url`.
const newUrl = new URL(String(url), document.baseURI);
const samePage =
newUrl.pathname + newUrl.search === oldUrl.pathname + oldUrl.search;
if (samePage) {
if (updateHistory && newUrl.hash !== oldUrl.hash) {
history.pushState(null, "", String(newUrl));
}
setRoute({
url: newUrl,
content: route.content,
cleanup: route.cleanup,
});
scrollToHash(newUrl.hash);
} else {
try {
if (navigationAbortControllerRef.current) {
navigationAbortControllerRef.current.abort();
}
navigationAbortControllerRef.current = abortController;
dispatchEvent(
new CustomEvent("ruckroutechangestart", {
detail: {
url: newUrl,
abortController,
},
}),
);
// Todo: Validate what the router returns.
const { content, cleanup } = router(newUrl, headManager, false);
let routeContent;
/** @type {((value: unknown) => void) | undefined} */
let onAbort;
const abortPromise = new Promise((resolve) => {
onAbort = resolve;
});
abortController.signal.addEventListener(
"abort",
/** @type {Exclude<typeof onAbort, undefined>} */ (onAbort),
{ once: true },
);
try {
await Promise.race([
// The content might not be a promise.
Promise.resolve(content).then((resolvedContent) => {
routeContent = resolvedContent;
}),
abortPromise,
]);
} finally {
abortController.signal.removeEventListener(
"abort",
/** @type {Exclude<typeof onAbort, undefined>} */ (onAbort),
);
}
if (abortController.signal.aborted) {
try {
if (cleanup) cleanup();
} finally {
dispatchEvent(
new CustomEvent("ruckroutechangeabort", {
detail: {
url: newUrl,
},
}),
);
}
} else {
if (updateHistory) history.pushState(null, "", String(newUrl));
if (!newUrl.hash) scrollTo(0, 0);
const callback = () => {
if (newUrl.hash) {
const target = document.querySelector(newUrl.hash);
if (target) target.scrollIntoView();
else scrollTo(0, 0);
}
dispatchEvent(
new CustomEvent("ruckroutechangeend", {
detail: {
url: newUrl,
},
}),
);
};
setRoute({
url: newUrl,
content: h(Effect, { callback, children: routeContent }),
cleanup,
});
// Now that the new route is set, cleanup the previous route.
if (route.cleanup) route.cleanup();
}
} catch (error) {
dispatchEvent(
new CustomEvent("ruckroutechangeerror", {
detail: {
url: newUrl,
error,
},
}),
);
}
}
}
},
[router, headManager, route.cleanup, route.content, route.url],
);
const onPopState = useCallback(() => {
navigate({
url: new URL(location.href),
updateHistory: false,
});
}, [navigate]);
useEffect(() => {
addEventListener("popstate", onPopState);
return () => {
removeEventListener("popstate", onPopState);
};
}, [onPopState]);
useEffect(onEffectsDone, [onEffectsDone]);
return h(
NavigateContext.Provider,
{ value: navigate },
h(
RouteContext.Provider,
{ value: route },
h(
HydrationTimeStampContext.Provider,
{ value: hydrationTimeStamp },
h(
HeadManagerContext.Provider,
{ value: headManager },
h(
CacheContext.Provider,
{ value: cache },
h(LoadingContext.Provider, { value: loading }, children),
),
),
),
),
);
}
/**
* Navigates the Ruck app to a route URL.
*
* Ruck generally attempts to match what the page content and scroll position
* would be for a SSR page load of the URL, according to these URL scenarios:
*
* - _To the same page, without a hash._ The page scrolls to the start.
* - _To the same page, with a hash._ If the hash target exists it will be
* scrolled to, even if the same hash is already in the URL. If it doesn’t, no
* scrolling happens.
* - _To another page, without a hash._ The page scrolls to the start.
* - _To another page, with a hash._ After route content mounts, if the hash
* target exists it will be scrolled to. If it doesn’t, the page scrolls to
* the start.
* @callback Navigate
* @param {NavigateOptions} options Options.
* @returns {Promise<void>} Resolves once the navigation is done.
*/
/**
* {@linkcode Navigate} options.
* @typedef {object} NavigateOptions
* @prop {string | URL} url Ruck app route URL to navigate to that’s absolute or
* relative to the `document.baseURI`.
* @prop {boolean} [updateHistory] Update the browser `history` API? Defaults
* to `true`.
* @prop {AbortController} [abortController] Abort controller to abort the
* navigation. Has no effect after navigation ends.
*/