12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300 |
- import React, { useState, useCallback, useEffect, useRef } from 'react';
- import {
- View,
- TouchableOpacity,
- Image,
- Text,
- FlatList,
- Dimensions,
- Alert,
- ScrollView,
- Linking,
- ActivityIndicator,
- AppState,
- AppStateStatus,
- TextInput,
- Platform
- } from 'react-native';
- import {
- GiftedChat,
- Bubble,
- InputToolbar,
- IMessage,
- Send,
- BubbleProps,
- Composer,
- TimeProps,
- MessageProps,
- Actions,
- isSameUser,
- isSameDay,
- SystemMessage,
- MessageText
- } from 'react-native-gifted-chat';
- import { MaterialCommunityIcons } from '@expo/vector-icons';
- import { GestureHandlerRootView, Swipeable } from 'react-native-gesture-handler';
- import { Header, WarningModal } from 'src/components';
- import { Colors } from 'src/theme';
- import { useFocusEffect, useNavigation } from '@react-navigation/native';
- import { Audio } from 'expo-av';
- import ChatMessageBox from '../Components/ChatMessageBox';
- import ReplyMessageBar from '../Components/ReplyMessageBar';
- import { useSharedValue, withTiming } from 'react-native-reanimated';
- import { BlurView } from 'expo-blur';
- import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context';
- import Clipboard from '@react-native-clipboard/clipboard';
- import { trigger } from 'react-native-haptic-feedback';
- import ReactModal from 'react-native-modal';
- import { storage, StoreType } from 'src/storage';
- import {
- usePostGetGroupChatQuery,
- usePostSendGroupMessageMutation,
- usePostReactToGroupMessageMutation,
- usePostGroupMessagesReadMutation,
- usePostDeleteGroupMessageMutation,
- usePostGetPinnedGroupMessageQuery,
- usePostSetPinGroupMessageMutation,
- usePostGetGroupSettingsQuery,
- usePostGetGroupMembersQuery
- } from '@api/chat';
- import { CustomMessage, GroupMessage, Reaction } from '../types';
- import { API_HOST, APP_VERSION, WEBSOCKET_URL } from 'src/constants';
- import ReactionBar from '../Components/ReactionBar';
- import OptionsMenu from '../Components/OptionsMenu';
- import EmojiSelectorModal from '../Components/EmojiSelectorModal';
- import TypingIndicator from '../Components/TypingIndicator';
- import { styles } from '../ChatScreen/styles';
- import SendIcon from 'assets/icons/messages/send.svg';
- import { SheetManager } from 'react-native-actions-sheet';
- import { NAVIGATION_PAGES } from 'src/types';
- import { usePushNotification } from 'src/contexts/PushNotificationContext';
- import ReactionsListModal from '../Components/ReactionsListModal';
- import { dismissChatNotifications } from '../utils';
- import { useMessagesStore } from 'src/stores/unreadMessagesStore';
- import FileViewer from 'react-native-file-viewer';
- import * as FileSystem from 'expo-file-system';
- import ImageView from 'better-react-native-image-viewing';
- import * as MediaLibrary from 'expo-media-library';
- import BanIcon from 'assets/icons/messages/ban.svg';
- import AttachmentsModal from '../Components/AttachmentsModal';
- import RenderMessageVideo from '../Components/renderMessageVideo';
- import MessageLocation from '../Components/MessageLocation';
- import GroupIcon from 'assets/icons/messages/group-chat.svg';
- import { CACHED_ATTACHMENTS_DIR } from 'src/constants/constants';
- import GroupStatusModal from '../Components/GroupStatusModal';
- import PinIcon from 'assets/icons/messages/pin.svg';
- import MentionsList from '../Components/MentionsList';
- const options = {
- enableVibrateFallback: true,
- ignoreAndroidSystemSettings: false
- };
- const reactionEmojis = ['👍', '❤️', '😂', '😮', '😭'];
- const GroupChatScreen = ({ route }: { route: any }) => {
- const token = storage.get('token', StoreType.STRING) as string;
- const {
- group_token,
- name,
- avatar,
- userType = 'normal'
- }: {
- group_token: string;
- name: string;
- avatar: string | null;
- userType: 'normal' | 'not_exist' | 'blocked';
- } = route.params;
- const groupName =
- userType === 'blocked'
- ? 'Account is blocked'
- : userType === 'not_exist'
- ? 'Account does not exist'
- : name;
- const currentUserId = storage.get('uid', StoreType.STRING) as number;
- const insets = useSafeAreaInsets();
- const [groupAvatar, setGroupAvatar] = useState<string | null>(null);
- const [messages, setMessages] = useState<CustomMessage[] | null>();
- const navigation = useNavigation();
- const [prevThenMessageId, setPrevThenMessageId] = useState<number>(-1);
- const {
- data: chatData,
- refetch: refetch,
- isFetching: isFetching
- } = usePostGetGroupChatQuery(token, group_token, 50, prevThenMessageId, true);
- const [canSeeMembers, setCanSeeMembers] = useState(false);
- const { data: pinData, refetch: refetchPinned } = usePostGetPinnedGroupMessageQuery(
- token,
- group_token,
- true
- );
- const { data } = usePostGetGroupSettingsQuery(token, group_token, true);
- const { data: members, refetch: refetchMembers } = usePostGetGroupMembersQuery(
- token,
- group_token,
- canSeeMembers
- );
- const { mutateAsync: sendMessage } = usePostSendGroupMessageMutation();
- const [isSearchingMessage, setIsSearchingMessage] = useState<number | null>(null);
- const swipeableRowRef = useRef<Swipeable | null>(null);
- const messageContainerRef = useRef<FlatList<IMessage> | null>(null);
- const [selectedMedia, setSelectedMedia] = useState<any>(null);
- const [pinned, setPinned] = useState<any>(null);
- const [replyMessage, setReplyMessage] = useState<CustomMessage | null>(null);
- const [modalInfo, setModalInfo] = useState({
- visible: false,
- type: 'confirm',
- message: '',
- action: () => {},
- buttonTitle: '',
- title: ''
- });
- const [selectedMessage, setSelectedMessage] = useState<BubbleProps<CustomMessage> | null>(null);
- const [emojiSelectorVisible, setEmojiSelectorVisible] = useState(false);
- const [messagePosition, setMessagePosition] = useState<{
- x: number;
- y: number;
- width: number;
- height: number;
- isMine: boolean;
- } | null>(null);
- const [isModalVisible, setIsModalVisible] = useState(false);
- const [unreadMessageIndex, setUnreadMessageIndex] = useState<number | null>(null);
- const { mutateAsync: markMessagesAsRead } = usePostGroupMessagesReadMutation();
- const { mutateAsync: deleteMessage } = usePostDeleteGroupMessageMutation();
- const { mutateAsync: reactToMessage } = usePostReactToGroupMessageMutation();
- const { mutateAsync: pinMessage } = usePostSetPinGroupMessageMutation();
- const [highlightedMessageId, setHighlightedMessageId] = useState<number | null>(null);
- const [isRerendering, setIsRerendering] = useState<boolean>(false);
- const [isTyping, setIsTyping] = useState<string | null>(null);
- const messageRefs = useRef<{ [key: string]: any }>({});
- const flatList = useRef<FlatList | null>(null);
- const scrollY = useSharedValue(0);
- const { isSubscribed } = usePushNotification();
- const [isLoadingEarlier, setIsLoadingEarlier] = useState(false);
- const [hasMoreMessages, setHasMoreMessages] = useState(true);
- const updateUnreadMessagesCount = useMessagesStore((state) => state.updateUnreadMessagesCount);
- const [insetsColor, setInsetsColor] = useState(Colors.FILL_LIGHT);
- const [text, setText] = useState('');
- const [mentionList, setMentionList] = useState<any>([]);
- const [showMentions, setShowMentions] = useState(false);
- const [inputHeight, setInputHeight] = useState(45);
- const appState = useRef(AppState.currentState);
- const textInputRef = useRef<TextInput>(null);
- const socket = useRef<WebSocket | null>(null);
- const closeModal = () => {
- setModalInfo({ ...modalInfo, visible: false });
- };
- useEffect(() => {
- Audio.setAudioModeAsync({
- allowsRecordingIOS: false,
- staysActiveInBackground: false,
- playsInSilentModeIOS: true,
- shouldDuckAndroid: true,
- playThroughEarpieceAndroid: false
- });
- }, []);
- useEffect(() => {
- if (pinData && pinData?.message) {
- setPinned(pinData.message);
- }
- }, [pinData]);
- const onSendMedia = useCallback(
- async (files: { uri: string; type: 'image' | 'video' }[]) => {
- for (const file of files) {
- const tempMessage: CustomMessage = {
- _id: Date.now() + Math.random(),
- text: '',
- createdAt: new Date(),
- user: { _id: +currentUserId, name: 'Me', avatar: null as never },
- reactions: {},
- deleted: false,
- attachment: {
- id: -1,
- filename: file.type,
- filetype: file.type,
- attachment_link: file.uri,
- attachment_full_url: file.uri
- },
- pending: true,
- isSending: true,
- image: file.type === 'image' ? file.uri : undefined,
- video: file.type === 'video' ? file.uri : undefined
- };
- if (replyMessage) {
- tempMessage.replyMessage = {
- text: replyMessage.text,
- id: replyMessage._id,
- name:
- replyMessage.user._id !== +currentUserId ? (replyMessage.user.name as string) : 'Me'
- };
- }
- setMessages((previousMessages) => GiftedChat.append(previousMessages ?? [], [tempMessage]));
- const messageData = {
- token,
- to_group_token: group_token,
- text: '',
- reply_to_id: replyMessage ? (replyMessage._id as number) : -1,
- attachment: {
- uri: file.uri,
- type: file.type,
- name: file.uri.split('/').pop()
- }
- };
- const res = await sendMessage(messageData, {
- onSuccess: (res) => {
- const { attachment, message_id } = res;
- const newMessage = {
- _id: message_id,
- text: '',
- attachment,
- replyMessage: { ...tempMessage.replyMessage, sender: replyMessage?.user?._id },
- image: file.type === 'image' ? API_HOST + attachment.attachment_full_url : undefined,
- video: file.type === 'video' ? file.uri : undefined
- };
- setMessages((previousMessages) =>
- (previousMessages ?? []).map((msg) =>
- msg._id === tempMessage._id
- ? {
- ...msg,
- _id: res.message_id,
- isSending: false
- }
- : msg
- )
- );
- sendWebSocketMessage('new_message', newMessage as unknown as CustomMessage);
- }
- });
- clearReplyMessage();
- }
- },
- [replyMessage]
- );
- const onSendLocation = useCallback(
- async (coords: { latitude: number; longitude: number }) => {
- const tempMessage: CustomMessage = {
- _id: Date.now() + Math.random(),
- text: '',
- createdAt: new Date(),
- user: { _id: +currentUserId, name: 'Me', avatar: null as never },
- pending: true,
- deleted: false,
- reactions: {},
- attachment: {
- id: -1,
- filename: 'location.json',
- filetype: 'nomadmania/location',
- lat: coords.latitude,
- lng: coords.longitude
- }
- };
- if (replyMessage) {
- tempMessage.replyMessage = {
- text: replyMessage.text,
- id: replyMessage._id,
- name: replyMessage.user._id !== +currentUserId ? (replyMessage.user.name as string) : 'Me'
- };
- }
- setMessages((previousMessages) => GiftedChat.append(previousMessages ?? [], [tempMessage]));
- const locationData = JSON.stringify({ lat: coords.latitude, lng: coords.longitude });
- const fileUri = FileSystem.documentDirectory + 'location.json';
- await FileSystem.writeAsStringAsync(fileUri, locationData);
- const locationFile = {
- uri: fileUri,
- type: 'application/json',
- name: 'location.json'
- };
- const messageData = {
- token,
- to_group_token: group_token,
- text: tempMessage.text,
- reply_to_id: replyMessage ? (replyMessage._id as number) : -1,
- attachment: locationFile
- };
- sendMessage(messageData, {
- onSuccess: async (res) => {
- const { attachment, message_id } = res;
- const newMessage = {
- _id: message_id,
- text: '',
- attachment,
- replyMessage: { ...tempMessage.replyMessage, sender: replyMessage?.user?._id }
- };
- setMessages((previousMessages) =>
- (previousMessages ?? []).map((msg) =>
- msg._id === tempMessage._id ? { ...msg, _id: res.message_id } : msg
- )
- );
- sendWebSocketMessage('new_message', newMessage as unknown as CustomMessage);
- await FileSystem.deleteAsync(fileUri);
- },
- onError: async (err) => {
- await FileSystem.deleteAsync(fileUri);
- }
- });
- clearReplyMessage();
- },
- [replyMessage]
- );
- const onSendFile = useCallback(
- (files: { uri: string; type: string; name?: string }[]) => {
- const newMsgs = files.map((file) => {
- const msg: CustomMessage = {
- _id: Date.now() + Math.random(),
- text: '',
- createdAt: new Date(),
- user: { _id: +currentUserId, name: 'Me', avatar: null as never },
- deleted: false,
- reactions: {},
- isSending: true,
- attachment: {
- id: -1,
- filename: file.name ?? 'File',
- filetype: file.type,
- attachment_link: file.uri
- }
- };
- if (replyMessage) {
- msg.replyMessage = {
- text: replyMessage.text,
- id: replyMessage._id,
- name:
- replyMessage.user._id !== +currentUserId ? (replyMessage.user.name as string) : 'Me'
- };
- }
- if (file.type.includes('image')) {
- msg.image = file.uri;
- } else if (file.type.includes('video')) {
- msg.video = file.uri;
- }
- setMessages((previousMessages) => GiftedChat.append(previousMessages ?? [], [msg]));
- const messageData = {
- token,
- to_group_token: group_token,
- text: '',
- reply_to_id: replyMessage ? (replyMessage._id as number) : -1,
- attachment: {
- uri: file.uri,
- type: file.type,
- name: file.name || file.uri.split('/').pop()
- }
- };
- sendMessage(messageData, {
- onSuccess: (res) => {
- const { attachment, message_id } = res;
- const newMessage = {
- _id: message_id,
- text: '',
- attachment,
- replyMessage: { ...msg.replyMessage, sender: replyMessage?.user?._id },
- image: file.type === 'image' ? API_HOST + attachment.attachment_full_url : undefined,
- video: file.type === 'video' ? file.uri : undefined
- };
- setMessages((previousMessages) =>
- (previousMessages ?? []).map((prevMsg) =>
- prevMsg._id === msg._id
- ? {
- ...prevMsg,
- _id: res.message_id,
- attachment: res.attachment,
- isSending: false,
- image:
- res.attachment?.attachment_small_url && file.type?.startsWith('image')
- ? API_HOST + res.attachment.attachment_small_url
- : undefined,
- video:
- res.attachment?.attachment_link && file.type?.startsWith('video')
- ? API_HOST + res.attachment.attachment_link
- : undefined
- }
- : prevMsg
- )
- );
- sendWebSocketMessage('new_message', newMessage as unknown as CustomMessage);
- }
- });
- return msg;
- });
- clearReplyMessage();
- },
- [replyMessage]
- );
- async function openFileInApp(uri: string, fileName: string) {
- try {
- const dirExist = await FileSystem.getInfoAsync(CACHED_ATTACHMENTS_DIR);
- if (!dirExist.exists) {
- await FileSystem.makeDirectoryAsync(CACHED_ATTACHMENTS_DIR, { intermediates: true });
- }
- const fileUri = `${CACHED_ATTACHMENTS_DIR}${fileName}`;
- const fileExists = await FileSystem.getInfoAsync(fileUri);
- if (fileExists.exists && fileExists.size > 1024) {
- await FileViewer.open(fileUri, {
- showOpenWithDialog: true,
- showAppsSuggestions: true
- });
- return;
- }
- const { uri: localUri } = await FileSystem.downloadAsync(uri, fileUri, {
- headers: { Nmtoken: token, 'App-Version': APP_VERSION, Platform: Platform.OS }
- });
- await FileViewer.open(localUri, {
- showOpenWithDialog: true,
- showAppsSuggestions: true
- });
- } catch (err) {
- console.warn('openFileInApp error:', err);
- Alert.alert('Cannot open file', 'No application found to open this file.');
- }
- }
- async function downloadFileToDevice(currentMessage: CustomMessage) {
- if (!currentMessage.image && !currentMessage.video) {
- return;
- }
- const fileUrl = currentMessage.video
- ? currentMessage.video
- : API_HOST + currentMessage.attachment?.attachment_full_url;
- const fileType = currentMessage.attachment?.filetype || '';
- const fileExt = fileType.split('/').pop() || '';
- const fileName = currentMessage.attachment?.filename?.split('.')[0] || 'file';
- const fileUri = `${FileSystem.cacheDirectory}${fileName}.${fileExt}`;
- try {
- const { status } = await MediaLibrary.requestPermissionsAsync();
- if (status !== 'granted') {
- return;
- }
- const downloadOptions = currentMessage.video
- ? { headers: { Nmtoken: token, 'App-Version': APP_VERSION, Platform: Platform.OS } }
- : undefined;
- const { uri } = await FileSystem.downloadAsync(fileUrl, fileUri, downloadOptions);
- await MediaLibrary.createAssetAsync(uri);
- Alert.alert(
- 'Success',
- `${fileType.startsWith('video') ? 'Video' : 'Image'} saved to gallery.`
- );
- } catch (error) {
- Alert.alert('Error', 'Failed to download the file.');
- }
- }
- const renderMessageFile = (props: BubbleProps<CustomMessage>) => {
- const { currentMessage } = props;
- const leftMessage = currentMessage?.user?._id !== +currentUserId;
- if (!currentMessage?.attachment) return null;
- const { attachment_link, filename } = currentMessage.attachment;
- const fileName = filename ?? 'Attachment';
- const uri = API_HOST + attachment_link;
- return (
- <TouchableOpacity
- style={[
- styles.fileContainer,
- { backgroundColor: leftMessage ? 'rgba(15, 63, 79, 0.2)' : 'rgba(244, 244, 244, 0.2)' }
- ]}
- onPress={() => {
- openFileInApp(uri, fileName);
- }}
- onLongPress={() => handleLongPress(currentMessage, props)}
- disabled={currentMessage?.isSending}
- >
- {currentMessage?.isSending ? (
- <ActivityIndicator
- size="small"
- color={leftMessage ? Colors.DARK_BLUE : Colors.FILL_LIGHT}
- />
- ) : (
- <MaterialCommunityIcons
- name="file"
- size={32}
- color={leftMessage ? Colors.DARK_BLUE : Colors.FILL_LIGHT}
- />
- )}
- <Text
- style={[
- styles.fileNameText,
- { color: leftMessage ? Colors.DARK_BLUE : Colors.FILL_LIGHT }
- ]}
- >
- {fileName}
- </Text>
- </TouchableOpacity>
- );
- };
- const renderMessageLocation = (props: BubbleProps<CustomMessage>) => {
- const { currentMessage } = props;
- if (!currentMessage?.attachment) return null;
- const { lat, lng } = currentMessage.attachment;
- if (!lat || !lng) return null;
- return (
- <View
- style={[
- {
- alignItems: 'center',
- borderRadius: 8,
- marginVertical: 6,
- marginHorizontal: 6,
- width: 220
- }
- ]}
- >
- <MessageLocation props={props} lat={lat} lng={lng} onLongPress={handleLongPress} />
- </View>
- );
- };
- const onShareLiveLocation = useCallback(() => {}, []);
- useEffect(() => {
- if (data && data.settings) {
- setCanSeeMembers(data.settings.members_can_see_members === 1 || data.settings.admin === 1);
- }
- }, [data]);
- useEffect(() => {
- let unsubscribe: any;
- const setupNotificationHandler = async () => {
- unsubscribe = await dismissChatNotifications(
- group_token,
- isSubscribed,
- setModalInfo,
- navigation
- );
- };
- setupNotificationHandler();
- return () => {
- if (unsubscribe) unsubscribe();
- updateUnreadMessagesCount();
- };
- }, [group_token]);
- useEffect(() => {
- socket.current = new WebSocket(WEBSOCKET_URL);
- socket.current.onopen = () => {
- socket.current?.send(JSON.stringify({ token }));
- };
- socket.current.onmessage = (event) => {
- const data = JSON.parse(event.data);
- handleWebSocketMessage(data);
- };
- socket.current.onclose = () => {
- console.log('WebSocket connection closed chat screen');
- };
- return () => {
- if (socket.current) {
- socket.current.close();
- socket.current = null;
- }
- };
- }, [token]);
- useEffect(() => {
- const handleAppStateChange = async (nextAppState: AppStateStatus) => {
- const prevState = appState.current;
- appState.current = nextAppState;
- if (prevState.match(/inactive|background/) && nextAppState === 'active') {
- refetch();
- if (!socket.current || socket.current.readyState === WebSocket.CLOSED) {
- socket.current = new WebSocket(WEBSOCKET_URL);
- socket.current.onopen = () => {
- socket.current?.send(JSON.stringify({ token }));
- };
- socket.current.onmessage = (event) => {
- const data = JSON.parse(event.data);
- handleWebSocketMessage(data);
- };
- }
- await dismissChatNotifications(group_token, isSubscribed, setModalInfo, navigation);
- }
- };
- const subscription = AppState.addEventListener('change', handleAppStateChange);
- return () => {
- subscription.remove();
- if (socket.current) {
- socket.current.close();
- socket.current = null;
- }
- };
- }, [token]);
- const handleWebSocketMessage = (data: any) => {
- switch (data.action) {
- case 'new_message':
- if (data.group_token === group_token && data.message) {
- const newMessage = mapApiMessageToGiftedMessage(data.message);
- setMessages((previousMessages) => {
- const messageExists =
- previousMessages && previousMessages.some((msg) => msg._id === newMessage._id);
- if (!messageExists) {
- return GiftedChat.append(previousMessages ?? [], [
- {
- ...newMessage,
- user: {
- _id: data.uid,
- name: data.name,
- avatar: API_HOST + data.avatar
- }
- }
- ]);
- }
- return previousMessages;
- });
- }
- break;
- case 'new_reaction':
- if (data.group_token === group_token && data.reaction) {
- // todo: name
- updateMessageWithReaction(data.reaction);
- }
- break;
- case 'unreact':
- if (data.group_token === group_token && data.unreacted_message_id) {
- // todo: name
- removeReactionFromMessage(data.unreacted_message_id);
- }
- break;
- case 'delete_message':
- if (data.group_token === group_token && data.deleted_message_id) {
- removeDeletedMessage(data.deleted_message_id);
- }
- break;
- case 'is_typing':
- if (data.group_token === group_token && data.uid !== +currentUserId) {
- setIsTyping(data.name);
- }
- break;
- case 'stopped_typing':
- if (data.group_token === group_token) {
- setIsTyping(null);
- }
- break;
- case 'messages_read':
- if (data.group_token === group_token && data.read_messages_ids) {
- setMessages(
- (prevMessages) =>
- prevMessages?.map((msg) => {
- if (data.read_messages_ids.includes(msg._id)) {
- return { ...msg, received: true };
- }
- return msg;
- }) ?? []
- );
- }
- break;
- case 'messages_received':
- if (data.group_token === group_token && data.received_messages_ids) {
- setMessages(
- (prevMessages) =>
- prevMessages?.map((msg) => {
- if (data.received_messages_ids.includes(msg._id)) {
- return { ...msg, sent: true };
- }
- return msg;
- }) ?? []
- );
- }
- break;
- default:
- break;
- }
- };
- const updateMessageWithReaction = (reactionData: any) => {
- setMessages(
- (prevMessages) =>
- prevMessages?.map((msg) => {
- if (msg._id === reactionData.message_id) {
- const updatedReactions = [
- ...(Array.isArray(msg.reactions)
- ? msg.reactions?.filter((r: any) => r.uid !== reactionData.uid)
- : []),
- reactionData
- ];
- return { ...msg, reactions: updatedReactions };
- }
- return msg;
- }) ?? []
- );
- };
- const removeReactionFromMessage = (messageId: number) => {
- setMessages(
- (prevMessages) =>
- prevMessages?.map((msg) => {
- if (msg._id === messageId) {
- const updatedReactions = Array.isArray(msg.reactions)
- ? msg.reactions?.filter((r: any) => r.uid === +currentUserId)
- : [];
- return { ...msg, reactions: updatedReactions };
- }
- return msg;
- }) ?? []
- );
- };
- const removeDeletedMessage = (messageId: number) => {
- setMessages(
- (prevMessages) =>
- prevMessages?.map((msg) => {
- if (msg._id === messageId) {
- return {
- ...msg,
- deleted: true,
- text: 'This message was deleted',
- pending: false,
- sent: false,
- received: false
- };
- }
- return msg;
- }) ?? []
- );
- };
- useEffect(() => {
- const pingInterval = setInterval(() => {
- if (socket.current && socket.current.readyState === WebSocket.OPEN) {
- socket.current.send(
- JSON.stringify({ action: 'ping', conversation_with_group: group_token })
- );
- } else {
- socket.current = new WebSocket(WEBSOCKET_URL);
- socket.current.onopen = () => {
- socket.current?.send(JSON.stringify({ token }));
- };
- socket.current.onmessage = (event) => {
- const data = JSON.parse(event.data);
- handleWebSocketMessage(data);
- };
- return () => {
- if (socket.current) {
- socket.current.close();
- socket.current = null;
- }
- };
- }
- }, 50000);
- return () => clearInterval(pingInterval);
- }, []);
- const sendWebSocketMessage = (
- action: string,
- message: CustomMessage | null = null,
- reaction: string | null = null,
- readMessagesIds: number[] | null = null
- ) => {
- if (socket.current && socket.current.readyState === WebSocket.OPEN) {
- const data: any = {
- action,
- conversation_with_group: group_token
- };
- if (action === 'new_message' && message) {
- data.message = {
- id: message._id,
- text: message.text,
- sender: +currentUserId,
- sent_datetime: new Date().toISOString().replace('T', ' ').substring(0, 19),
- reply_to_id: message.replyMessage?.id ?? -1,
- reply_to: message.replyMessage ?? null,
- reactions: message.reactions ?? '{}',
- status: 2,
- attachement: message.attachment ? message.attachment : -1
- };
- }
- if (action === 'new_reaction' && message && reaction) {
- data.reaction = {
- message_id: message._id,
- reaction,
- uid: +currentUserId,
- datetime: new Date().toISOString()
- };
- }
- if (action === 'unreact' && message) {
- data.message_id = message._id;
- }
- if (action === 'delete_message' && message) {
- data.message_id = message._id;
- }
- if (action === 'messages_read' && readMessagesIds) {
- data.messages_ids = readMessagesIds;
- }
- socket.current.send(JSON.stringify(data));
- }
- };
- const handleTyping = (isTyping: boolean) => {
- if (isTyping) {
- sendWebSocketMessage('is_typing');
- } else {
- sendWebSocketMessage('stopped_typing');
- }
- };
- const mapApiMessageToGiftedMessage = (message: GroupMessage): CustomMessage => {
- return {
- _id: message.id,
- text: message.text,
- createdAt: new Date(message.sent_datetime + 'Z'),
- user: {
- _id: message.sender,
- name: message.sender !== +currentUserId ? message.sender_name : 'Me',
- avatar:
- message.sender !== +currentUserId && message.sender_avatar
- ? API_HOST + message.sender_avatar
- : message.sender === +currentUserId
- ? (null as never)
- : undefined
- },
- replyMessage:
- message.reply_to_id && message.reply_to_id > 0
- ? {
- text: message.reply_to.text,
- id: message.reply_to.id,
- name:
- message.reply_to.sender !== +currentUserId ? message.reply_to?.sender_name : 'Me'
- }
- : null,
- reactions: JSON.parse(message.reactions || '{}'),
- attachment: message.attachement !== -1 ? message.attachement : null,
- pending: message.status === 1,
- sent: message.status === 2,
- received: message.status === 3,
- deleted: message.status === 4,
- isSending: false,
- video:
- message.attachement !== -1 && message.attachement?.filetype?.startsWith('video')
- ? API_HOST + message.attachement?.attachment_link
- : null,
- image:
- message.attachement !== -1 && message.attachement?.filetype?.startsWith('image')
- ? API_HOST + message.attachement?.attachment_small_url
- : null,
- system: message.sender === -1
- };
- };
- useFocusEffect(
- useCallback(() => {
- refetch();
- }, [])
- );
- useFocusEffect(
- useCallback(() => {
- if (chatData?.groupAvatar) {
- setGroupAvatar(API_HOST + chatData.groupAvatar);
- }
- if (chatData?.messages) {
- const mappedMessages = chatData.messages.map(mapApiMessageToGiftedMessage);
- if (unreadMessageIndex === null && !isFetching) {
- const firstUnreadIndex = mappedMessages.findLastIndex(
- (msg) => !msg.received && !msg?.deleted && msg.user._id !== +currentUserId
- );
- if (firstUnreadIndex !== -1) {
- setUnreadMessageIndex(firstUnreadIndex);
- const unreadMarker: any = {
- _id: 'unreadMarker',
- text: 'Unread messages',
- system: true
- };
- mappedMessages.splice(firstUnreadIndex + 1, 0, unreadMarker);
- setTimeout(() => {
- if (flatList.current) {
- flatList.current.scrollToIndex({
- index: firstUnreadIndex,
- animated: true,
- viewPosition: 0.5
- });
- }
- }, 500);
- } else {
- setUnreadMessageIndex(0);
- }
- }
- setMessages((previousMessages) => {
- const newMessages = mappedMessages.filter(
- (newMsg) => !previousMessages?.some((oldMsg) => oldMsg._id === newMsg._id)
- );
- return prevThenMessageId !== -1 && previousMessages
- ? GiftedChat.prepend(previousMessages, newMessages)
- : mappedMessages;
- });
- if (mappedMessages.length < 50) {
- setHasMoreMessages(false);
- }
- if (mappedMessages.length === 0 && !modalInfo.visible) {
- setTimeout(() => {
- textInputRef.current?.focus();
- }, 500);
- }
- setIsLoadingEarlier(false);
- }
- }, [chatData])
- );
- useEffect(() => {
- if (messages) {
- if (isSearchingMessage) {
- const messageIndex = messages.findIndex((msg) => msg._id === isSearchingMessage);
- if (messageIndex !== -1 && flatList.current) {
- setIsSearchingMessage(null);
- }
- scrollToMessage(isSearchingMessage);
- }
- }
- }, [messages]);
- useEffect(() => {
- if (messages?.length === 0 && !modalInfo.visible) {
- setTimeout(() => {
- textInputRef.current?.focus();
- }, 500);
- }
- }, [modalInfo]);
- const loadEarlierMessages = async () => {
- if (!hasMoreMessages || (isLoadingEarlier && !isSearchingMessage) || !messages) return;
- setIsLoadingEarlier(true);
- const previousMessageId = messages[messages.length - 1]._id;
- setPrevThenMessageId(previousMessageId);
- };
- const sentToServer = useRef<Set<number>>(new Set());
- const handleViewableItemsChanged = ({ viewableItems }: { viewableItems: any[] }) => {
- const newViewableUnreadMessages = viewableItems
- .filter(
- (item) =>
- !item.item.received &&
- !item.item.deleted &&
- item.item._id !== 'unreadMarker' &&
- item.item.user._id !== +currentUserId &&
- !sentToServer.current.has(item.item._id)
- )
- .map((item) => item.item._id);
- if (newViewableUnreadMessages.length > 0) {
- markMessagesAsRead(
- {
- token,
- group_token,
- messages_id: newViewableUnreadMessages
- },
- {
- onSuccess: (res) => {
- newViewableUnreadMessages.forEach((id) => sentToServer.current.add(id));
- sendWebSocketMessage('messages_read', null, null, newViewableUnreadMessages);
- }
- }
- );
- }
- };
- const renderSystemMessage = (props: any) => {
- if (props.currentMessage._id === 'unreadMarker') {
- return (
- <View style={styles.unreadMessagesContainer}>
- <Text style={styles.unreadMessagesText}>{props.currentMessage.text}</Text>
- </View>
- );
- } else if (props.currentMessage.user._id === -1) {
- return (
- <SystemMessage
- currentMessage={props.currentMessage}
- containerStyle={{
- marginTop: 0,
- marginBottom: 0,
- paddingVertical: 2,
- paddingHorizontal: 12
- }}
- wrapperStyle={{
- backgroundColor: Colors.FILL_LIGHT,
- paddingHorizontal: 6,
- paddingVertical: 4,
- borderRadius: 10
- }}
- textStyle={{
- color: Colors.DARK_BLUE,
- fontStyle: 'italic',
- fontSize: 12,
- textAlign: 'center'
- }}
- />
- );
- }
- return null;
- };
- const clearReplyMessage = () => setReplyMessage(null);
- const handleLongPress = (message: CustomMessage, props: BubbleProps<CustomMessage>) => {
- const messageRef = messageRefs.current[message._id];
- setSelectedMessage(props);
- trigger('impactMedium', options);
- const isMine = message.user._id === +currentUserId;
- if (messageRef) {
- messageRef.measureInWindow((x: number, y: number, width: number, height: number) => {
- const screenHeight = Dimensions.get('window').height;
- const spaceAbove = y - insets.top;
- const spaceBelow = screenHeight - (y + height) - insets.bottom * 2;
- let finalY = y;
- scrollY.value = 0;
- if (isNaN(y) || isNaN(height)) {
- console.error("Invalid measurement values for 'y' or 'height'", { y, height });
- return;
- }
- if (spaceBelow < 220) {
- const extraShift = 220 - spaceBelow;
- finalY -= extraShift;
- }
- if (spaceAbove < 50) {
- const extraShift = 50 - spaceAbove;
- finalY += extraShift;
- }
- if (spaceBelow < 220 || spaceAbove < 50) {
- const targetY = screenHeight / 2 - height / 2;
- scrollY.value = withTiming(finalY - finalY);
- }
- if (height > Dimensions.get('window').height - 200) {
- finalY = 100;
- }
- finalY = isNaN(finalY) ? 0 : finalY;
- setMessagePosition({ x, y: finalY, width, height, isMine });
- setIsModalVisible(true);
- });
- }
- };
- const openEmojiSelector = () => {
- SheetManager.show('emoji-selector');
- trigger('impactLight', options);
- };
- const closeEmojiSelector = () => {
- SheetManager.hide('emoji-selector');
- };
- const handleReactionPress = (emoji: string, messageId: number) => {
- addReaction(messageId, emoji);
- };
- const handleDeleteMessage = (messageId: number) => {
- deleteMessage(
- {
- token,
- message_id: messageId,
- group_token
- },
- {
- onSuccess: () => {
- setMessages(
- (prevMessages) =>
- prevMessages?.map((msg) => {
- if (msg._id === messageId) {
- return {
- ...msg,
- deleted: true,
- text: 'This message was deleted',
- pending: false,
- sent: false,
- received: false,
- attachment: null,
- image: undefined,
- video: undefined
- };
- }
- return msg;
- }) ?? []
- );
- const messageToDelete = messages?.find((msg) => msg._id === messageId);
- if (messageToDelete) {
- sendWebSocketMessage('delete_message', messageToDelete, null, null);
- }
- }
- }
- );
- };
- const handlePinMessage = (messageId: number, pin: 0 | 1) => {
- pinMessage(
- {
- token,
- message_id: messageId,
- group_token,
- pin
- },
- {
- onSuccess: () => {
- refetchPinned();
- if (pin === 0) {
- setPinned(null);
- }
- }
- }
- );
- };
- const handleOptionPress = (option: string) => {
- if (!selectedMessage) return;
- switch (option) {
- case 'reply':
- setReplyMessage(selectedMessage.currentMessage);
- setIsModalVisible(false);
- break;
- case 'copy':
- Clipboard.setString(selectedMessage?.currentMessage?.text ?? '');
- setIsModalVisible(false);
- Alert.alert('Copied');
- break;
- case 'delete':
- handleDeleteMessage(selectedMessage.currentMessage?._id);
- setIsModalVisible(false);
- break;
- case 'download':
- downloadFileToDevice(selectedMessage.currentMessage);
- setIsModalVisible(false);
- break;
- case 'info':
- SheetManager.show('group-status', {
- payload: {
- messageId: selectedMessage.currentMessage._id,
- groupToken: group_token,
- setInsetsColor
- } as any
- });
- setIsModalVisible(false);
- setInsetsColor(Colors.WHITE);
- break;
- case 'pin':
- handlePinMessage(selectedMessage.currentMessage?._id, 1);
- setIsModalVisible(false);
- break;
- default:
- break;
- }
- closeEmojiSelector();
- };
- const openReactionList = (
- reactions: { uid: number; name: string; reaction: string }[],
- messageId: number
- ) => {
- SheetManager.show('reactions-list-modal', {
- payload: {
- users: reactions,
- currentUserId: +currentUserId,
- token,
- messageId,
- conversation_with_user: group_token,
- setMessages,
- sendWebSocketMessage,
- isGroup: true,
- groupToken: group_token
- } as any
- });
- };
- const renderTimeContainer = (time: TimeProps<CustomMessage>) => {
- const createdAt = new Date(time.currentMessage.createdAt);
- const formattedTime = createdAt.toLocaleTimeString([], {
- hour: '2-digit',
- minute: '2-digit',
- hour12: true
- });
- const hasReactions =
- time.currentMessage.reactions &&
- Array.isArray(time.currentMessage.reactions) &&
- time.currentMessage.reactions.length > 0;
- return (
- <View
- style={[
- styles.bottomContainer,
- {
- justifyContent: hasReactions ? 'space-between' : 'flex-end'
- }
- ]}
- >
- {hasReactions && (
- <TouchableOpacity
- style={[
- styles.bottomCustomContainer,
- {
- backgroundColor:
- time.position === 'left' ? 'rgba(0, 0, 0, 0.2)' : 'rgba(255, 255, 255, 0.2)'
- }
- ]}
- onPress={() =>
- Array.isArray(time.currentMessage.reactions) &&
- openReactionList(
- time.currentMessage.reactions.map((reaction) => ({
- ...reaction,
- name: reaction.uid !== +currentUserId ? reaction?.name : 'Me'
- })),
- time.currentMessage._id
- )
- }
- >
- {Object.entries(
- (Array.isArray(time.currentMessage.reactions)
- ? time.currentMessage.reactions
- : []
- ).reduce(
- (acc: Record<string, { count: number }>, { reaction }: { reaction: string }) => {
- if (!acc[reaction]) {
- acc[reaction] = { count: 0 };
- }
- acc[reaction].count += 1;
- return acc;
- },
- {}
- )
- ).map(([emoji, { count }]: any) => {
- return (
- <View key={emoji}>
- <Text style={{}}>
- {emoji}
- {(count as number) > 1 ? ` ${count}` : ''}
- </Text>
- </View>
- );
- })}
- </TouchableOpacity>
- )}
- <View style={styles.timeContainer}>
- <Text style={styles.timeText}>{formattedTime}</Text>
- {renderTicks(time.currentMessage)}
- </View>
- </View>
- );
- };
- const renderSelectedMessage = () =>
- selectedMessage && (
- <View
- style={{
- maxHeight: '80%',
- width: messagePosition?.width,
- position: 'absolute',
- top: messagePosition?.y,
- left: messagePosition?.x
- }}
- >
- <ScrollView>
- <Bubble
- {...selectedMessage}
- wrapperStyle={{
- right: { backgroundColor: Colors.DARK_BLUE },
- left: { backgroundColor: Colors.FILL_LIGHT }
- }}
- textStyle={{
- right: { color: Colors.WHITE },
- left: { color: Colors.DARK_BLUE }
- }}
- renderTicks={() => null}
- renderTime={renderTimeContainer}
- renderCustomView={() =>
- selectedMessage.currentMessage.attachment?.filetype === 'nomadmania/location'
- ? renderMessageLocation(selectedMessage)
- : selectedMessage.currentMessage.attachment &&
- !selectedMessage.currentMessage.image &&
- !selectedMessage.currentMessage.video
- ? renderMessageFile(selectedMessage)
- : renderReplyMessageView(selectedMessage)
- }
- />
- </ScrollView>
- </View>
- );
- const handleBackgroundPress = () => {
- setIsModalVisible(false);
- setSelectedMessage(null);
- closeEmojiSelector();
- };
- useFocusEffect(
- useCallback(() => {
- navigation?.getParent()?.setOptions({
- tabBarStyle: {
- display: 'none'
- }
- });
- }, [navigation])
- );
- const onSend = useCallback(
- (newMessages: CustomMessage[] = []) => {
- if (replyMessage) {
- newMessages[0].replyMessage = {
- text: transformMessageForServer(replyMessage.text),
- id: replyMessage._id,
- name: replyMessage.user._id !== +currentUserId ? (replyMessage.user.name as string) : 'Me'
- };
- }
- const user = {
- _id: +currentUserId,
- name: 'Me',
- avatar: null as never
- };
- const message = { ...newMessages[0], pending: true, isSending: true, user };
- setMessages((previousMessages) =>
- GiftedChat.append(previousMessages ?? [], [
- { ...message, text: transformMessageForServer(newMessages[0].text) }
- ])
- );
- sendMessage(
- {
- token,
- to_group_token: group_token,
- text: transformMessageForServer(message.text),
- reply_to_id: replyMessage ? (replyMessage._id as number) : -1
- },
- {
- onSuccess: (res) => {
- const newMessage = {
- _id: res.message_id,
- text: message.text,
- replyMessage: { ...message.replyMessage, sender: replyMessage?.user?._id }
- };
- setMessages((previousMessages) =>
- (previousMessages ?? []).map((msg) =>
- msg._id === message._id ? { ...msg, _id: res.message_id, isSending: false } : msg
- )
- );
- sendWebSocketMessage('new_message', newMessage as unknown as CustomMessage);
- }
- }
- );
- clearReplyMessage();
- },
- [replyMessage]
- );
- const addReaction = (messageId: number, reaction: string) => {
- if (!messages) return;
- const updatedMessages = messages.map((msg: any) => {
- if (msg._id === messageId) {
- const updatedReactions: Reaction[] = [
- ...(Array.isArray(msg.reactions)
- ? msg.reactions?.filter((r: Reaction) => r.uid !== +currentUserId)
- : []),
- { datetime: new Date().toISOString(), reaction: reaction, uid: +currentUserId }
- ];
- return {
- ...msg,
- reactions: updatedReactions
- };
- }
- return msg;
- });
- setMessages(updatedMessages);
- reactToMessage(
- { token, message_id: messageId, reaction: reaction, group_token: group_token },
- {
- onSuccess: () => {
- const message = messages.find((msg) => msg._id === messageId);
- if (message) {
- sendWebSocketMessage('new_reaction', message, reaction);
- }
- }
- }
- );
- setIsModalVisible(false);
- };
- const updateRowRef = useCallback(
- (ref: any) => {
- if (
- ref &&
- replyMessage &&
- ref.props.children.props.currentMessage?._id === replyMessage._id
- ) {
- swipeableRowRef.current = ref;
- }
- },
- [replyMessage]
- );
- const renderReplyMessageView = (props: BubbleProps<CustomMessage>) => {
- if (!props.currentMessage) {
- return null;
- }
- const { currentMessage } = props;
- if (!currentMessage || !currentMessage?.replyMessage) {
- return null;
- }
- return (
- <TouchableOpacity
- style={[
- styles.replyMessageContainer,
- {
- backgroundColor:
- currentMessage.user._id !== +currentUserId
- ? 'rgba(255, 255, 255, 0.7)'
- : 'rgba(0, 0, 0, 0.2)',
- borderColor:
- currentMessage.user._id !== +currentUserId ? Colors.DARK_BLUE : Colors.WHITE
- }
- ]}
- onPress={() => {
- if (currentMessage?.replyMessage?.id) {
- scrollToMessage(currentMessage.replyMessage.id);
- }
- }}
- >
- <View style={styles.replyContent}>
- <Text
- style={[
- styles.replyAuthorName,
- {
- color: currentMessage.user._id !== +currentUserId ? Colors.DARK_BLUE : Colors.WHITE
- }
- ]}
- >
- {currentMessage.replyMessage.name}
- </Text>
- <Text
- numberOfLines={1}
- style={[
- styles.replyMessageText,
- {
- color: currentMessage.user._id !== +currentUserId ? Colors.DARK_BLUE : Colors.WHITE
- }
- ]}
- >
- {currentMessage.replyMessage.text}
- </Text>
- </View>
- </TouchableOpacity>
- );
- };
- const scrollToMessage = (messageId: number) => {
- if (!messages) return;
- const messageIndex = messages.findIndex((message) => message._id === messageId);
- if (messageIndex !== -1 && flatList.current) {
- setIsSearchingMessage(null);
- flatList.current.scrollToIndex({
- index: messageIndex,
- animated: true,
- viewPosition: 0.5
- });
- setHighlightedMessageId(messageId);
- setMessages((previousMessages) =>
- (previousMessages ?? []).map((msg) =>
- msg._id === messageId
- ? {
- ...msg,
- isRendering: msg?.isRendering ? false : true
- }
- : msg
- )
- );
- }
- if (hasMoreMessages && messageIndex === -1) {
- setIsSearchingMessage(messageId);
- loadEarlierMessages();
- }
- };
- useEffect(() => {
- if (highlightedMessageId && isRerendering) {
- setTimeout(() => {
- setHighlightedMessageId(null);
- setIsRerendering(false);
- }, 1500);
- }
- }, [highlightedMessageId, isRerendering]);
- useEffect(() => {
- if (replyMessage && swipeableRowRef.current) {
- swipeableRowRef.current.close();
- swipeableRowRef.current = null;
- }
- }, [replyMessage]);
- const handleOpenImage = async (uri: string, fileName: string) => {
- const dirExist = await FileSystem.getInfoAsync(CACHED_ATTACHMENTS_DIR);
- if (!dirExist.exists) {
- await FileSystem.makeDirectoryAsync(CACHED_ATTACHMENTS_DIR, { intermediates: true });
- }
- const fileUri = `${CACHED_ATTACHMENTS_DIR}${fileName}`;
- const fileExists = await FileSystem.getInfoAsync(fileUri);
- if (fileExists.exists && fileExists.size > 1024) {
- setSelectedMedia(fileUri);
- return;
- }
- setSelectedMedia(uri);
- const { uri: localUri } = await FileSystem.downloadAsync(uri, fileUri, {
- headers: { Nmtoken: token, 'App-Version': APP_VERSION, Platform: Platform.OS }
- });
- };
- const renderMessageImage = (props: any) => {
- const { currentMessage } = props;
- const leftMessage = currentMessage?.user?._id !== +currentUserId;
- return (
- <TouchableOpacity
- onPress={() => {
- if (!currentMessage.attachment.attachment_full_url?.startsWith('/')) {
- setSelectedMedia(currentMessage.attachment.attachment_full_url);
- return;
- }
- handleOpenImage(
- API_HOST + currentMessage.attachment.attachment_full_url,
- currentMessage.attachment?.filename
- );
- }}
- onLongPress={() => handleLongPress(currentMessage, props)}
- style={styles.imageContainer}
- disabled={currentMessage.isSending}
- >
- <Image
- source={{
- uri: currentMessage.image,
- headers: { Nmtoken: token, 'App-Version': APP_VERSION, Platform: Platform.OS }
- }}
- style={styles.chatImage}
- resizeMode="cover"
- />
- {currentMessage.isSending && (
- <View
- style={{
- position: 'absolute',
- top: 0,
- left: 0,
- right: 0,
- bottom: 0,
- justifyContent: 'center',
- alignItems: 'center'
- }}
- >
- <ActivityIndicator
- size="large"
- color={leftMessage ? Colors.DARK_BLUE : Colors.FILL_LIGHT}
- />
- </View>
- )}
- </TouchableOpacity>
- );
- };
- const renderTicks = (message: CustomMessage) => {
- if (message.user._id !== +currentUserId) return null;
- if (message.isSending) {
- return (
- <View>
- <ActivityIndicator
- size={16}
- color={Colors.LIGHT_GRAY}
- style={{ transform: 'scale(0.8)' }}
- />
- </View>
- );
- }
- return message.received ? (
- <View>
- <MaterialCommunityIcons name="check-all" size={16} color={Colors.WHITE} />
- </View>
- ) : message.sent ? (
- <View>
- <MaterialCommunityIcons name="check" size={16} color={Colors.WHITE} />
- </View>
- ) : message.pending ? (
- <View>
- <MaterialCommunityIcons name="check" size={16} color={Colors.LIGHT_GRAY} />
- </View>
- ) : null;
- };
- const renderBubble = (props: BubbleProps<CustomMessage>) => {
- const { currentMessage } = props;
- if (currentMessage.deleted) {
- const text = currentMessage.text.length
- ? props.currentMessage.text
- : 'This message was deleted';
- return (
- <View>
- <Bubble
- {...props}
- renderTime={() => null}
- currentMessage={{
- ...props.currentMessage,
- text: text
- }}
- renderMessageText={() => (
- <View style={{ paddingHorizontal: 12, paddingVertical: 6 }}>
- <Text style={{ color: Colors.LIGHT_GRAY, fontStyle: 'italic', fontSize: 12 }}>
- {text}
- </Text>
- </View>
- )}
- wrapperStyle={{
- right: {
- backgroundColor: Colors.DARK_BLUE
- },
- left: {
- backgroundColor: Colors.FILL_LIGHT
- }
- }}
- textStyle={{
- left: {
- color: Colors.DARK_BLUE
- },
- right: {
- color: Colors.WHITE
- }
- }}
- />
- </View>
- );
- }
- const isHighlighted = currentMessage._id === highlightedMessageId;
- const backgroundColor = isHighlighted
- ? Colors.ORANGE
- : currentMessage.user._id === +currentUserId
- ? Colors.DARK_BLUE
- : Colors.FILL_LIGHT;
- const messageToCompare = props.previousMessage;
- const showUserName =
- props.position === 'left' &&
- currentMessage &&
- messageToCompare &&
- (!isSameUser(currentMessage, messageToCompare) ||
- !isSameDay(currentMessage, messageToCompare));
- return (
- <View
- key={`${currentMessage._id}-${isHighlighted ? 'highlighted' : 'normal'}`}
- ref={(ref) => {
- if (ref && currentMessage) {
- messageRefs.current[currentMessage._id] = ref;
- }
- }}
- collapsable={false}
- >
- <Bubble
- {...props}
- wrapperStyle={{
- right: {
- backgroundColor: backgroundColor
- },
- left: {
- backgroundColor: backgroundColor
- }
- }}
- textStyle={{
- left: {
- color: Colors.DARK_BLUE
- },
- right: {
- color: Colors.FILL_LIGHT
- }
- }}
- onLongPress={() => handleLongPress(currentMessage, props)}
- renderTicks={() => null}
- renderTime={renderTimeContainer}
- renderCustomView={() => {
- return (
- <View>
- {showUserName ? (
- <Text
- style={{
- color: Colors.BLACK,
- fontWeight: '600',
- fontSize: 13,
- paddingHorizontal: 10,
- paddingTop: 8,
- paddingBottom: 2
- }}
- >
- {/* {'~ '} */}
- {props.currentMessage.user.name}
- </Text>
- ) : null}
- {currentMessage.attachment?.filetype === 'nomadmania/location'
- ? renderMessageLocation(props)
- : currentMessage.attachment && !currentMessage.image && !currentMessage.video
- ? renderMessageFile(props)
- : renderReplyMessageView(props)}
- </View>
- );
- }}
- />
- </View>
- );
- };
- const openAttachmentsModal = () => {
- SheetManager.show('chat-attachments', {
- payload: {
- name: groupName,
- uid: group_token,
- setModalInfo,
- closeOptions: () => {},
- onSendMedia,
- onSendLocation,
- onShareLiveLocation,
- onSendFile,
- isGroup: true
- } as any
- });
- };
- const renderInputToolbar = (props: any) => {
- if (!chatData?.can_send_messages) return null;
- return (
- <>
- {showMentions && canSeeMembers ? (
- <MentionsList
- mentionList={mentionList}
- inputHeight={inputHeight}
- onMentionSelect={onMentionSelect}
- />
- ) : null}
- <View
- onLayout={(e) => {
- setInputHeight(e.nativeEvent.layout.height);
- }}
- >
- <InputToolbar
- {...props}
- renderActions={() =>
- userType === 'normal' ? (
- <Actions
- icon={() => (
- <MaterialCommunityIcons name="plus" size={28} color={Colors.DARK_BLUE} />
- )}
- onPressActionButton={openAttachmentsModal}
- />
- ) : null
- }
- containerStyle={{
- backgroundColor: Colors.FILL_LIGHT
- }}
- />
- </View>
- </>
- );
- };
- const renderScrollToBottom = () => {
- return (
- <TouchableOpacity
- style={styles.scrollToBottom}
- onPress={() => {
- if (flatList.current) {
- flatList.current.scrollToIndex({ index: 0, animated: true });
- }
- }}
- >
- <MaterialCommunityIcons name="chevron-down" size={24} color={Colors.WHITE} />
- </TouchableOpacity>
- );
- };
- const shouldUpdateMessage = (
- props: MessageProps<IMessage>,
- nextProps: MessageProps<IMessage>
- ) => {
- setIsRerendering(true);
- const currentId = nextProps.currentMessage._id;
- return currentId === highlightedMessageId;
- };
- const onInputTextChanged = (value: string) => {
- handleTyping(value.length > 0);
- setText(value);
- const mentionMatch = value.match(/(^|\s)(@\w*)$/);
- if (mentionMatch) {
- setShowMentions(true);
- const searchText = mentionMatch[2].slice(1).toLowerCase();
- setMentionList(
- (members?.settings ?? [])?.filter(
- (m) => m.name.toLowerCase().includes(searchText) && m.uid !== +currentUserId
- )
- );
- } else {
- setShowMentions(false);
- }
- };
- const onMentionSelect = (member: { uid: number; name: string }) => {
- const words = text.split(' ');
- words[words.length - 1] = `@${member.name} `;
- setText(words.join(' '));
- setShowMentions(false);
- };
- const transformMessageForServer = (text: string) => {
- let transformedText = text;
- members?.settings?.forEach((member) => {
- const mentionRegex = new RegExp(`@${member.name}\\b`, 'g');
- transformedText = transformedText.replace(mentionRegex, `@{${member.uid}}`);
- });
- return transformedText;
- };
- return (
- <SafeAreaView
- edges={['top']}
- style={{
- height: '100%'
- }}
- >
- <View style={{ paddingHorizontal: '5%' }}>
- <Header
- label={groupName}
- textColor={userType !== 'normal' ? Colors.RED : Colors.DARK_BLUE}
- rightElement={
- <TouchableOpacity
- onPress={() =>
- navigation.navigate(
- ...([NAVIGATION_PAGES.GROUP_SETTINGS, { groupToken: group_token }] as never)
- )
- }
- disabled={userType !== 'normal'}
- >
- {groupAvatar && userType === 'normal' ? (
- <Image source={{ uri: groupAvatar, cache: 'reload' }} style={styles.avatar} />
- ) : userType === 'normal' ? (
- <GroupIcon fill={Colors.DARK_BLUE} width={30} height={30} />
- ) : (
- <BanIcon fill={Colors.RED} width={30} height={30} />
- )}
- </TouchableOpacity>
- }
- />
- </View>
- {pinned && (
- <TouchableOpacity
- style={{
- height: 38,
- flexDirection: 'row',
- backgroundColor: Colors.FILL_LIGHT,
- borderBottomWidth: 1,
- borderBottomColor: Colors.DARK_LIGHT
- }}
- onPress={() => scrollToMessage(pinned.id)}
- >
- <View
- style={{
- height: 50,
- width: 6,
- backgroundColor: Colors.DARK_BLUE
- }}
- ></View>
- <View
- style={{
- paddingLeft: 8,
- height: '100%',
- justifyContent: 'center'
- }}
- >
- <PinIcon fill={Colors.DARK_BLUE} height={18} />
- </View>
- <View style={{ flex: 1, justifyContent: 'center' }}>
- <Text style={{ color: Colors.DARK_BLUE, paddingLeft: 10 }} numberOfLines={1}>
- {pinned.text}
- </Text>
- </View>
- {data?.settings?.admin === 1 && (
- <View style={{ alignItems: 'flex-end', justifyContent: 'center' }}>
- <TouchableOpacity
- style={{ paddingRight: 10 }}
- onPress={() => handlePinMessage(pinned.id, 0)}
- >
- <MaterialCommunityIcons
- name="close-circle-outline"
- size={24}
- color={Colors.DARK_BLUE}
- />
- </TouchableOpacity>
- </View>
- )}
- </TouchableOpacity>
- )}
- <GestureHandlerRootView style={styles.container}>
- {messages ? (
- <GiftedChat
- messages={messages as CustomMessage[]}
- text={text}
- listViewProps={{
- ref: flatList,
- showsVerticalScrollIndicator: false,
- initialNumToRender: messages.length,
- onViewableItemsChanged: handleViewableItemsChanged,
- viewabilityConfig: { itemVisiblePercentThreshold: 50 },
- onScrollToIndexFailed: (info: any) => {
- const wait = new Promise((resolve) => setTimeout(resolve, 300));
- wait.then(() => {
- flatList.current?.scrollToIndex({
- index: info.index,
- animated: true,
- viewPosition: 0.5
- });
- });
- }
- }}
- renderSystemMessage={renderSystemMessage}
- onSend={(newMessages: CustomMessage[]) => onSend(newMessages)}
- user={{ _id: +currentUserId, name: 'Me' }}
- renderBubble={renderBubble}
- renderMessageImage={renderMessageImage}
- showUserAvatar={true}
- onPressAvatar={(user) => {
- navigation.navigate(
- ...([NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW, { userId: user._id }] as never)
- );
- }}
- renderInputToolbar={renderInputToolbar}
- renderCustomView={renderReplyMessageView}
- isCustomViewBottom={false}
- messageContainerRef={messageContainerRef}
- minComposerHeight={34}
- onInputTextChanged={onInputTextChanged}
- textInputRef={textInputRef}
- isTyping={isTyping ? true : false}
- renderTypingIndicator={() => <TypingIndicator isTyping={isTyping} />}
- renderSend={(props) => (
- <View style={styles.sendBtn}>
- {props.text?.trim() && (
- <Send
- {...props}
- containerStyle={{
- justifyContent: 'center'
- }}
- >
- <SendIcon fill={Colors.DARK_BLUE} />
- </Send>
- )}
- {!props.text?.trim() && <SendIcon fill={Colors.LIGHT_GRAY} />}
- </View>
- )}
- renderMessageVideo={(props) => (
- <RenderMessageVideo
- props={props}
- token={token}
- currentUserId={+currentUserId}
- onLongPress={handleLongPress}
- />
- )}
- textInputProps={{
- ...styles.composer,
- selectionColor: Colors.LIGHT_GRAY
- }}
- placeholder=""
- renderMessage={(props) => (
- <ChatMessageBox
- {...(props as MessageProps<CustomMessage>)}
- updateRowRef={updateRowRef}
- setReplyOnSwipeOpen={setReplyMessage}
- />
- )}
- renderChatFooter={() => (
- <ReplyMessageBar clearReply={clearReplyMessage} message={replyMessage} />
- )}
- maxComposerHeight={100}
- renderComposer={(props) => <Composer {...props} />}
- keyboardShouldPersistTaps="handled"
- renderChatEmpty={() => (
- <View style={styles.emptyChat}>
- <Text
- style={styles.emptyChatText}
- >{`No messages yet.\nFeel free to start the conversation.`}</Text>
- </View>
- )}
- shouldUpdateMessage={shouldUpdateMessage}
- scrollToBottom={true}
- scrollToBottomComponent={renderScrollToBottom}
- scrollToBottomStyle={{ backgroundColor: 'transparent' }}
- parsePatterns={(linkStyle) => [
- {
- type: 'url',
- style: { color: Colors.ORANGE, textDecorationLine: 'underline' },
- onPress: (url: string) => Linking.openURL(url),
- onLongPress: (url: string) => {
- Clipboard.setString(url ?? '');
- Alert.alert('Link copied');
- }
- },
- {
- pattern: /@\{(\d+)\}/g,
- renderText: (messageText: string) => {
- const tagId = messageText.slice(2, messageText.length - 1);
- const user = (members?.settings ?? [])?.find((m) => m.uid === +tagId);
- if (user) {
- return (
- <Text
- style={{ color: Colors.ORANGE }}
- onPress={() =>
- navigation.navigate(
- ...([
- NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW,
- { userId: user.uid }
- ] as never)
- )
- }
- >
- @{user.name}
- </Text>
- );
- } else {
- return messageText;
- }
- }
- }
- ]}
- infiniteScroll={true}
- loadEarlier={hasMoreMessages}
- isLoadingEarlier={isLoadingEarlier}
- onLoadEarlier={loadEarlierMessages}
- renderLoadEarlier={() => (
- <View style={{ paddingVertical: 20 }}>
- <ActivityIndicator size="small" color={Colors.DARK_BLUE} />
- </View>
- )}
- />
- ) : (
- <ActivityIndicator size="large" color={Colors.DARK_BLUE} />
- )}
- <ImageView
- images={[
- {
- uri: selectedMedia,
- cache: 'force-cache',
- headers: { Nmtoken: token, 'App-Version': APP_VERSION, Platform: Platform.OS }
- }
- ]}
- imageIndex={0}
- visible={!!selectedMedia}
- onRequestClose={() => setSelectedMedia(null)}
- backgroundColor={Colors.DARK_BLUE}
- />
- <ReactModal
- isVisible={isModalVisible}
- onBackdropPress={handleBackgroundPress}
- style={styles.reactModalContainer}
- animationIn="fadeIn"
- animationOut="fadeOut"
- useNativeDriver
- backdropColor="transparent"
- >
- <BlurView
- intensity={80}
- style={styles.modalBackground}
- experimentalBlurMethod="dimezisBlurView"
- >
- <TouchableOpacity
- style={styles.modalBackground}
- activeOpacity={1}
- onPress={handleBackgroundPress}
- >
- <ReactionBar
- messagePosition={messagePosition}
- selectedMessage={selectedMessage}
- reactionEmojis={reactionEmojis}
- handleReactionPress={handleReactionPress}
- openEmojiSelector={openEmojiSelector}
- />
- {renderSelectedMessage()}
- <OptionsMenu
- selectedMessage={selectedMessage}
- handleOptionPress={handleOptionPress}
- messagePosition={messagePosition}
- isGroup={true}
- isAdmin={data?.settings?.admin == 1}
- />
- <EmojiSelectorModal
- visible={emojiSelectorVisible}
- selectedMessage={selectedMessage}
- addReaction={addReaction}
- closeEmojiSelector={closeEmojiSelector}
- />
- </TouchableOpacity>
- </BlurView>
- </ReactModal>
- <WarningModal
- isVisible={modalInfo.visible}
- onClose={closeModal}
- type={modalInfo.type}
- message={modalInfo.message}
- buttonTitle={modalInfo.buttonTitle}
- title={modalInfo.title}
- action={() => {
- modalInfo.action();
- closeModal();
- }}
- />
- <AttachmentsModal />
- <ReactionsListModal />
- <GroupStatusModal />
- </GestureHandlerRootView>
- <View
- style={{
- height: insets.bottom,
- backgroundColor: insetsColor
- }}
- />
- </SafeAreaView>
- );
- };
- export default GroupChatScreen;
|