1 | import { findFocusedRoute, getActionFromState as getActionFromStateDefault, getPathFromState as getPathFromStateDefault, getStateFromPath as getStateFromPathDefault } from '@react-navigation/core';
|
2 | import { nanoid } from 'nanoid/non-secure';
|
3 | import * as React from 'react';
|
4 | import ServerContext from './ServerContext';
|
5 |
|
6 | const createMemoryHistory = () => {
|
7 | let index = 0;
|
8 | let items = [];
|
9 |
|
10 |
|
11 | const pending = [];
|
12 |
|
13 | const interrupt = () => {
|
14 |
|
15 |
|
16 |
|
17 | pending.forEach(it => {
|
18 | const cb = it.cb;
|
19 |
|
20 | it.cb = () => cb(true);
|
21 | });
|
22 | };
|
23 |
|
24 | const history = {
|
25 | get index() {
|
26 | var _window$history$state;
|
27 |
|
28 |
|
29 |
|
30 | const id = (_window$history$state = window.history.state) === null || _window$history$state === void 0 ? void 0 : _window$history$state.id;
|
31 |
|
32 | if (id) {
|
33 | const index = items.findIndex(item => item.id === id);
|
34 | return index > -1 ? index : 0;
|
35 | }
|
36 |
|
37 | return 0;
|
38 | },
|
39 |
|
40 | get(index) {
|
41 | return items[index];
|
42 | },
|
43 |
|
44 | backIndex({
|
45 | path
|
46 | }) {
|
47 |
|
48 | for (let i = index - 1; i >= 0; i--) {
|
49 | const item = items[i];
|
50 |
|
51 | if (item.path === path) {
|
52 | return i;
|
53 | }
|
54 | }
|
55 |
|
56 | return -1;
|
57 | },
|
58 |
|
59 | push({
|
60 | path,
|
61 | state
|
62 | }) {
|
63 | interrupt();
|
64 | const id = nanoid();
|
65 |
|
66 |
|
67 | items = items.slice(0, index + 1);
|
68 | items.push({
|
69 | path,
|
70 | state,
|
71 | id
|
72 | });
|
73 | index = items.length - 1;
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | window.history.pushState({
|
79 | id
|
80 | }, '', path);
|
81 | },
|
82 |
|
83 | replace({
|
84 | path,
|
85 | state
|
86 | }) {
|
87 | var _window$history$state2, _window$history$state3;
|
88 |
|
89 | interrupt();
|
90 | const id = (_window$history$state2 = (_window$history$state3 = window.history.state) === null || _window$history$state3 === void 0 ? void 0 : _window$history$state3.id) !== null && _window$history$state2 !== void 0 ? _window$history$state2 : nanoid();
|
91 |
|
92 | if (items.length) {
|
93 | items[index] = {
|
94 | path,
|
95 | state,
|
96 | id
|
97 | };
|
98 | } else {
|
99 |
|
100 |
|
101 | items.push({
|
102 | path,
|
103 | state,
|
104 | id
|
105 | });
|
106 | }
|
107 |
|
108 | window.history.replaceState({
|
109 | id
|
110 | }, '', path);
|
111 | },
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | go(n) {
|
119 | interrupt();
|
120 |
|
121 | if (n > 0) {
|
122 |
|
123 | n = Math.min(n, items.length - 1);
|
124 | } else if (n < 0) {
|
125 |
|
126 |
|
127 | n = index + n < 0 ? -index : n;
|
128 | }
|
129 |
|
130 | if (n === 0) {
|
131 | return;
|
132 | }
|
133 |
|
134 | index += n;
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | return new Promise((resolve, reject) => {
|
141 | const done = interrupted => {
|
142 | clearTimeout(timer);
|
143 |
|
144 | if (interrupted) {
|
145 | reject(new Error('History was changed during navigation.'));
|
146 | return;
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | const {
|
158 | title
|
159 | } = window.document;
|
160 | window.document.title = '';
|
161 | window.document.title = title;
|
162 | resolve();
|
163 | };
|
164 |
|
165 | pending.push({
|
166 | ref: done,
|
167 | cb: done
|
168 | });
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | const timer = setTimeout(() => {
|
175 | const index = pending.findIndex(it => it.ref === done);
|
176 |
|
177 | if (index > -1) {
|
178 | pending[index].cb();
|
179 | pending.splice(index, 1);
|
180 | }
|
181 | }, 100);
|
182 |
|
183 | const onPopState = () => {
|
184 | const last = pending.pop();
|
185 | window.removeEventListener('popstate', onPopState);
|
186 | last === null || last === void 0 ? void 0 : last.cb();
|
187 | };
|
188 |
|
189 | window.addEventListener('popstate', onPopState);
|
190 | window.history.go(n);
|
191 | });
|
192 | },
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | listen(listener) {
|
198 | const onPopState = () => {
|
199 | if (pending.length) {
|
200 |
|
201 | return;
|
202 | }
|
203 |
|
204 | listener();
|
205 | };
|
206 |
|
207 | window.addEventListener('popstate', onPopState);
|
208 | return () => window.removeEventListener('popstate', onPopState);
|
209 | }
|
210 |
|
211 | };
|
212 | return history;
|
213 | };
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | const findMatchingState = (a, b) => {
|
221 | if (a === undefined || b === undefined || a.key !== b.key) {
|
222 | return [undefined, undefined];
|
223 | }
|
224 |
|
225 |
|
226 | const aHistoryLength = a.history ? a.history.length : a.routes.length;
|
227 | const bHistoryLength = b.history ? b.history.length : b.routes.length;
|
228 | const aRoute = a.routes[a.index];
|
229 | const bRoute = b.routes[b.index];
|
230 | const aChildState = aRoute.state;
|
231 | const bChildState = bRoute.state;
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | if (aHistoryLength !== bHistoryLength || aRoute.key !== bRoute.key || aChildState === undefined || bChildState === undefined || aChildState.key !== bChildState.key) {
|
238 | return [a, b];
|
239 | }
|
240 |
|
241 | return findMatchingState(aChildState, bChildState);
|
242 | };
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | const series = cb => {
|
249 |
|
250 | let handling = false;
|
251 | let queue = [];
|
252 |
|
253 | const callback = async () => {
|
254 | try {
|
255 | if (handling) {
|
256 |
|
257 |
|
258 | queue.unshift(callback);
|
259 | return;
|
260 | }
|
261 |
|
262 | handling = true;
|
263 | await cb();
|
264 | } finally {
|
265 | handling = false;
|
266 |
|
267 | if (queue.length) {
|
268 |
|
269 | const last = queue.pop();
|
270 | last === null || last === void 0 ? void 0 : last();
|
271 | }
|
272 | }
|
273 | };
|
274 |
|
275 | return callback;
|
276 | };
|
277 |
|
278 | let isUsingLinking = false;
|
279 | export default function useLinking(ref, {
|
280 | independent,
|
281 | enabled = true,
|
282 | config,
|
283 | getStateFromPath = getStateFromPathDefault,
|
284 | getPathFromState = getPathFromStateDefault,
|
285 | getActionFromState = getActionFromStateDefault
|
286 | }) {
|
287 | React.useEffect(() => {
|
288 | if (independent) {
|
289 | return undefined;
|
290 | }
|
291 |
|
292 | if (enabled !== false && isUsingLinking) {
|
293 | throw new Error(['Looks like you have configured linking in multiple places. This is likely an error since URL integration should only be handled in one place to avoid conflicts. Make sure that:', "- You are not using both 'linking' prop and 'useLinking'", "- You don't have 'useLinking' in multiple components"].join('\n').trim());
|
294 | } else {
|
295 | isUsingLinking = enabled !== false;
|
296 | }
|
297 |
|
298 | return () => {
|
299 | isUsingLinking = false;
|
300 | };
|
301 | });
|
302 | const [history] = React.useState(createMemoryHistory);
|
303 |
|
304 |
|
305 |
|
306 | const enabledRef = React.useRef(enabled);
|
307 | const configRef = React.useRef(config);
|
308 | const getStateFromPathRef = React.useRef(getStateFromPath);
|
309 | const getPathFromStateRef = React.useRef(getPathFromState);
|
310 | const getActionFromStateRef = React.useRef(getActionFromState);
|
311 | React.useEffect(() => {
|
312 | enabledRef.current = enabled;
|
313 | configRef.current = config;
|
314 | getStateFromPathRef.current = getStateFromPath;
|
315 | getPathFromStateRef.current = getPathFromState;
|
316 | getActionFromStateRef.current = getActionFromState;
|
317 | });
|
318 | const server = React.useContext(ServerContext);
|
319 | const getInitialState = React.useCallback(() => {
|
320 | let value;
|
321 |
|
322 | if (enabledRef.current) {
|
323 | var _server$location;
|
324 |
|
325 | const location = (_server$location = server === null || server === void 0 ? void 0 : server.location) !== null && _server$location !== void 0 ? _server$location : typeof window !== 'undefined' ? window.location : undefined;
|
326 | const path = location ? location.pathname + location.search : undefined;
|
327 |
|
328 | if (path) {
|
329 | value = getStateFromPathRef.current(path, configRef.current);
|
330 | }
|
331 | }
|
332 |
|
333 | const thenable = {
|
334 | then(onfulfilled) {
|
335 | return Promise.resolve(onfulfilled ? onfulfilled(value) : value);
|
336 | },
|
337 |
|
338 | catch() {
|
339 | return thenable;
|
340 | }
|
341 |
|
342 | };
|
343 | return thenable;
|
344 | }, []);
|
345 | const previousIndexRef = React.useRef(undefined);
|
346 | const previousStateRef = React.useRef(undefined);
|
347 | const pendingPopStatePathRef = React.useRef(undefined);
|
348 | React.useEffect(() => {
|
349 | previousIndexRef.current = history.index;
|
350 | return history.listen(() => {
|
351 | var _previousIndexRef$cur;
|
352 |
|
353 | const navigation = ref.current;
|
354 |
|
355 | if (!navigation || !enabled) {
|
356 | return;
|
357 | }
|
358 |
|
359 | const path = location.pathname + location.search;
|
360 | const index = history.index;
|
361 | const previousIndex = (_previousIndexRef$cur = previousIndexRef.current) !== null && _previousIndexRef$cur !== void 0 ? _previousIndexRef$cur : 0;
|
362 | previousIndexRef.current = index;
|
363 | pendingPopStatePathRef.current = path;
|
364 |
|
365 |
|
366 |
|
367 | const record = history.get(index);
|
368 |
|
369 | if ((record === null || record === void 0 ? void 0 : record.path) === path && record !== null && record !== void 0 && record.state) {
|
370 | navigation.resetRoot(record.state);
|
371 | return;
|
372 | }
|
373 |
|
374 | const state = getStateFromPathRef.current(path, configRef.current);
|
375 |
|
376 |
|
377 | if (state) {
|
378 |
|
379 |
|
380 | const rootState = navigation.getRootState();
|
381 |
|
382 | if (state.routes.some(r => !(rootState !== null && rootState !== void 0 && rootState.routeNames.includes(r.name)))) {
|
383 | console.warn("The navigation state parsed from the URL contains routes not present in the root navigator. This usually means that the linking configuration doesn't match the navigation structure. See https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.");
|
384 | return;
|
385 | }
|
386 |
|
387 | if (index > previousIndex) {
|
388 | const action = getActionFromStateRef.current(state, configRef.current);
|
389 |
|
390 | if (action !== undefined) {
|
391 | try {
|
392 | navigation.dispatch(action);
|
393 | } catch (e) {
|
394 |
|
395 |
|
396 | console.warn(`An error occurred when trying to handle the link '${path}': ${e.message}`);
|
397 | }
|
398 | } else {
|
399 | navigation.resetRoot(state);
|
400 | }
|
401 | } else {
|
402 | navigation.resetRoot(state);
|
403 | }
|
404 | } else {
|
405 |
|
406 | navigation.resetRoot(state);
|
407 | }
|
408 | });
|
409 | }, [enabled, history, ref]);
|
410 | React.useEffect(() => {
|
411 | var _ref$current;
|
412 |
|
413 | if (!enabled) {
|
414 | return;
|
415 | }
|
416 |
|
417 | if (ref.current) {
|
418 |
|
419 |
|
420 | const state = ref.current.getRootState();
|
421 |
|
422 | if (state) {
|
423 | var _route$path;
|
424 |
|
425 | const route = findFocusedRoute(state);
|
426 | const path = (_route$path = route === null || route === void 0 ? void 0 : route.path) !== null && _route$path !== void 0 ? _route$path : getPathFromStateRef.current(state, configRef.current);
|
427 |
|
428 | if (previousStateRef.current === undefined) {
|
429 | previousStateRef.current = state;
|
430 | }
|
431 |
|
432 | history.replace({
|
433 | path,
|
434 | state
|
435 | });
|
436 | }
|
437 | }
|
438 |
|
439 | const onStateChange = async () => {
|
440 | var _route$path2;
|
441 |
|
442 | const navigation = ref.current;
|
443 |
|
444 | if (!navigation || !enabled) {
|
445 | return;
|
446 | }
|
447 |
|
448 | const previousState = previousStateRef.current;
|
449 | const state = navigation.getRootState();
|
450 | const pendingPath = pendingPopStatePathRef.current;
|
451 | const route = findFocusedRoute(state);
|
452 | const path = (_route$path2 = route === null || route === void 0 ? void 0 : route.path) !== null && _route$path2 !== void 0 ? _route$path2 : getPathFromStateRef.current(state, configRef.current);
|
453 | previousStateRef.current = state;
|
454 | pendingPopStatePathRef.current = undefined;
|
455 |
|
456 |
|
457 |
|
458 |
|
459 | const [previousFocusedState, focusedState] = findMatchingState(previousState, state);
|
460 |
|
461 | if (previousFocusedState && focusedState &&
|
462 |
|
463 | path !== pendingPath) {
|
464 | const historyDelta = (focusedState.history ? focusedState.history.length : focusedState.routes.length) - (previousFocusedState.history ? previousFocusedState.history.length : previousFocusedState.routes.length);
|
465 |
|
466 | if (historyDelta > 0) {
|
467 |
|
468 |
|
469 | history.push({
|
470 | path,
|
471 | state
|
472 | });
|
473 | } else if (historyDelta < 0) {
|
474 |
|
475 | const nextIndex = history.backIndex({
|
476 | path
|
477 | });
|
478 | const currentIndex = history.index;
|
479 |
|
480 | try {
|
481 | if (nextIndex !== -1 && nextIndex < currentIndex) {
|
482 |
|
483 | await history.go(nextIndex - currentIndex);
|
484 | } else {
|
485 |
|
486 |
|
487 |
|
488 | await history.go(historyDelta);
|
489 | }
|
490 |
|
491 |
|
492 | history.replace({
|
493 | path,
|
494 | state
|
495 | });
|
496 | } catch (e) {
|
497 | }
|
498 | } else {
|
499 |
|
500 | history.replace({
|
501 | path,
|
502 | state
|
503 | });
|
504 | }
|
505 | } else {
|
506 |
|
507 |
|
508 | history.replace({
|
509 | path,
|
510 | state
|
511 | });
|
512 | }
|
513 | };
|
514 |
|
515 |
|
516 |
|
517 |
|
518 | return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.addListener('state', series(onStateChange));
|
519 | });
|
520 | return {
|
521 | getInitialState
|
522 | };
|
523 | }
|
524 |
|
\ | No newline at end of file |