1
- import React , { useCallback , useMemo , useRef } from 'react' ;
1
+ import React , { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
2
2
import { View } from 'react-native' ;
3
3
import { withOnyx } from 'react-native-onyx' ;
4
4
import type { OnyxEntry } from 'react-native-onyx' ;
@@ -15,6 +15,7 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
15
15
import useLocalize from '@hooks/useLocalize' ;
16
16
import useThemeStyles from '@hooks/useThemeStyles' ;
17
17
import type { CustomRNImageManipulatorResult } from '@libs/cropOrRotateImage/types' ;
18
+ import * as FileUtils from '@libs/fileDownload/FileUtils' ;
18
19
import Navigation from '@libs/Navigation/Navigation' ;
19
20
import * as OptionsListUtils from '@libs/OptionsListUtils' ;
20
21
import * as ReportUtils from '@libs/ReportUtils' ;
@@ -45,7 +46,7 @@ function navigateToEditChatName() {
45
46
46
47
function NewChatConfirmPage ( { newGroupDraft, allPersonalDetails} : NewChatConfirmPageProps ) {
47
48
const optimisticReportID = useRef < string > ( ReportUtils . generateReportID ( ) ) ;
48
- const fileRef = useRef < File | CustomRNImageManipulatorResult | undefined > ( ) ;
49
+ const [ avatarFile , setAvatarFile ] = useState < File | CustomRNImageManipulatorResult | undefined > ( ) ;
49
50
const { translate} = useLocalize ( ) ;
50
51
const styles = useThemeStyles ( ) ;
51
52
const personalData = useCurrentUserPersonalDetails ( ) ;
@@ -104,10 +105,33 @@ function NewChatConfirmPage({newGroupDraft, allPersonalDetails}: NewChatConfirmP
104
105
}
105
106
106
107
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 ] ) ;
109
110
110
111
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
+
111
135
return (
112
136
< ScreenWrapper testID = { NewChatConfirmPage . displayName } >
113
137
< HeaderWithBackButton
@@ -119,12 +143,12 @@ function NewChatConfirmPage({newGroupDraft, allPersonalDetails}: NewChatConfirmP
119
143
isUsingDefaultAvatar = { ! stashedLocalAvatarImage }
120
144
source = { stashedLocalAvatarImage ?? ReportUtils . getDefaultGroupAvatar ( optimisticReportID . current ) }
121
145
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 } ) ;
124
148
} }
125
149
onImageRemoved = { ( ) => {
126
- fileRef . current = undefined ;
127
- Report . setGroupDraft ( { avatarUri : null } ) ;
150
+ setAvatarFile ( undefined ) ;
151
+ Report . setGroupDraft ( { avatarUri : null , avatarFileName : null , avatarFileType : null } ) ;
128
152
} }
129
153
size = { CONST . AVATAR_SIZE . XLARGE }
130
154
avatarStyle = { styles . avatarXLarge }
0 commit comments