Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 1x 18x 18x 18x 18x 18x 18x 5x 1x 1x 4x 4x 4x 4x 3x 1x 18x 18x 18x 10x 9x 9x 1x 1x 8x 1x 1x 7x 5x 5x 5x 2x 2x 1x 1x 1x 18x 16x 15x 15x 9x 8x 1x 1x 1x 1x 18x 18x 18x 9x 9x 18x 18x 18x 18x 12x 10x 9x 2x 6x 5x 18x 4x 4x 4x 2x 1x 1x 1x 1x 1x 4x 18x 18x 18x 18x | import { useCallback, useEffect, useRef } from 'react';
import { AppState, AppStateStatus, InteractionManager } from 'react-native';
import { SCREENS } from '@repo/constants/screens';
import { useDisabledFeatures } from '@repo/hooks/useFlags';
import { useDeepLinkContext } from '@/contexts/DeepLinkContext';
import { buildDeepLinkFromNotification } from '@/hooks/deeplink';
import { NOTIFICATION_LOG } from '@/services/notification/constants';
import { extractNotificationData } from '@/services/notification/helpers';
import { NotificationMessages } from '@/services/notification/messages';
import { notificationService } from '@/services/notification/notificationService';
import { sentryService } from '@/services/sentryService';
export const useNavigateNotifications = () => {
const { navigateFromDeepLink, navigationRef, isNavigationReady } = useDeepLinkContext();
const { value: disableFeatures } = useDisabledFeatures();
const cancelledRef = useRef(false); // Track if component is unmounted
const appStateRef = useRef<AppStateStatus>(AppState.currentState); // Track previous app state
const pendingNavigationRef = useRef<{ ticketId: string; shouldRefresh?: boolean } | null>(null); // Queue navigation until nav is ready
// Background navigation
const navigateDirectly = useCallback(
(ticketId: string, shouldRefresh = true) => {
if (!navigationRef?.current) {
pendingNavigationRef.current = { ticketId, shouldRefresh };
return;
}
try {
InteractionManager.runAfterInteractions(() => {
Eif (!cancelledRef.current && navigationRef?.current) {
navigationRef.current.navigate(SCREENS.REQUEST_DETAIL, {
id: ticketId,
fromDeepLink: true,
refreshTimeStamp: shouldRefresh ? Date.now() : undefined,
});
pendingNavigationRef.current = null;
}
});
} catch (error) {
sentryService.captureException(error, {
tags: {
component: NOTIFICATION_LOG.COMPONENT,
action: NOTIFICATION_LOG.ACTIONS.DIRECT_NAVIGATE,
},
extra: { ticketId },
});
}
},
[navigationRef],
);
// Nav when ready
useEffect(() => {
Iif (isNavigationReady && pendingNavigationRef.current && !cancelledRef.current) {
const { ticketId, shouldRefresh } = pendingNavigationRef.current;
InteractionManager.runAfterInteractions(() => {
if (!cancelledRef.current && navigationRef?.current) {
navigationRef.current.navigate(SCREENS.REQUEST_DETAIL, {
id: ticketId,
fromDeepLink: true,
refreshTimeStamp: shouldRefresh ? Date.now() : undefined,
});
pendingNavigationRef.current = null;
}
});
}
}, [isNavigationReady, navigationRef]);
// Main processor
const processPendingNotification = useCallback(
(data?: Record<string, unknown>) => {
if (cancelledRef.current || !data) return;
const notificationData = extractNotificationData(data);
if (!notificationData) {
sentryService.captureException(new Error(NotificationMessages.NOTIFICATION_INVALID_DATA), {
tags: {
component: NOTIFICATION_LOG.COMPONENT,
action: NOTIFICATION_LOG.ACTIONS.EXTRACT_DATA,
},
extra: { data },
});
return;
}
// Filter out notifications for disabled features
if (disableFeatures[notificationData.category]) {
// Silently ignore notifications for disabled features
notificationService.clearPendingNotificationData();
return;
}
// Handle ticket navigate
if (notificationData.ticketId) {
// Only refresh if the notification hasn't already triggered a foreground refresh
const shouldRefresh = !notificationData._alreadyRefreshed;
navigateDirectly(notificationData.ticketId, shouldRefresh);
return;
}
const deepLink = buildDeepLinkFromNotification(notificationData);
if (!deepLink) {
sentryService.captureException(new Error(NotificationMessages.NOTIFICATION_INVALID_DATA), {
tags: {
component: NOTIFICATION_LOG.COMPONENT,
action: NOTIFICATION_LOG.ACTIONS.BUILD_DEEP_LINK,
},
extra: { notificationData },
});
return;
}
navigateFromDeepLink(deepLink);
},
[navigateFromDeepLink, navigateDirectly, disableFeatures],
);
const checkAndProcessPending = useCallback(async () => {
if (!isNavigationReady || !navigationRef?.current) return;
const pendingData = notificationService.getPendingNotificationData();
if (pendingData && !cancelledRef.current) {
await new Promise(resolve => setTimeout(resolve, 100)); // Small delay for app activity
if (!cancelledRef.current && navigationRef?.current) {
InteractionManager.runAfterInteractions(() => {
Eif (!cancelledRef.current && navigationRef?.current) {
processPendingNotification(pendingData);
notificationService.clearPendingNotificationData();
}
});
}
}
}, [isNavigationReady, navigationRef, processPendingNotification]);
useEffect(() => {
cancelledRef.current = false;
const handleNotificationTap = (data?: Record<string, unknown>) => {
Iif (cancelledRef.current) return;
processPendingNotification(data);
};
notificationService.setOnNotificationTap(handleNotificationTap);
// Initial check (only when app opens from killed state, not when already running)
// Only check if app was not already active (meaning it was killed/closed)
const initialAppState = appStateRef.current;
const isAppOpeningFromKilledState = initialAppState !== 'active';
if (isAppOpeningFromKilledState && isNavigationReady && !cancelledRef.current) {
notificationService
.getInitialNotification()
.then(() => {
if (isNavigationReady && !cancelledRef.current) {
checkAndProcessPending();
}
})
.catch(error => {
sentryService.captureException(error, {
tags: {
component: NOTIFICATION_LOG.COMPONENT,
action: NOTIFICATION_LOG.ACTIONS.CHECK_INITIAL_MOUNT,
},
});
});
} else if (isNavigationReady && !cancelledRef.current) {
// Also check for pending data if app is already active (e.g. hook remount)
checkAndProcessPending();
}
// Handle app transition from background to active (when user clicks notification in background)
const subscription = AppState.addEventListener('change', (nextAppState: AppStateStatus) => {
const wasInBackground = appStateRef.current.match(/inactive|background/);
const isNowActive = nextAppState === 'active';
// Only navigate when app comes from background to active (user clicked notification)
// Not when app is already active (already running)
if (wasInBackground && isNowActive && !cancelledRef.current) {
notificationService
.getInitialNotification()
.then(() => {
Eif (!cancelledRef.current) {
InteractionManager.runAfterInteractions(() => {
Eif (!cancelledRef.current) checkAndProcessPending();
});
}
})
.catch(error => {
sentryService.captureException(error, {
tags: {
component: NOTIFICATION_LOG.COMPONENT,
action: NOTIFICATION_LOG.ACTIONS.CHECK_INITIAL_FOREGROUND,
},
});
Eif (!cancelledRef.current) checkAndProcessPending();
});
}
appStateRef.current = nextAppState;
});
return () => {
cancelledRef.current = true;
subscription.remove();
notificationService.setOnNotificationTap(undefined as any);
};
}, [processPendingNotification, checkAndProcessPending, isNavigationReady]);
};
|