1
- import React from 'react' ;
2
- import { StyleProp , StyleSheet , Text , View , ViewStyle } from 'react-native' ;
1
+ import React , { useCallback , useEffect , useRef , useState } from 'react' ;
2
+ import { StyleProp , StyleSheet , View , ViewStyle } from 'react-native' ;
3
3
import { MessageTypes , type MessageProps } from '../../../interfaces' ;
4
4
import { Bubble } from 'react-native-gifted-chat' ;
5
5
import {
8
8
} from './CustomImageVideoBubble' ;
9
9
import MessageStatus from '../MessageStatus' ;
10
10
import { CustomBubbleVoice } from './CustomBubbleVoice' ;
11
+ import { formatSendMessage } from '../../../utilities' ;
12
+ import { FirestoreServices } from '../../../services/firebase' ;
11
13
12
14
interface CustomBubbleProps {
13
15
bubbleMessage : Bubble < MessageProps > [ 'props' ] ;
@@ -23,6 +25,11 @@ interface CustomBubbleProps {
23
25
customMessageStatus ?: ( hasUnread : boolean ) => JSX . Element ;
24
26
onSetCurrentId : ( id : string ) => void ;
25
27
isCurrentlyPlaying : boolean ;
28
+ mediaMessageIds : string [ ] ;
29
+ finishUploadCallback : ( id : string ) => void ;
30
+ sendMessageNotification ?: ( ) => void ;
31
+ timeoutSendNotify ?: number ;
32
+ notifyRef ?: React . MutableRefObject < NodeJS . Timeout | null > ;
26
33
}
27
34
28
35
export const CustomBubble : React . FC < CustomBubbleProps > = ( {
@@ -39,7 +46,15 @@ export const CustomBubble: React.FC<CustomBubbleProps> = ({
39
46
customMessageStatus,
40
47
onSetCurrentId,
41
48
isCurrentlyPlaying,
49
+ mediaMessageIds,
50
+ finishUploadCallback,
51
+ sendMessageNotification,
52
+ timeoutSendNotify,
53
+ notifyRef,
42
54
} ) => {
55
+ const [ isUploading , setIsUploading ] = useState ( false ) ;
56
+ const firebaseInstance = useRef ( FirestoreServices . getInstance ( ) ) . current ;
57
+
43
58
const styleBuble = {
44
59
left : { backgroundColor : 'transparent' } ,
45
60
right : { backgroundColor : 'transparent' } ,
@@ -129,6 +144,57 @@ export const CustomBubble: React.FC<CustomBubbleProps> = ({
129
144
}
130
145
}
131
146
} ;
147
+
148
+ const uploadFileToStorage = useCallback ( async ( ) => {
149
+ if ( bubbleMessage . currentMessage ) {
150
+ const { text, type, path, extension, fileName, size, duration } =
151
+ bubbleMessage . currentMessage || { } ;
152
+ const messageData = formatSendMessage (
153
+ firebaseInstance . userId ,
154
+ text ,
155
+ type ,
156
+ path ,
157
+ extension ,
158
+ fileName ,
159
+ size ,
160
+ duration
161
+ ) ;
162
+
163
+ await firebaseInstance . sendMessageWithFile ( messageData ) ;
164
+ finishUploadCallback ( bubbleMessage . currentMessage . _id . toString ( ) ) ;
165
+ setIsUploading ( false ) ;
166
+
167
+ if ( notifyRef ) {
168
+ notifyRef . current = setTimeout ( ( ) => {
169
+ sendMessageNotification ?.( ) ;
170
+ } , timeoutSendNotify ) ;
171
+ }
172
+ }
173
+ } , [
174
+ bubbleMessage . currentMessage ,
175
+ finishUploadCallback ,
176
+ firebaseInstance ,
177
+ notifyRef ,
178
+ sendMessageNotification ,
179
+ timeoutSendNotify ,
180
+ ] ) ;
181
+
182
+ useEffect ( ( ) => {
183
+ if (
184
+ ! isUploading &&
185
+ bubbleMessage . currentMessage ?. _id &&
186
+ mediaMessageIds . includes ( bubbleMessage . currentMessage . _id . toString ( ) )
187
+ ) {
188
+ setIsUploading ( true ) ;
189
+ uploadFileToStorage ( ) ;
190
+ }
191
+ } , [
192
+ bubbleMessage ?. currentMessage ?. _id ,
193
+ isUploading ,
194
+ mediaMessageIds ,
195
+ uploadFileToStorage ,
196
+ ] ) ;
197
+
132
198
return (
133
199
< View style = { styles . container } >
134
200
{ bubbleMessage . currentMessage &&
0 commit comments