Skip to content

Commit

Permalink
fix: better way to find current url
Browse files Browse the repository at this point in the history
  • Loading branch information
mironiasty committed Dec 8, 2023
1 parent c572ed0 commit 7efa788
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
4 changes: 2 additions & 2 deletions example/src/WebView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { useCurrentUrl, useWebviewNavigate } from 'react-native-web-screen';
import { Routes } from './webScreenRoutes';
import Form from './Strada/Form';
import { baseURL } from './webScreen';
import { baseURL, linkingConfig } from './webScreen';

export type Props = { navigation: any } & Pick<VisitableViewProps, 'onMessage'>;

Expand All @@ -19,7 +19,7 @@ const stradaComponents = [Form];
const WebView: React.FC<Props> = ({ navigation, ...props }) => {
const navigateTo = useWebviewNavigate();

const currentUrl = useCurrentUrl(baseURL);
const currentUrl = useCurrentUrl(baseURL, linkingConfig);

const onVisitProposal = ({ action: actionType, url }: VisitProposal) => {
navigateTo(url, actionType);
Expand Down
6 changes: 3 additions & 3 deletions example/src/webScreen.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getLinkingObject } from 'react-native-web-screen';
import { Routes } from './webScreenRoutes';
import { LinkingOptions } from '@react-navigation/native';
import { LinkingConfig } from 'packages/navigation/src/hooks/useCurrentUrl';

const config: LinkingOptions<{}>['config'] = {
export const linkingConfig: LinkingConfig = {
screens: {
[Routes.BottomTabs]: {
screens: {
Expand All @@ -26,4 +26,4 @@ const config: LinkingOptions<{}>['config'] = {

export const baseURL = 'http://localhost:45678/';

export const linking = getLinkingObject(baseURL, config);
export const linking = getLinkingObject(baseURL, linkingConfig);
33 changes: 0 additions & 33 deletions packages/navigation/src/WebScreen.tsx

This file was deleted.

38 changes: 29 additions & 9 deletions packages/navigation/src/hooks/useCurrentUrl.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import { useNavigation } from '@react-navigation/native';
import { LinkingOptions, useNavigation } from '@react-navigation/native';

export function useCurrentUrl(defaultBaseUrl?: string) {
export type LinkingConfig = LinkingOptions<{}>['config'];

function findPath(name: string, config: LinkingConfig): string | undefined {
if (!config) return undefined;
const screens = config.screens;
for (const key of Object.keys(screens)) {
// @ts-expect-error
const pathOrScreen: string | LinkingConfig = screens[key];
if (typeof pathOrScreen === 'string') {
if (key === name) {
return pathOrScreen;
}
} else {
const path = findPath(name, pathOrScreen);
if (path) {
return path;
}
}
}
return undefined;
}

export function useCurrentUrl(baseUrl: string, config: LinkingConfig) {
const navigation = useNavigation();
const state = navigation.getState();

const currentRoute = state.routes[state.index];
const params = currentRoute?.params as
| { path?: string; baseURL?: string }
| undefined;

const path = params?.path ?? '';
const baseURL = params?.baseURL ?? defaultBaseUrl ?? '';
if (currentRoute) {
const path = findPath(currentRoute?.name, config);

return `${baseURL}${path}`;
return `${baseUrl}${path}`;
}
return baseUrl;
}

0 comments on commit 7efa788

Please sign in to comment.