Skip to content

Instantly share code, notes, and snippets.

@kaushal9678
Last active April 2, 2021 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushal9678/74783476be06fe7ef8196c39b63589fd to your computer and use it in GitHub Desktop.
Save kaushal9678/74783476be06fe7ef8196c39b63589fd to your computer and use it in GitHub Desktop.
A file where I handle all incoming DeepLink url's
const navigation = container.get<INavigationServices>(TYPES.INavigationServices);
export const manageRoutes = (url: string) => {
const route = url.replace(/.*?:\/\//g, '').split('?');
if(url.includes('/jobs-')){
const job = route[0].substring(route[0].lastIndexOf('/') + 1)
const jobSearch = job.split('-').join(" ")
//If you got deeplink for the initial route then just fire a emitter
GlobalEmitter.emit(GlobalEvents.JOB_SEARCH_DEEP_LINK,jobSearch)
}else if (url.includes('/job')) {//Job Detail
const jobId = route[0].substring(route[0].lastIndexOf('/') + 1)
//TODO: Insted of RootNaviation you can use INavigationService
//RootNavigation.navigate('jdp', { jobId: jobId[0] })
navigation.navigateToJDP(jobId, IPathPagePieces.BasicJobResult)
} else if (url.includes('/apply/cb')) {//Apply Link
const jobId = route[0].substring(route[0].lastIndexOf('/') + 1)
navigation.navigateToApply(jobId, IPathPagePieces.SavedJobPage)
} else if (url.includes('/resume/new') || url.includes('/resumes/new') ) {//Add new Resume
const type = route[0].substring(route[0].lastIndexOf('/') + 1)
const resumeManager = container.get<IResumeManager>(TYPES.IResumeManager);
resumeManager.openUploadResumeBS();
} else if (url.includes('/apply')) {//Apply to recommended job internal
const jobId = route[0].substring(route[0].lastIndexOf('/') + 1)
// navigation.navigateToPostApply(jobId:jobId,)
navigation.navigateToPostApply(jobId, false, false, null, null, false, "");
}
}
import DeepLinking from 'react-native-deep-linking';
import { ColorSchemeName, Linking } from "react-native";
import { manageRoutes } from "./DeepLinkNavigation";
export default function Navigation({
colorScheme,
}: {
colorScheme: ColorSchemeName;
}) {
const navigationRef = useRef();
const routeNameRef = useRef();
const handleUrl = ({ url }: { url: string }) => {
Linking.canOpenURL(url).then((supported) => {
if (supported) {
DeepLinking.evaluateUrl(url);
manageRoutes(url)
}
});
};
useEffect(() => {
//Note: If you don't want to use DeepLinking library you can also use react-native listener as per doc.
DeepLinking.addScheme('deeplink://'); //In Deeplinking you have to add your scheme that
//you defined in LinkingConfiguration
Linking.addEventListener('url', handleUrl);
return () => {
Linking.removeEventListener('url', handleUrl);
};
}, []);
return (
<SafeAreaProvider>
<NavigationContainer
uriPrefix={linking.prefixes}
linking={LinkingConfiguration}
theme={colorScheme === "dark" ? ThemeDark : ThemeLight}
ref={navigationRef}
onReady={() =>
(routeNameRef.current = navigationRef.current.getCurrentRoute().name)
}
onStateChange={async () => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef?.current?.getCurrentRoute().name;
if (previousRouteName !== currentRouteName) {// For Firebase Analytics
}
// Save the current route name for later comparison
routeNameRef.current = currentRouteName;
}}
>
<RootNavigator />
</NavigationContainer>
</SafeAreaProvider>
);
}
// A root stack navigator is often used for displaying modals on top of all other content
// Read more here: https://reactnavigation.org/docs/modal
const Stack = createStackNavigator()//<RootStackParamList>();
function RootNavigator() {
return (
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="BottomTabs" component={BottomTabNavigator} />
{/* <Stack.Screen
name="NotFound"
component={NotFoundScreen}
options={{title: 'Oops!'}}
/> */}
</Stack.Navigator>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment