Skip to content

Commit

Permalink
chore(suite-native): simplify connect popup deeplink callback
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Feb 5, 2025
1 parent 0e80d40 commit 1b30739
Showing 1 changed file with 11 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect } from 'react';
import { useEffect } from 'react';

import { useNavigation } from '@react-navigation/native';
import * as Linking from 'expo-linking';
Expand Down Expand Up @@ -34,33 +34,16 @@ export const useConnectPopupNavigation = () => {
const featureFlagEnabled = useFeatureFlag(FeatureFlag.IsConnectPopupEnabled);
const navigation = useNavigation<NavigationProp>();

const navigateToConnectPopup = useCallback(
(url: string) => {
if (!featureFlagEnabled) return;
if (!isConnectPopupUrl(url)) return;
const parsedUrl = Linking.parse(url);
navigation.navigate(RootStackRoutes.ConnectPopup, { parsedUrl });
},
[navigation, featureFlagEnabled],
);

useEffect(() => {
const navigateToInitalUrl = async () => {
const currentUrl = await Linking.getInitialURL();
if (currentUrl) {
navigateToConnectPopup(currentUrl);
}
};
navigateToInitalUrl();
}, [navigateToConnectPopup]);
const url = Linking.useURL();

useEffect(() => {
// there could be when you open same deep link for second time and in that case it will be ignored
// this could be probably handed by Linking.addEventListener
const subscription = Linking.addEventListener('url', event => {
navigateToConnectPopup(event.url);
});

return () => subscription?.remove();
}, [navigateToConnectPopup]);
if (!featureFlagEnabled) return;
if (!url || !isConnectPopupUrl(url)) return;
try {
const parsedUrl = Linking.parse(url);
navigation.navigate(RootStackRoutes.ConnectPopup, { parsedUrl });
} catch (error) {
console.warn('Invalid deeplink URL', { error, url });
}
}, [url, navigation, featureFlagEnabled]);
};

0 comments on commit 1b30739

Please sign in to comment.