@messenger-box/platform-mobile 0.0.1-alpha.411 → 0.0.1-alpha.421

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,49 +1,45 @@
1
- import React, { useCallback, useEffect, useMemo, useState, useRef } from 'react';
1
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
2
  import {
3
+ Avatar,
3
4
  Box,
4
5
  Button,
6
+ Center,
5
7
  HStack,
6
- VStack,
7
8
  Icon,
8
9
  Image,
9
10
  Spinner,
10
11
  Text,
11
12
  useColorModeValue,
12
- ScrollView,
13
- Center,
14
- Avatar,
13
+ VStack,
15
14
  } from 'native-base';
16
- import { Platform, TouchableOpacity } from 'react-native';
17
- import { useNavigation, useFocusEffect, useRoute } from '@react-navigation/native';
15
+ import { Platform } from 'react-native';
16
+ import { useFocusEffect, useNavigation, useRoute } from '@react-navigation/native';
18
17
  import { useSelector } from 'react-redux';
19
- import { orderBy, uniqBy, startCase } from 'lodash';
18
+ import { orderBy, startCase, uniqBy } from 'lodash';
20
19
  import * as ImagePicker from 'expo-image-picker';
21
20
  import { encode as atob } from 'base-64';
22
21
  import { Ionicons, MaterialCommunityIcons } from '@expo/vector-icons';
23
22
  import { Actions, GiftedChat, IMessage, MessageText, Send } from 'react-native-gifted-chat';
24
23
  import {
25
- useCheckForNewMessagesQuery,
26
- useGetAllUsersQuery,
27
- useThreadMessagesLazyQuery,
28
- useSendThreadMessageMutation,
29
- useMessagesQuery,
30
- useSendMessagesMutation,
31
- useViewChannelDetailQuery,
32
- useUploadFilesNative,
33
- useGetNewMongooseObjectIdQuery,
34
- useOnThreadChatMessageAddedSubscription,
35
- IPreDefinedRole,
36
- useSendExpoNotificationOnPostMutation,
37
24
  IPost,
25
+ IPostThread,
26
+ IPreDefinedRole,
38
27
  OnThreadChatMessageAddedDocument as CHAT_MESSAGE_ADDED,
39
- usePostThreadMessagesQuery,
28
+ useCreatePostThreadMutation,
29
+ useOnThreadChatMessageAddedSubscription,
30
+ useSendExpoNotificationOnPostMutation,
31
+ useSendThreadMessageMutation,
32
+ useThreadMessagesLazyQuery,
33
+ useGetPostThreadLazyQuery,
34
+ useUploadFilesNative,
40
35
  } from '@messenger-box/platform-client';
41
- import { IExpoNotificationData, IFileInfo } from '@messenger-box/core';
36
+ import { IExpoNotificationData, IFileInfo, objectId } from '@messenger-box/core';
42
37
  import { config } from '../config';
43
38
  import { userSelector } from '@adminide-stack/user-auth0-client';
44
- import { SlackMessage, ImageViewerModal } from '../components/SlackMessageContainer';
39
+ import { ImageViewerModal, SlackMessage } from '../components/SlackMessageContainer';
45
40
  import CachedImage from '../components/CachedImage';
46
41
  import { format, isToday, isYesterday } from 'date-fns';
42
+
47
43
  const {
48
44
  MESSAGES_PER_PAGE,
49
45
  CALL_TO_ACTION_BOX_BGCOLOR,
@@ -101,94 +97,20 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
101
97
  const [isShowImageViewer, setImageViewer] = useState<boolean>(false);
102
98
  const [imageObject, setImageObject] = useState<any>({});
103
99
  const [parentId, setParentId] = useState<any>(postParentId);
100
+ const [postThread, setPostThread] = useState<IPostThread>();
104
101
  const { startUpload } = useUploadFilesNative();
105
102
  const [threadPost, setThreadPost] = useState<any[]>([]);
106
- const [skip, setSkip] = useState(0);
107
103
  const [isScrollToBottom, setIsScrollToBottom] = useState(false);
108
104
  const threadMessageListRef = useRef<any>(null);
109
- const { data: mongooseObjectId, refetch: refetchNewPostId } = useGetNewMongooseObjectIdQuery({
110
- fetchPolicy: 'network-only',
111
- // pollInterval: 5000,
112
- });
113
105
 
114
- const {
115
- data: newThreadMsg,
116
- loading: newThreadMsgLoading,
117
- error: newThreadMsgError,
118
- } = useOnThreadChatMessageAddedSubscription({
119
- variables: {
120
- channelId: channelId?.toString(),
121
- postParentId: !parentId || parentId == 0 ? null : parentId?.toString(),
122
- },
123
- });
124
-
125
- const [sendThreadMessage] = useSendThreadMessageMutation();
106
+ // const [sendThreadMessage] = useSendThreadMessageMutation();
107
+ const [sendThreadMessage] = useCreatePostThreadMutation();
126
108
  const [sendExpoNotificationOnPostMutation] = useSendExpoNotificationOnPostMutation();
127
109
 
128
- // const [getThreadMessages, { data: threadMessagesData, loading: threadLoading, refetch: refetchThreadMessages }] =
129
- // useThreadMessagesLazyQuery({
130
- // variables: {
131
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
132
- // role: role?.toString(),
133
- // postParentId: !parentId || parentId == 0 ? null : parentId?.toString(),
134
- // },
135
- // fetchPolicy: 'cache-and-network',
136
- // });
137
-
138
110
  const [
139
111
  getThreadMessages,
140
112
  { data, loading: threadLoading, fetchMore: fetchMoreMessages, refetch: refetchThreadMessages, subscribeToMore },
141
- ] = useThreadMessagesLazyQuery();
142
-
143
- // const {
144
- // data,
145
- // loading: messageLoading,
146
- // refetch,
147
- // fetchMore: fetchMoreMessages,
148
- // subscribeToMore,
149
- // }: any = usePostThreadMessagesQuery({
150
- // variables: {
151
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
152
- // parentId: !parentId || parentId == 0 ? null : parentId?.toString(),
153
- // limit: MESSAGES_PER_PAGE,
154
- // skip: skip,
155
- // },
156
- // skip: !channelId,
157
- // fetchPolicy: 'cache-and-network',
158
- // refetchWritePolicy: 'merge',
159
- // });
160
-
161
- // useFocusEffect(
162
- // React.useCallback(() => {
163
- // navigation?.setOptions({ title: params?.title ?? 'Thread' });
164
- // if (parentId || parentId == 0) {
165
- // refetchThreadMessages({
166
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
167
- // role: role?.toString(),
168
- // postParentId: !parentId || parentId == 0 ? null : parentId?.toString(),
169
- // });
170
- // }
171
- // refetch({
172
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
173
- // parentId: !parentId || parentId == 0 ? null : parentId?.toString(),
174
- // limit: MESSAGES_PER_PAGE,
175
- // skip: 0,
176
- // }).then(({ data }) => {
177
- // if (!data?.messages) {
178
- // return;
179
- // }
180
- // const { data: messages, totalCount }: any = data.messages;
181
- // setTotalCount(totalCount);
182
- // setChannelMessages(messages);
183
- // //setChannelMessages((oldMessages: any) => uniqBy([...oldMessages, ...messages], ({ id }) => id));
184
- // });
185
- // return () => {
186
- // setTotalCount(0);
187
- // setChannelMessages([]);
188
- // setThreadPost([]);
189
- // };
190
- // }, [channelId, parentId]),
191
- // );
113
+ ] = useGetPostThreadLazyQuery({ fetchPolicy: 'cache-and-network' });
192
114
 
193
115
  useFocusEffect(
194
116
  React.useCallback(() => {
@@ -199,10 +121,7 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
199
121
  role: role?.toString(),
200
122
  postParentId: postParentId?.toString(),
201
123
  selectedFields: 'id channel post replies replyCount lastReplyAt createdAt updatedAt',
202
- repliesLimit2: MESSAGES_PER_PAGE,
203
- repliesSkip2: 0,
204
- skip: 0,
205
- limit: 1,
124
+ limit: MESSAGES_PER_PAGE,
206
125
  });
207
126
  }
208
127
  setParentId(postParentId);
@@ -215,10 +134,6 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
215
134
  }, [postParentId]),
216
135
  );
217
136
 
218
- // useEffect(() => {
219
- // setParentId(postParentId);
220
- // }, [postParentId]);
221
-
222
137
  useEffect(() => {
223
138
  //if (parentId && parentId == 0) {
224
139
  if (channelId && parentId) {
@@ -228,22 +143,20 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
228
143
  role: role?.toString(),
229
144
  postParentId: !parentId || parentId == 0 ? null : parentId?.toString(),
230
145
  selectedFields: 'id channel post replies replyCount lastReplyAt createdAt updatedAt',
231
- repliesLimit2: MESSAGES_PER_PAGE,
232
- repliesSkip2: 0,
233
- skip: 0,
234
- limit: 1,
146
+ limit: MESSAGES_PER_PAGE,
235
147
  },
236
148
  });
237
149
  }
238
150
  }, [parentId]);
239
151
 
240
152
  React.useEffect(() => {
241
- if (data?.threadMessages?.data) {
242
- const threads: any = data.threadMessages;
243
- const threadPost = threads?.data?.map((t: any) => t?.post) ?? [];
244
- const threadReplies = threads?.data?.map((t: any) => t?.replies)?.flat(1) ?? [];
245
- const messeageTotalCount = threads?.data?.map((t: any) => t?.replyCount)?.[0] ?? 0;
246
- const messages = [...threadPost, ...threadReplies];
153
+ if (data?.getPostThread) {
154
+ const threads: any = data.getPostThread;
155
+ const threadPost = threads?.post ?? [];
156
+ const threadReplies = threads?.replies ?? [];
157
+ const messeageTotalCount = threads?.replyCount ?? 0;
158
+ const messages = [threadPost, ...threadReplies];
159
+
247
160
  if (
248
161
  (messages && messages.length > 0 && messeageTotalCount > totalCount) ||
249
162
  (messages && messages.length > 0 && channelMessages.length === 0)
@@ -264,136 +177,10 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
264
177
  setTotalCount(messagesTotalCount);
265
178
  };
266
179
 
267
- // useEffect(() => {
268
- // //if (threadMessagesData?.threadMessages?.data) {
269
- // // console.log('threadMessagesData?.threadMessages', JSON.stringify(threadMessagesData?.threadMessages));
270
- // const threads: any = threadMessagesData?.threadMessages;
271
- // if (threadPost?.length == 0 && threads?.data?.length > 0) {
272
- // const threadMessage = threads?.data?.map((t: any) => t?.post);
273
- // setThreadPost(threadMessage);
274
- // setChannelMessages((oldMessages: any) => uniqBy([...threadMessage, ...oldMessages], ({ id }) => id));
275
- // }
276
- // // if (!isPostParentIdThread) {
277
- // // // setTotalCount((pc: any) => pc + threadTotalCount);
278
- // // setChannelMessages((oldMessages: any) => uniqBy([...threadMessage, ...oldMessages], ({ id }) => id));
279
- // // }
280
- // // }
281
- // }, [threadMessagesData, threadPost, isPostParentIdThread]);
282
-
283
- // React.useEffect(() => {
284
- // if (newThreadMsg) {
285
- // console.log('newThreadMsg');
286
- // const msg = newThreadMsg?.threadChatMessageAdded;
287
- // setTotalCount((preCount: any) => preCount + 1);
288
- // setChannelMessages((oldMessages: any) => uniqBy([...oldMessages, msg], ({ id }) => id));
289
- // }
290
- // }, [newThreadMsg]);
291
-
292
- // React.useEffect(() => {
293
- // if (newThreadMsg) {
294
- // console.log('newThreadMsg');
295
- // refetch({
296
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
297
- // parentId: !parentId || parentId == 0 ? null : parentId?.toString(),
298
- // limit: MESSAGES_PER_PAGE,
299
- // skip: 0,
300
- // });
301
- // }
302
- // }, [newThreadMsg, skip, parentId]);
303
-
304
- // useEffect(() => {
305
- // if (data?.messages?.data && (loadingOldMessages || channelMessages.length === 0)) {
306
- // const { data: messages, totalCount: messeageTotalCount } = data.messages;
307
- // if (messages && messages.length > 0) {
308
- // setChannelMessages((oldMessages: any) => uniqBy([...messages, ...oldMessages], ({ id }) => id));
309
- // setLoadingOldMessages(false);
310
- // }
311
- // if (totalCount !== messeageTotalCount) setTotalCount(messeageTotalCount);
312
- // }
313
- // }, [data, loadingOldMessages, channelMessages, totalCount]);
314
-
315
- // useEffect(() => {
316
- // if ((data?.messages?.data && channelMessages.length === 0) || (data?.messages?.data && loadingOldMessages)) {
317
- // const { data: messages, totalCount: messeageTotalCount } = data.messages;
318
- // if (messages && messages.length > 0) {
319
- // setChannelMessages((oldMessages: any) => uniqBy([...messages, ...oldMessages], ({ id }) => id));
320
- // setTotalCount(messeageTotalCount);
321
- // }
322
- // setLoadEarlierMsg(false);
323
- // if (loadingOldMessages) setLoadingOldMessages(false);
324
- // }
325
- // if (threadPost?.length && data?.messages?.totalCount == channelMessages.length) {
326
- // // setTotalCount((pc: any) => pc + threadTotalCount);
327
- // setChannelMessages((oldMessages: any) => uniqBy([...threadPost, ...oldMessages], ({ id }) => id));
328
- // }
329
- // }, [data, loadingOldMessages, channelMessages, threadPost]);
330
-
331
- // React.useEffect(() => {
332
- // if (data?.messages?.data) {
333
- // const { data: messages, totalCount: messeageTotalCount } = data.messages;
334
- // if (
335
- // (messages && messages.length > 0 && messeageTotalCount > totalCount) ||
336
- // (messages && messages.length > 0 && (loadingOldMessages || channelMessages.length === 0))
337
- // ) {
338
- // setChannelMessages((oldMessages: any) => uniqBy([...messages, ...oldMessages], ({ id }) => id));
339
- // setTotalCount(messeageTotalCount);
340
- // setLoadEarlierMsg(false);
341
- // }
342
-
343
- // if (loadingOldMessages && channelMessages) setLoadingOldMessages(false);
344
-
345
- // // if (
346
- // // channelMessages &&
347
- // // channelMessages?.length == MESSAGES_PER_PAGE &&
348
- // // totalCount > channelMessages?.length
349
- // // ) {
350
- // // onFetchOld();
351
- // // }
352
- // }
353
-
354
- // // if (threadPost?.length > 0 && channelMessages.length == (0 || MESSAGES_PER_PAGE)) {
355
- // // setChannelMessages((oldMessages: any) => uniqBy([...threadPost, ...oldMessages], ({ id }) => id));
356
- // // }
357
- // }, [data, loadingOldMessages, totalCount, channelMessages, isScrollToBottom]);
358
-
359
- // useEffect(() => {
360
- // if (
361
- // !messageLoading &&
362
- // checkForMessages?.messages?.totalCount &&
363
- // checkForMessages?.messages?.totalCount > totalCount
364
- // ) {
365
- // const numberOfNewMessages = checkForMessages?.messages?.totalCount - totalCount;
366
- // console.log('new msg check');
367
- // refetch({
368
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
369
- // parentId: !parentId || parentId == 0 ? null : parentId?.toString(),
370
- // limit: numberOfNewMessages,
371
- // }).then(({ data }) => {
372
- // if (!data?.messages) {
373
- // return;
374
- // }
375
- // const { data: messages, totalCount }: any = data.messages;
376
- // setTotalCount(totalCount);
377
- // setChannelMessages((oldMessages: any) => uniqBy([...oldMessages, ...messages], ({ id }) => id));
378
- // });
379
- // }
380
- // }, [checkForMessages, totalCount, parentId]);
381
-
382
180
  React.useEffect(() => {
383
181
  if (selectedImage) setImageLoading(false);
384
182
  }, [selectedImage]);
385
183
 
386
- // const onFetchOld = useCallback(() => {
387
- // if (data?.messages?.totalCount > channelMessages.length) {
388
- // setLoadEarlierMsg(true);
389
- // refetch({
390
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
391
- // parentId: !parentId || parentId == 0 ? null : parentId?.toString(),
392
- // skip: channelMessages.length,
393
- // })?.then((res: any) => setLoadingOldMessages(true));
394
- // }
395
- // }, [data, channelMessages]);
396
-
397
184
  const scrollToBottom = React.useCallback(() => {
398
185
  if (threadMessageListRef?.current) {
399
186
  setIsScrollToBottom(false);
@@ -401,20 +188,8 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
401
188
  }
402
189
  }, [threadMessageListRef]);
403
190
 
404
- // const onFetchOld = useCallback((data: any, channelMessages: any) => {
405
- // if (data?.messages?.totalCount > channelMessages.length) {
406
- // setLoadEarlierMsg(true);
407
- // refetch({
408
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
409
- // parentId: !parentId || parentId == 0 ? null : parentId?.toString(),
410
- // skip: channelMessages.length,
411
- // })?.then((res: any) => setLoadingOldMessages(true));
412
- // }
413
- // }, []);
414
-
415
191
  const onFetchOld = useCallback(() => {
416
192
  if (totalCount > channelMessages.length && !loadingOldMessages) {
417
- setSkip(channelMessages.length);
418
193
  setLoadEarlierMsg(true);
419
194
  fetchMoreMessages({
420
195
  variables: {
@@ -422,21 +197,15 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
422
197
  role: role?.toString(),
423
198
  postParentId: parentId?.toString(),
424
199
  selectedFields: 'id channel post replies replyCount lastReplyAt createdAt updatedAt',
425
- repliesLimit2: MESSAGES_PER_PAGE,
426
- repliesSkip2: channelMessages.length - 1,
427
- skip: 0,
428
- limit: 1,
429
- // channelId: channelId?.toString(),
430
- // parentId: null,
431
- // skip: channelMessages.length,
200
+ limit: MESSAGES_PER_PAGE,
201
+ skip: channelMessages.length - 1,
432
202
  },
433
203
  })
434
204
  .then((res: any) => {
435
- if (res?.data?.threadMessages?.data) {
436
- const threadReplies =
437
- res?.data?.threadMessages?.data?.map((t: any) => t?.replies)?.flat(1) ?? [];
438
- const messeageTotalCount =
439
- res?.data?.threadMessages?.data?.map((t: any) => t?.replyCount)?.[0] ?? 0;
205
+ if (res?.data?.getPostThread) {
206
+ const threads: any = res?.data?.getPostThread;
207
+ const threadReplies = threads?.replies ?? [];
208
+ const messeageTotalCount = threads?.replyCount ?? 0;
440
209
  setThreadMessages(threadReplies, messeageTotalCount);
441
210
  }
442
211
  })
@@ -448,10 +217,6 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
448
217
  setLoadEarlierMsg(false);
449
218
  setLoadingOldMessages(false);
450
219
  });
451
- //?.then((res: any) => setLoadingOldMessages(true));
452
- // refetch({ channelId: channelId?.toString(), parentId: null, skip: channelMessages.length })?.then(
453
- // (res: any) => setLoadingOldMessages(true),
454
- // );
455
220
  }
456
221
  }, [parentId, channelId, totalCount, channelMessages]);
457
222
 
@@ -495,42 +260,14 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
495
260
  if (imageSource.cancelled) setLoading(false);
496
261
  };
497
262
 
498
- // const sendPushNotification = async (title: String, body: String, data: any, to: any) => {
499
- // try {
500
- // const response = await fetch('https://exp.host/--/api/v2/push/send/', {
501
- // method: 'POST',
502
- // headers: {
503
- // Accept: 'application/json',
504
- // 'Accept-Encoding': 'gzip, deflate',
505
- // 'Content-Type': 'application/json',
506
- // },
507
- // body: JSON.stringify({
508
- // to: to,
509
- // data: data,
510
- // title: title,
511
- // body: body,
512
- // sound: Platform.OS === 'android' ? undefined || null : 'default',
513
- // }),
514
- // });
515
- // const result: any = await response.json();
516
- // console.log('expo api response', result);
517
- // } catch (error) {
518
- // console.error('Error:', error);
519
- // }
520
- // };
521
-
522
- // const ObjectId = (m = Math, d = Date, h = 16, s = (s:any) => m.floor(s).toString(h)) =>
523
- // s(d.now() / 1000) + ' '.repeat(h).replace(/./g, () => s(m.random() * h))
524
-
525
263
  const handleSend = useCallback(
526
264
  async (message: string) => {
527
265
  if (!channelId) return;
528
266
  if (!message && message != ' ' && images.length == 0) return;
529
267
 
268
+ const postId = objectId();
269
+
530
270
  if (images && images.length > 0) {
531
- const newPostId = await refetchNewPostId();
532
- const postId: any = newPostId?.data?.getNewMongooseObjectId?.toString();
533
- // const postId: any = mongooseObjectId?.getNewMongooseObjectId;
534
271
  setLoading(true);
535
272
  const uploadResponse = await startUpload({
536
273
  file: images,
@@ -557,6 +294,7 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
557
294
  variables: {
558
295
  postId,
559
296
  channelId,
297
+ postThreadId: postThread && postThread?.id,
560
298
  postParentId: !parentId || parentId == 0 ? null : parentId,
561
299
  threadMessageInput: {
562
300
  content: message,
@@ -569,37 +307,17 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
569
307
  setLoading(false);
570
308
  return;
571
309
  }
572
- const responseMessage = data?.sendThreadMessage?.lastMessage;
310
+ setPostThread(data?.createPostThread?.data);
311
+ const lastMessageId = data?.createPostThread?.lastMessage?.id;
573
312
  if (!parentId || parentId == 0) {
574
- setParentId(responseMessage?.id);
575
- // setChannelMessages((messages: any) => [
576
- // ...messages,
577
- // {
578
- // ...responseMessage,
579
- // files: {
580
- // totalCount: uploadedFiles.length,
581
- // data: uploadedFiles,
582
- // },
583
- // },
584
- // ]);
585
-
586
- // setTotalCount((t: any) => t + 1);
313
+ setParentId(data?.createPostThread?.lastMessage?.id);
587
314
  }
588
315
 
589
316
  setChannelToTop(channelToTop + 1);
590
317
  setLoading(false);
591
318
  setMsg('');
592
-
593
- // if (!parentId || parentId == 0) {
594
- // setParentId(responseMessage?.id);
595
- // }
596
319
  const msg = message == '' ? 'Send a file' : message;
597
- sendPushNotification(
598
- responseMessage,
599
- channelId,
600
- parentId,
601
- data?.sendThreadMessage?.data?.id,
602
- );
320
+ sendPushNotification(lastMessageId, channelId, parentId, data?.createPostThread?.data?.id);
603
321
  },
604
322
  });
605
323
  }
@@ -608,6 +326,7 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
608
326
  await sendThreadMessage({
609
327
  variables: {
610
328
  channelId,
329
+ postThreadId: postThread && postThread?.id,
611
330
  postParentId: !parentId || parentId == 0 ? null : parentId,
612
331
  threadMessageInput: {
613
332
  content: message,
@@ -619,27 +338,24 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
619
338
  setLoading(false);
620
339
  return;
621
340
  }
622
- const responseMessage = data?.sendThreadMessage?.lastMessage;
341
+ setPostThread(data?.createPostThread.data);
342
+ const lastMessageId = data?.createPostThread?.lastMessage?.id;
623
343
  if (!parentId || parentId == 0) {
624
- setParentId(responseMessage?.id);
625
- // setChannelMessages((messages: any) => [...messages, responseMessage]);
626
- // setTotalCount((t: any) => t + 1);
344
+ setParentId(data?.createPostThread?.lastMessage?.id);
627
345
  }
628
346
  setChannelToTop(channelToTop + 1);
629
347
  setLoading(false);
630
348
  setMsg('');
631
- // if (!parentId || parentId == 0) {
632
- // setParentId(responseMessage?.id);
633
- // }
634
- sendPushNotification(responseMessage, channelId, parentId, data?.sendThreadMessage?.data?.id);
349
+
350
+ sendPushNotification(lastMessageId, channelId, parentId, data?.createPostThread?.data?.id);
635
351
  },
636
352
  });
637
353
  }
638
354
  },
639
- [mongooseObjectId, setChannelMessages, channelId, images, parentId, expoTokens],
355
+ [setChannelMessages, channelId, images, parentId, expoTokens],
640
356
  );
641
357
 
642
- const sendPushNotification = async (post: IPost, channelId: string, parentId: any, threadId: string) => {
358
+ const sendPushNotification = async (messageId: string, channelId: string, parentId: any, threadId: string) => {
643
359
  const notificationData: IExpoNotificationData = {
644
360
  url: config.THREAD_MESSEGE_PATH,
645
361
  params: { channelId, title: params?.title ?? 'Thread', postParentId: parentId, hideTabBar: true },
@@ -650,55 +366,13 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
650
366
  if (parentId || parentId == 0) {
651
367
  await sendExpoNotificationOnPostMutation({
652
368
  variables: {
653
- postId: post?.id?.toString(),
369
+ postId: messageId?.toString(),
654
370
  notificationData,
655
371
  },
656
372
  });
657
373
  }
658
374
  };
659
375
 
660
- // const fetchTokenAndSendPushNotification = (message: any, channelId: any, parentId: any) => {
661
- // const givenName = auth?.profile?.given_name ?? '';
662
- // const familyName = auth?.profile?.family_name ?? '';
663
- // const fullName = givenName ? givenName + ' ' + familyName : '';
664
- // const title: String = fullName ? fullName : 'Message';
665
- // const body: String = message;
666
- // const notificationData: any = {
667
- // url: config.THREAD_MESSEGE_PATH,
668
- // params: { channelId, title: params?.title ?? 'Thread', postParentId: parentId, hideTabBar: true },
669
- // screen: 'DialogThreadMessages',
670
- // };
671
- // if (parentId || parentId == 0) {
672
- // refetchThreadMessages({
673
- // channelId: !parentId || parentId == 0 ? null : channelId?.toString(),
674
- // role: role?.toString(),
675
- // postParentId: !parentId || parentId == 0 ? null : parentId?.toString(),
676
- // })?.then((res: any) => {
677
- // if (res?.data?.threadMessages?.data?.length > 0) {
678
- // const participants =
679
- // res?.data?.threadMessages?.data?.map((t: any) => t?.participants)?.flat(1) ?? [];
680
- // const participantsTokens =
681
- // participants?.length > 0
682
- // ? participants
683
- // ?.filter((u: any) => u?.id != auth?.id)
684
- // ?.map((p: any) => p.tokens)
685
- // ?.flat(1)
686
- // : [];
687
- // const expoTokens =
688
- // participantsTokens?.length > 0
689
- // ? participantsTokens
690
- // ?.filter((t: any) => t?.type == 'EXPO_NOTIFICATION_TOKEN')
691
- // ?.map((et: any) => et?.token)
692
- // : [];
693
- // if (expoTokens?.length > 0) {
694
- // const to: any = expoTokens;
695
- // sendPushNotification(title, body, notificationData, to);
696
- // }
697
- // }
698
- // });
699
- // }
700
- // };
701
-
702
376
  const messageList = useMemo(() => {
703
377
  let currentDate = '';
704
378
  let res: any = [];
@@ -1010,46 +684,20 @@ const ThreadConversationViewComponent = ({ channelId, postParentId, isPostParent
1010
684
  },
1011
685
  updateQuery: (prev, { subscriptionData }: any) => {
1012
686
  if (!subscriptionData.data) return prev;
1013
-
1014
687
  const newMessage: any = subscriptionData?.data?.threadChatMessageAdded;
1015
- const threadReplies =
1016
- prev?.threadMessages?.data?.map((t: any) => t?.replies)?.flat(1) ?? [];
1017
- const replies = [...threadReplies, newMessage];
1018
- const previousData = prev?.threadMessages?.data?.map((t: any) => {
1019
- let thread = { ...t };
1020
- thread.replies = replies;
1021
- return thread;
1022
- });
1023
-
1024
- // const previousData = prev?.threadMessages?.data ? [...prev.threadMessages.data, newMessage] : [];
1025
- // const totalMsgCount = prev?.threadMessages?.totalCount + 1;
1026
- const merged = {
1027
- ...prev,
1028
- threadMessages: {
1029
- ...prev?.threadMessages,
1030
- data: previousData,
688
+ const prevReplyCount: any = prev?.getPostThread?.replyCount;
689
+ const newReplyCount = prevReplyCount || 0 + 1;
690
+ const replies = prev?.getPostThread?.replies || [];
691
+
692
+ return Object.assign({}, prev, {
693
+ getPostThread: {
694
+ ...prev?.getPostThread,
695
+ lastReplyAt: newMessage.createdAt,
696
+ replies: [newMessage, ...replies],
697
+ replyCount: newReplyCount,
698
+ updatedAt: newMessage.createdAt,
1031
699
  },
1032
- };
1033
- return merged;
1034
- // const mergedData = prev?.messages?.data
1035
- // ? [...prev.messages.data, newMessage]
1036
- // : [];
1037
- // const totalMsgCount = prev?.messages?.totalCount + 1;
1038
- // const merged = {
1039
- // ...prev?.messages,
1040
- // messages: {
1041
- // ...prev?.messages,
1042
- // data: mergedData,
1043
- // totalCount: totalMsgCount,
1044
- // },
1045
- // };
1046
- // return merged;
1047
- // return Object.assign({}, prev, {
1048
- // messages: {
1049
- // data: [...prev.messages.data, newMessage],
1050
- // totalCount: prev.messages.totalCount + 1,
1051
- // },
1052
- // });
700
+ });
1053
701
  },
1054
702
  })
1055
703
  }