diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index f3f3c7a2b195..3d1cf2622a46 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -386,7 +386,14 @@ function ReportScreen({route, navigation}: ReportScreenProps) { () => !!linkedAction && !shouldReportActionBeVisible(linkedAction, linkedAction.reportActionID, canUserPerformWriteAction(report)), [linkedAction, report], ); + const prevIsLinkedActionDeleted = usePrevious(linkedAction ? isLinkedActionDeleted : undefined); + + // eslint-disable-next-line react-compiler/react-compiler + const lastReportActionIDFromRoute = usePrevious(!firstRenderRef.current ? reportActionIDFromRoute : undefined); + + const [isNavigatingToDeletedAction, setIsNavigatingToDeletedAction] = useState(false); + const isLinkedActionInaccessibleWhisper = useMemo( () => !!linkedAction && isWhisperAction(linkedAction) && !(linkedAction?.whisperedToAccountIDs ?? []).includes(currentUserAccountID), [currentUserAccountID, linkedAction], @@ -416,11 +423,9 @@ function ReportScreen({route, navigation}: ReportScreenProps) { (!!deleteTransactionNavigateBackUrl && getReportIDFromLink(deleteTransactionNavigateBackUrl) === report?.reportID) || (!reportMetadata.isOptimisticReport && isLoading); - const isLinkedActionBecomesDeleted = prevIsLinkedActionDeleted !== undefined && !prevIsLinkedActionDeleted && isLinkedActionDeleted; - // eslint-disable-next-line rulesdir/no-negated-variables const shouldShowNotFoundLinkedAction = - (!isLinkedActionInaccessibleWhisper && isLinkedActionDeleted && !isLinkedActionBecomesDeleted) || + (!isLinkedActionInaccessibleWhisper && isLinkedActionDeleted && isNavigatingToDeletedAction) || (shouldShowSkeleton && !reportMetadata.isLoadingInitialReportActions && !!reportActionIDFromRoute && @@ -735,13 +740,23 @@ function ReportScreen({route, navigation}: ReportScreenProps) { }, [fetchReport]); useEffect(() => { - // If the linked action is previously available but now deleted, - // remove the reportActionID from the params to not link to the deleted action. - if (!isLinkedActionBecomesDeleted) { + // Only handle deletion cases when there's a deleted action + if (!isLinkedActionDeleted) { + setIsNavigatingToDeletedAction(false); return; } - Navigation.setParams({reportActionID: ''}); - }, [isLinkedActionBecomesDeleted]); + + // we want to do this destinguish between normal navigation and delete behavior + if (lastReportActionIDFromRoute !== reportActionIDFromRoute) { + setIsNavigatingToDeletedAction(true); + return; + } + + // Clear params when Action gets deleted while heighlighted + if (!isNavigatingToDeletedAction && prevIsLinkedActionDeleted === false) { + Navigation.setParams({reportActionID: ''}); + } + }, [isLinkedActionDeleted, prevIsLinkedActionDeleted, lastReportActionIDFromRoute, reportActionIDFromRoute, isNavigatingToDeletedAction]); // If user redirects to an inaccessible whisper via a deeplink, on a report they have access to, // then we set reportActionID as empty string, so we display them the report and not the "Not found page". @@ -775,7 +790,6 @@ function ReportScreen({route, navigation}: ReportScreenProps) { !isDeletedAction(mostRecentReportAction); const lastRoute = usePrevious(route); - const lastReportActionIDFromRoute = usePrevious(reportActionIDFromRoute); const onComposerFocus = useCallback(() => setIsComposerFocus(true), []); const onComposerBlur = useCallback(() => setIsComposerFocus(false), []);