Skip to content

Commit 0717d3f

Browse files
authored
Merge pull request Expensify#66074 from thelullabyy/fix/64548-chat-with-draft-message-disappear
LHN - Chat with draft message disappears when change priority mode
2 parents 10af833 + f4f7f3d commit 0717d3f

3 files changed

Lines changed: 50 additions & 17 deletions

File tree

src/Expensify.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import useDebugShortcut from './hooks/useDebugShortcut';
1818
import useIsAuthenticated from './hooks/useIsAuthenticated';
1919
import useLocalize from './hooks/useLocalize';
2020
import useOnyx from './hooks/useOnyx';
21+
import usePriorityMode from './hooks/usePriorityChange';
2122
import {updateLastRoute} from './libs/actions/App';
2223
import {disconnect} from './libs/actions/Delegate';
2324
import * as EmojiPickerAction from './libs/actions/EmojiPickerAction';
@@ -103,6 +104,7 @@ function Expensify() {
103104
const [lastVisitedPath] = useOnyx(ONYXKEYS.LAST_VISITED_PATH, {canBeMissing: true});
104105

105106
useDebugShortcut();
107+
usePriorityMode();
106108

107109
const [initialUrl, setInitialUrl] = useState<Route | null>(null);
108110

src/hooks/usePriorityChange.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {useEffect} from 'react';
2+
import {openApp} from '@libs/actions/App';
3+
import CONST from '@src/CONST';
4+
import ONYXKEYS from '@src/ONYXKEYS';
5+
import useOnyx from './useOnyx';
6+
import usePrevious from './usePrevious';
7+
8+
function usePriorityMode() {
9+
const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {canBeMissing: true});
10+
const [allReportsWithDraftComments] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, {canBeMissing: true});
11+
const prevPriorityMode = usePrevious(priorityMode);
12+
13+
useEffect(() => {
14+
if (!(prevPriorityMode === CONST.PRIORITY_MODE.GSD && priorityMode === CONST.PRIORITY_MODE.DEFAULT)) {
15+
return;
16+
}
17+
// When a user switches their priority mode away from #focus/GSD we need to call openApp
18+
// to fetch all their chats because #focus mode works with a subset of a user's chats.
19+
openApp(false, allReportsWithDraftComments);
20+
}, [priorityMode, allReportsWithDraftComments, prevPriorityMode]);
21+
}
22+
23+
export default usePriorityMode;

src/libs/actions/App.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type {AppStateStatus} from 'react-native';
44
import {AppState} from 'react-native';
55
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
66
import Onyx from 'react-native-onyx';
7-
import type {ValueOf} from 'type-fest';
87
import {importEmojiLocale} from '@assets/emojis';
98
import * as API from '@libs/API';
109
import type {GetMissingOnyxMessagesParams, HandleRestrictedEventParams, OpenAppParams, OpenOldDotLinkParams, ReconnectAppParams, UpdatePreferredLocaleParams} from '@libs/API/parameters';
@@ -76,19 +75,6 @@ Onyx.connect({
7675
},
7776
});
7877

79-
let priorityMode: ValueOf<typeof CONST.PRIORITY_MODE> | undefined;
80-
Onyx.connect({
81-
key: ONYXKEYS.NVP_PRIORITY_MODE,
82-
callback: (nextPriorityMode) => {
83-
// When someone switches their priority mode we need to fetch all their chats because only #focus mode works with a subset of a user's chats. This is only possible via the OpenApp command.
84-
if (nextPriorityMode === CONST.PRIORITY_MODE.DEFAULT && priorityMode === CONST.PRIORITY_MODE.GSD) {
85-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
86-
openApp();
87-
}
88-
priorityMode = nextPriorityMode;
89-
},
90-
});
91-
9278
let isUsingImportedState: boolean | undefined;
9379
Onyx.connect({
9480
key: ONYXKEYS.IS_USING_IMPORTED_STATE,
@@ -259,7 +245,12 @@ function getPolicyParamsForOpenOrReconnect(): Promise<PolicyParamsForOpenOrRecon
259245
/**
260246
* Returns the Onyx data that is used for both the OpenApp and ReconnectApp API commands.
261247
*/
262-
function getOnyxDataForOpenOrReconnect(isOpenApp = false, isFullReconnect = false, shouldKeepPublicRooms = false): OnyxData {
248+
function getOnyxDataForOpenOrReconnect(
249+
isOpenApp = false,
250+
isFullReconnect = false,
251+
shouldKeepPublicRooms = false,
252+
allReportsWithDraftComments?: Record<string, string | undefined>,
253+
): OnyxData {
263254
const result: OnyxData = {
264255
optimisticData: [
265256
{
@@ -320,16 +311,33 @@ function getOnyxDataForOpenOrReconnect(isOpenApp = false, isFullReconnect = fals
320311
});
321312
}
322313

314+
// Find all reports that have a non-null draft comment and map them to their corresponding report objects from allReports
315+
// This ensures that any report with a draft comment is preserved in Onyx even if it doesn’t contain chat history
316+
const reportsWithDraftComments = Object.entries(allReportsWithDraftComments ?? {})
317+
.filter(([, value]) => value !== null)
318+
.map(([key]) => key.replace(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, ''))
319+
.map((reportID) => allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]);
320+
321+
reportsWithDraftComments?.forEach((report) => {
322+
result.successData?.push({
323+
onyxMethod: Onyx.METHOD.MERGE,
324+
key: `${ONYXKEYS.COLLECTION.REPORT}${report?.reportID}`,
325+
value: {
326+
...report,
327+
},
328+
});
329+
});
330+
323331
return result;
324332
}
325333

326334
/**
327335
* Fetches data needed for app initialization
328336
*/
329-
function openApp(shouldKeepPublicRooms = false) {
337+
function openApp(shouldKeepPublicRooms = false, allReportsWithDraftComments?: Record<string, string | undefined>) {
330338
return getPolicyParamsForOpenOrReconnect().then((policyParams: PolicyParamsForOpenOrReconnect) => {
331339
const params: OpenAppParams = {enablePriorityModeFilter: true, ...policyParams};
332-
return API.writeWithNoDuplicatesConflictAction(WRITE_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true, undefined, shouldKeepPublicRooms));
340+
return API.writeWithNoDuplicatesConflictAction(WRITE_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true, undefined, shouldKeepPublicRooms, allReportsWithDraftComments));
333341
});
334342
}
335343

0 commit comments

Comments
 (0)