Skip to content

Commit 0d17b3d

Browse files
committed
feat: media bubble handle upload itself
1 parent 058f04e commit 0d17b3d

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

src/chat/ChatScreen.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ import type {
2929
IUserInfo,
3030
MessageProps,
3131
} from '../interfaces';
32-
import { formatMessageText, isOtherUserTyping } from '../utilities';
32+
import {
33+
formatMessageText,
34+
isMediaMessage,
35+
isOtherUserTyping,
36+
} from '../utilities';
3337
import SelectedImageModal from './components/SelectedImage';
3438
import { CustomBubble, CustomImageVideoBubbleProps } from './components/bubble';
3539
import { clearConversation } from '../reducer';
@@ -108,6 +112,7 @@ export const ChatScreen: React.FC<ChatScreenProps> = ({
108112
const [selectedMessage, setSelectedMessage] = useState<MessageProps | null>(
109113
null
110114
);
115+
const [mediaMessages, setMediaMessages] = useState<MessageProps[]>([]);
111116

112117
const conversationRef = useRef<ConversationProps | undefined>(
113118
conversationInfo
@@ -180,11 +185,15 @@ export const ChatScreen: React.FC<ChatScreenProps> = ({
180185
GiftedChat.append(previousMessages, [convertMessage as MessageProps])
181186
);
182187

183-
await firebaseInstance.sendMessage(messages);
188+
if (!isMediaMessage(messages.type)) {
189+
await firebaseInstance.sendMessage(messages);
184190

185-
timeoutMessageRef.current = setTimeout(() => {
186-
sendMessageNotification?.();
187-
}, timeoutSendNotify);
191+
timeoutMessageRef.current = setTimeout(() => {
192+
sendMessageNotification?.();
193+
}, timeoutSendNotify);
194+
} else {
195+
setMediaMessages((prev) => [...prev, messages]);
196+
}
188197
},
189198
[
190199
firebaseInstance,
@@ -325,6 +334,13 @@ export const ChatScreen: React.FC<ChatScreenProps> = ({
325334
isCurrentlyPlaying={
326335
currentPlayingMessageId === bubble.currentMessage?.id
327336
}
337+
mediaMessageIds={mediaMessages?.map((e) => e._id.toString())}
338+
finishUploadCallback={(id) => {
339+
setMediaMessages((prev) =>
340+
prev.filter((message) => message._id !== id)
341+
);
342+
}}
343+
notifyRef={timeoutMessageRef}
328344
/>
329345
);
330346
};

src/chat/components/bubble/CustomBubble.tsx

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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';
33
import { MessageTypes, type MessageProps } from '../../../interfaces';
44
import { Bubble } from 'react-native-gifted-chat';
55
import {
@@ -8,6 +8,8 @@ import {
88
} from './CustomImageVideoBubble';
99
import MessageStatus from '../MessageStatus';
1010
import { CustomBubbleVoice } from './CustomBubbleVoice';
11+
import { formatSendMessage } from '../../../utilities';
12+
import { FirestoreServices } from '../../../services/firebase';
1113

1214
interface CustomBubbleProps {
1315
bubbleMessage: Bubble<MessageProps>['props'];
@@ -23,6 +25,11 @@ interface CustomBubbleProps {
2325
customMessageStatus?: (hasUnread: boolean) => JSX.Element;
2426
onSetCurrentId: (id: string) => void;
2527
isCurrentlyPlaying: boolean;
28+
mediaMessageIds: string[];
29+
finishUploadCallback: (id: string) => void;
30+
sendMessageNotification?: () => void;
31+
timeoutSendNotify?: number;
32+
notifyRef?: React.MutableRefObject<NodeJS.Timeout | null>;
2633
}
2734

2835
export const CustomBubble: React.FC<CustomBubbleProps> = ({
@@ -39,7 +46,15 @@ export const CustomBubble: React.FC<CustomBubbleProps> = ({
3946
customMessageStatus,
4047
onSetCurrentId,
4148
isCurrentlyPlaying,
49+
mediaMessageIds,
50+
finishUploadCallback,
51+
sendMessageNotification,
52+
timeoutSendNotify,
53+
notifyRef,
4254
}) => {
55+
const [isUploading, setIsUploading] = useState(false);
56+
const firebaseInstance = useRef(FirestoreServices.getInstance()).current;
57+
4358
const styleBuble = {
4459
left: { backgroundColor: 'transparent' },
4560
right: { backgroundColor: 'transparent' },
@@ -129,6 +144,57 @@ export const CustomBubble: React.FC<CustomBubbleProps> = ({
129144
}
130145
}
131146
};
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+
132198
return (
133199
<View style={styles.container}>
134200
{bubbleMessage.currentMessage &&

0 commit comments

Comments
 (0)