native_convex_CRM/app/utils/navigationRef.ts

45 lines
1.3 KiB
TypeScript

import { createRef } from 'react';
import { NavigationContainerRef, CommonActions } from '@react-navigation/native';
// Shared ref — attach to <NavigationContainer ref={navigationRef}>
export const navigationRef =
createRef<NavigationContainerRef<ReactNavigation.RootParamList>>();
// Queued leadId when app launches from a killed state (navigator not ready yet)
let pendingLeadId: string | null = null;
// Navigate to LeadDetails. If navigator isn't ready yet, queue it for onReady.
export function navigateToLeadDetails(leadId: string): void {
if (!navigationRef.current?.isReady()) {
pendingLeadId = leadId;
return;
}
navigationRef.current.dispatch(
CommonActions.navigate({
name: 'DrawerStack',
params: {
screen: 'home',
params: {
screen: 'leads',
params: {
// Seed the LeadsStack with leadsList first so the back button
// is always present when arriving from a notification.
initial: false,
screen: 'leadDetails',
params: { lead: { id: leadId } },
},
},
},
}),
);
}
// Call from NavigationContainer's onReady — executes any queued navigation
export function flushPendingNavigation(): void {
if (pendingLeadId) {
navigateToLeadDetails(pendingLeadId);
pendingLeadId = null;
}
}