Skip to content

Commit a49a301

Browse files
authored
Merge pull request #43584 from wildan-m/wildan/fix/40198-create-group-image-lost
Fix create group image lost when modify member
2 parents 93b2a74 + 4a042be commit a49a301

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/pages/NewChatConfirmPage.tsx

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback, useMemo, useRef} from 'react';
1+
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
22
import {View} from 'react-native';
33
import {withOnyx} from 'react-native-onyx';
44
import type {OnyxEntry} from 'react-native-onyx';
@@ -15,6 +15,7 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
1515
import useLocalize from '@hooks/useLocalize';
1616
import useThemeStyles from '@hooks/useThemeStyles';
1717
import type {CustomRNImageManipulatorResult} from '@libs/cropOrRotateImage/types';
18+
import * as FileUtils from '@libs/fileDownload/FileUtils';
1819
import Navigation from '@libs/Navigation/Navigation';
1920
import * as OptionsListUtils from '@libs/OptionsListUtils';
2021
import * as ReportUtils from '@libs/ReportUtils';
@@ -45,7 +46,7 @@ function navigateToEditChatName() {
4546

4647
function NewChatConfirmPage({newGroupDraft, allPersonalDetails}: NewChatConfirmPageProps) {
4748
const optimisticReportID = useRef<string>(ReportUtils.generateReportID());
48-
const fileRef = useRef<File | CustomRNImageManipulatorResult | undefined>();
49+
const [avatarFile, setAvatarFile] = useState<File | CustomRNImageManipulatorResult | undefined>();
4950
const {translate} = useLocalize();
5051
const styles = useThemeStyles();
5152
const personalData = useCurrentUserPersonalDetails();
@@ -104,10 +105,33 @@ function NewChatConfirmPage({newGroupDraft, allPersonalDetails}: NewChatConfirmP
104105
}
105106

106107
const logins: string[] = (newGroupDraft.participants ?? []).map((participant) => participant.login);
107-
Report.navigateToAndOpenReport(logins, true, newGroupDraft.reportName ?? '', newGroupDraft.avatarUri ?? '', fileRef.current, optimisticReportID.current, true);
108-
}, [newGroupDraft]);
108+
Report.navigateToAndOpenReport(logins, true, newGroupDraft.reportName ?? '', newGroupDraft.avatarUri ?? '', avatarFile, optimisticReportID.current, true);
109+
}, [newGroupDraft, avatarFile]);
109110

110111
const stashedLocalAvatarImage = newGroupDraft?.avatarUri;
112+
113+
useEffect(() => {
114+
if (!stashedLocalAvatarImage) {
115+
return;
116+
}
117+
118+
const onSuccess = (file: File) => {
119+
setAvatarFile(file);
120+
};
121+
122+
const onFailure = () => {
123+
setAvatarFile(undefined);
124+
Report.setGroupDraft({avatarUri: null, avatarFileName: null, avatarFileType: null});
125+
};
126+
127+
// If the user navigates back to the member selection page and then returns to the confirmation page, the component will re-mount, causing avatarFile to be null.
128+
// To handle this, we re-read the avatar image file from disk whenever the component re-mounts.
129+
FileUtils.readFileAsync(stashedLocalAvatarImage, newGroupDraft?.avatarFileName ?? '', onSuccess, onFailure, newGroupDraft?.avatarFileType ?? '');
130+
131+
// we only need to run this when the component re-mounted
132+
// eslint-disable-next-line react-hooks/exhaustive-deps
133+
}, []);
134+
111135
return (
112136
<ScreenWrapper testID={NewChatConfirmPage.displayName}>
113137
<HeaderWithBackButton
@@ -119,12 +143,12 @@ function NewChatConfirmPage({newGroupDraft, allPersonalDetails}: NewChatConfirmP
119143
isUsingDefaultAvatar={!stashedLocalAvatarImage}
120144
source={stashedLocalAvatarImage ?? ReportUtils.getDefaultGroupAvatar(optimisticReportID.current)}
121145
onImageSelected={(image) => {
122-
fileRef.current = image;
123-
Report.setGroupDraft({avatarUri: image?.uri ?? ''});
146+
setAvatarFile(image);
147+
Report.setGroupDraft({avatarUri: image.uri ?? '', avatarFileName: image.name ?? '', avatarFileType: image.type});
124148
}}
125149
onImageRemoved={() => {
126-
fileRef.current = undefined;
127-
Report.setGroupDraft({avatarUri: null});
150+
setAvatarFile(undefined);
151+
Report.setGroupDraft({avatarUri: null, avatarFileName: null, avatarFileType: null});
128152
}}
129153
size={CONST.AVATAR_SIZE.XLARGE}
130154
avatarStyle={styles.avatarXLarge}

src/types/onyx/NewGroupChatDraft.ts

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ type NewGroupChatDraft = {
1717

1818
/** New group chat avatar URI */
1919
avatarUri: string | null;
20+
21+
/** New group chat avatar file name */
22+
avatarFileName: string | null;
23+
24+
/** New group chat avatar file type */
25+
avatarFileType: string | null;
2026
};
2127
export type {SelectedParticipant};
2228
export default NewGroupChatDraft;

0 commit comments

Comments
 (0)