Skip to content

Commit

Permalink
refactor: tests works / web works / native not :c
Browse files Browse the repository at this point in the history
  • Loading branch information
osdnk committed Oct 29, 2023
1 parent 22c4168 commit cf1bc62
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 203 deletions.
11 changes: 0 additions & 11 deletions packages/core/src/BaseNavigationContainer.tsx
Expand Up @@ -10,7 +10,6 @@ import {
import * as React from 'react';
import useLatestCallback from 'use-latest-callback';

import { usePrevious } from '../utils/usePrevious';
import { checkDuplicateRouteNames } from './checkDuplicateRouteNames';
import { checkSerializable } from './checkSerializable';
import { NOT_INITIALIZED_ERROR } from './createNavigationContainerRef';
Expand Down Expand Up @@ -432,16 +431,6 @@ export const BaseNavigationContainer = React.forwardRef(
}
);

const [stateForNextRouteNamesChange, setStateForNextRouteNamesChange] =
React.useState<Record<string, PartialState<NavigationState>> | null>(
null
);

const previousState = usePrevious(state);

if (state !== previousState && stateForNextRouteNamesChange !== null) {
setStateForNextRouteNamesChange(null);
}
return (
<NavigationIndependentTreeContext.Provider value={false}>
<NavigationContainerRefContext.Provider value={navigation}>
Expand Down
4 changes: 0 additions & 4 deletions packages/native/src/LinkingContext.tsx
Expand Up @@ -6,14 +6,10 @@ const MISSING_CONTEXT_ERROR = "Couldn't find a LinkingContext context.";

export const LinkingContext = React.createContext<{
options?: LinkingOptions<ParamListBase>;
lastUnhandledLinking: React.MutableRefObject<string | null | undefined>;
}>({
get options(): any {
throw new Error(MISSING_CONTEXT_ERROR);
},
get lastUnhandledLinking(): any {
throw new Error(MISSING_CONTEXT_ERROR);
},
});

LinkingContext.displayName = 'LinkingContext';
65 changes: 39 additions & 26 deletions packages/native/src/NavigationContainer.tsx
Expand Up @@ -23,6 +23,7 @@ import type {
LocaleDirection,
Theme,
} from './types';
import { UnhandledLinkingContext } from './UnhandledLinkingContext';
import { useBackButton } from './useBackButton';
import { useDocumentTitle } from './useDocumentTitle';
import { useLinking } from './useLinking';
Expand Down Expand Up @@ -86,7 +87,9 @@ function NavigationContainerInner(
useBackButton(refContainer);
useDocumentTitle(refContainer, documentTitle);

const lastUnhandledLinking = React.useRef<string | undefined>();
const [lastUnhandledLink, setlastUnhandledLink] = React.useState<
string | undefined
>();

const { getInitialState } = useLinking(
refContainer,
Expand All @@ -95,30 +98,38 @@ function NavigationContainerInner(
prefixes: [],
...linking,
},
lastUnhandledLinking
setlastUnhandledLink
);

const linkingContext = React.useMemo(
() => ({ options: linking, lastUnhandledLinking }),
[linking, lastUnhandledLinking]
const linkingContext = React.useMemo(() => ({ options: linking }), [linking]);

const unhandledLinkingContext = React.useMemo(
() => ({ lastUnhandledLink, setlastUnhandledLink }),
[lastUnhandledLink, setlastUnhandledLink]
);

const onReadyForLinkingHandling = useLatestCallback(() => {
// If the screen path matches lastUnhandledLinking, we do not track it
// If the screen path matches lastUnhandledLink, we do not track it
const path = refContainer.current?.getCurrentRoute()?.path;
if (path === linkingContext.lastUnhandledLinking.current) {
linkingContext.lastUnhandledLinking.current = undefined;
}
setlastUnhandledLink((previousLastUnhandledLink) => {
if (previousLastUnhandledLink === path) {
return undefined;
}
return previousLastUnhandledLink;
});
onReady?.();
});

const onStateChangeForLinkingHandling = useLatestCallback(
(state: Readonly<NavigationState> | undefined) => {
// If the screen path matches lastUnhandledLinking, we do not track it
// If the screen path matches lastUnhandledLink, we do not track it
const path = refContainer.current?.getCurrentRoute()?.path;
if (path === linkingContext.lastUnhandledLinking.current) {
linkingContext.lastUnhandledLinking.current = undefined;
}
setlastUnhandledLink((previousLastUnhandledLink) => {
if (previousLastUnhandledLink === path) {
return undefined;
}
return previousLastUnhandledLink;
});
onStateChange?.(state);
}
);
Expand Down Expand Up @@ -157,19 +168,21 @@ function NavigationContainerInner(

return (
<LocaleDirContext.Provider value={direction}>
<LinkingContext.Provider value={linkingContext}>
<ThemeProvider value={theme}>
<BaseNavigationContainer
{...rest}
onReady={onReadyForLinkingHandling}
onStateChange={onStateChangeForLinkingHandling}
initialState={
rest.initialState == null ? initialState : rest.initialState
}
ref={refContainer}
/>
</ThemeProvider>
</LinkingContext.Provider>
<UnhandledLinkingContext.Provider value={unhandledLinkingContext}>
<LinkingContext.Provider value={linkingContext}>
<ThemeProvider value={theme}>
<BaseNavigationContainer
{...rest}
onReady={onReadyForLinkingHandling}
onStateChange={onStateChangeForLinkingHandling}
initialState={
rest.initialState == null ? initialState : rest.initialState
}
ref={refContainer}
/>
</ThemeProvider>
</LinkingContext.Provider>
</UnhandledLinkingContext.Provider>
</LocaleDirContext.Provider>
);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/native/src/UnhandledLinkingContext.tsx
@@ -0,0 +1,19 @@
import * as React from 'react';

const MISSING_CONTEXT_ERROR = "Couldn't find an UnhandledLinkingContext context.";

export const UnhandledLinkingContext = React.createContext<{
lastUnhandledLink: string | undefined;
setlastUnhandledLink: (
lastUnhandledUrl: string | undefined
) => void;
}>({
get lastUnhandledLink(): any {
throw new Error(MISSING_CONTEXT_ERROR);
},
get setlastUnhandledLink(): any {
throw new Error(MISSING_CONTEXT_ERROR);
},
});

UnhandledLinkingContext.displayName = 'UnhandledLinkingContext';
18 changes: 9 additions & 9 deletions packages/native/src/__tests__/useLinking.test.tsx
Expand Up @@ -13,9 +13,9 @@ it('throws if multiple instances of useLinking are used', () => {
const options = { prefixes: [] };

function Sample() {
const lastUnhandledLinking = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLinking);
useLinking(ref, options, lastUnhandledLinking);
const lastUnhandledLink = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLink);
useLinking(ref, options, lastUnhandledLink);
return null;
}

Expand All @@ -33,14 +33,14 @@ it('throws if multiple instances of useLinking are used', () => {
element?.unmount();

function A() {
const lastUnhandledLinking = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLinking);
const lastUnhandledLink = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLink);
return null;
}

function B() {
const lastUnhandledLinking = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLinking);
const lastUnhandledLink = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLink);
return null;
}

Expand All @@ -59,8 +59,8 @@ it('throws if multiple instances of useLinking are used', () => {
element?.unmount();

function Sample2() {
const lastUnhandledLinking = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLinking);
const lastUnhandledLink = React.useRef<string | undefined>();
useLinking(ref, options, lastUnhandledLink);
return null;
}

Expand Down

0 comments on commit cf1bc62

Please sign in to comment.