Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/hooks/useIsOwnWorkspaceChatRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {useRef} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import type * as OnyxTypes from '@src/types/onyx';

/**
* Returns a ref that tracks whether the currently-open report is an own workspace chat.
*
* Must be updated synchronously during render — after a vacation delegate splits an expense
* the server sends an Onyx SET that wipes the report object. By the time any useEffect fires,
* `report` is already undefined, so live state and usePrevious both fail. The ref persists
* the last known value through that wipe window so navigation guards and re-fetch effects
* can still make the correct decision. See issue #84248.
*/
function useIsOwnWorkspaceChatRef(report: OnyxEntry<OnyxTypes.Report> | undefined, reportIDFromRoute: string | undefined) {
const isOwnWorkspaceChatRef = useRef(false);

if (report?.reportID && report.reportID === reportIDFromRoute) {
// Valid, matching report — update the ref.
isOwnWorkspaceChatRef.current = !!report.isOwnPolicyExpenseChat;
} else if (!report?.reportID) {
// Report wiped by Onyx SET — intentionally keep the last known value.
} else {
// Different report loaded — reset.
isOwnWorkspaceChatRef.current = false;
}

return isOwnWorkspaceChatRef;
}

export default useIsOwnWorkspaceChatRef;
19 changes: 19 additions & 0 deletions src/pages/inbox/ReportFetchHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {InteractionManager} from 'react-native';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useIsAnonymousUser from '@hooks/useIsAnonymousUser';
import useIsInSidePanel from '@hooks/useIsInSidePanel';
import useIsOwnWorkspaceChatRef from '@hooks/useIsOwnWorkspaceChatRef';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePaginatedReportActions from '@hooks/usePaginatedReportActions';
Expand Down Expand Up @@ -91,6 +92,9 @@ function ReportFetchHandler() {

const isTransactionThreadView = isReportTransactionThread(report);

// Track whether the current route is an own workspace chat. See issue #84248.
const isCurrentRouteOwnWorkspaceChatRef = useIsOwnWorkspaceChatRef(report, reportIDFromRoute);

const indexOfLinkedMessage = reportActionIDFromRoute ? reportActions.findIndex((obj) => String(obj.reportActionID) === String(reportActionIDFromRoute)) : -1;
const doesCreatedActionExists = !!reportActions?.findLast((action) => isCreatedAction(action));
const isLinkedMessageAvailable = indexOfLinkedMessage > -1;
Expand Down Expand Up @@ -151,6 +155,21 @@ function ReportFetchHandler() {

// Effect order below matches the original declaration order in ReportScreen.tsx.

// When a delegate splits an expense the server sends a temporary Onyx SET that wipes the
// workspace chat. The navigation guards in ReportScreen block any redirect, but the report
// stays blank until something re-fetches it. This effect detects the wipe and re-fetches.
// See issue #84248.
const prevReportID = usePrevious(report?.reportID);
useEffect(() => {
const wasJustWiped = !!prevReportID && prevReportID === reportIDFromRoute && !report?.reportID;
if (!wasJustWiped || !isCurrentRouteOwnWorkspaceChatRef.current) {
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ CONSISTENCY-5 (docs)

The eslint-disable-next-line react-hooks/exhaustive-deps on line 183 lacks a justification comment explaining why certain dependencies (likely fetchReport) are omitted. Even though fetchReport is created via useEffectEvent (and is therefore stable), this should be documented for future readers.

Add a comment above or on the same line, for example:

// fetchReport is a stable useEffectEvent callback and does not need to be listed as a dependency.
// eslint-disable-next-line react-hooks/exhaustive-deps

Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added justification comment above the eslint-disable line. Updated

}
fetchReport();
// fetchReport is a stable useEffectEvent callback and does not need to be listed as a dependency.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [report?.reportID, prevReportID, reportIDFromRoute]);

useEffect(() => {
if (!transactionThreadReportID || !route?.params?.reportActionID || !isOneTransactionThread(childReport, report, linkedAction)) {
return;
Expand Down
22 changes: 21 additions & 1 deletion src/pages/inbox/ReportNavigateAwayHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type {OnyxEntry} from 'react-native-onyx';
import {useCurrentReportIDState} from '@hooks/useCurrentReportID';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useIsOwnWorkspaceChatRef from '@hooks/useIsOwnWorkspaceChatRef';
import useOnyx from '@hooks/useOnyx';
import useParentReportAction from '@hooks/useParentReportAction';
import usePrevious from '@hooks/usePrevious';
Expand Down Expand Up @@ -81,6 +82,11 @@
const isOptimisticDelete = report?.statusNum === CONST.REPORT.STATUS_NUM.CLOSED;
const {wasDeleted: reportWasDeleted, parentReportID: deletedReportParentID} = useReportWasDeleted(reportIDFromRoute, report, isOptimisticDelete, userLeavingStatus);

// Track whether the current route is an own workspace chat. A vacation delegate split sends
// a temporary Onyx SET that wipes the report; by the time effects fire, report is undefined
// so we must persist the value in a ref updated synchronously during render. See issue #84248.
const isCurrentRouteOwnWorkspaceChatRef = useIsOwnWorkspaceChatRef(report, reportIDFromRoute);

const firstRender = useRef(true);

// Navigation action that reads non-reactive context (concierge params, modal state, etc.)
Expand Down Expand Up @@ -138,7 +144,10 @@
isEmpty(report) &&
(isMoneyRequest(prevReport) ||
isMoneyRequestReport(prevReport) ||
isPolicyExpenseChat(prevReport) ||
// Own workspace chats are excluded: a vacation delegate split sends a temporary
// Onyx SET that wipes the report — the chat was never intentionally removed.
// See issue #84248.
(isPolicyExpenseChat(prevReport) && !prevReport?.isOwnPolicyExpenseChat) ||
isGroupChat(prevReport) ||
isAdminRoom(prevReport) ||
isAnnounceRoom(prevReport));
Expand Down Expand Up @@ -194,6 +203,17 @@
return;
}

// For own workspace chats, a vacation delegate split sends a temporary Onyx SET that
// silently wipes the report from Onyx — triggering this effect. We skip navigation here
// entirely: the re-fetch effect in ReportFetchHandler restores the data, which causes
// useReportWasDeleted to reset wasDeleted → false, and this effect exits early on the
// next run. Genuine workspace deletions (e.g. user removed, workspace closed) always
// trigger the "navigate on removal" effect above via userLeavingStatus / didReportClose,
// never via this path alone. See issue #84248.
if (isCurrentRouteOwnWorkspaceChatRef.current) {
return;
}

// Try to navigate to parent report if available
if (deletedReportParentID && !isMoneyRequestReportPendingDeletion(deletedReportParentID)) {
Navigation.isNavigationReady().then(() => {
Expand All @@ -206,7 +226,7 @@
Navigation.isNavigationReady().then(() => {
navigateToConciergeChat(conciergeReportID, introSelected, currentUserAccountID, isSelfTourViewed, betas);
});
}, [reportWasDeleted, isFocused, deletedReportParentID, conciergeReportID, introSelected, currentUserAccountID, isSelfTourViewed, betas]);

Check warning on line 229 in src/pages/inbox/ReportNavigateAwayHandler.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

React Hook useEffect has a missing dependency: 'isCurrentRouteOwnWorkspaceChatRef'. Either include it or remove the dependency array

return null;
}
Expand Down
Loading