@messenger-box/platform-mobile 0.0.1-alpha.391 → 0.0.1-alpha.392

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@messenger-box/platform-mobile",
3
- "version": "0.0.1-alpha.391",
3
+ "version": "0.0.1-alpha.392",
4
4
  "description": "Sample core for higher packages to depend on",
5
5
  "license": "ISC",
6
6
  "author": "CDMBase LLC",
@@ -19,8 +19,8 @@
19
19
  "watch": "yarn build:lib:watch"
20
20
  },
21
21
  "dependencies": {
22
- "@messenger-box/core": "0.0.1-alpha.391",
23
- "@messenger-box/platform-client": "0.0.1-alpha.391",
22
+ "@messenger-box/core": "0.0.1-alpha.392",
23
+ "@messenger-box/platform-client": "0.0.1-alpha.392",
24
24
  "base-64": "1.0.0",
25
25
  "react-native-gifted-chat": "1.0.4"
26
26
  },
@@ -40,5 +40,5 @@
40
40
  "typescript": {
41
41
  "definition": "lib/index.d.ts"
42
42
  },
43
- "gitHead": "8d25492999ac0d8328186716751cd8966e63225c"
43
+ "gitHead": "b5ef43048bcad9378d61be44eb72fcb9800f820e"
44
44
  }
@@ -2,7 +2,14 @@ import React, { useMemo } from 'react';
2
2
  import { Text, Image, Pressable, HStack, Stack, Box, Avatar, View } from 'native-base';
3
3
  import { format, isToday, isYesterday } from 'date-fns';
4
4
  import { useFocusEffect } from '@react-navigation/native';
5
- import { IChannel, IUserAccount, useMessagesQuery, useUserAccountQuery } from '@messenger-box/platform-client';
5
+ import {
6
+ IChannel,
7
+ ISortEnum,
8
+ IUserAccount,
9
+ useMessagesQuery,
10
+ useOnChatMessageAddedSubscription,
11
+ useUserAccountQuery,
12
+ } from '@messenger-box/platform-client';
6
13
  import { startCase } from 'lodash';
7
14
 
8
15
  const createdAtText = (value: string) => {
@@ -37,7 +44,7 @@ export const DialogsListItemComponent: React.FC<IDialogListItemProps> = function
37
44
  channel,
38
45
  onOpen,
39
46
  }) {
40
- const parentId:any = null;
47
+ const parentId: any = null;
41
48
  const {
42
49
  data: messagesQuery,
43
50
  loading: messageLoading,
@@ -46,15 +53,39 @@ export const DialogsListItemComponent: React.FC<IDialogListItemProps> = function
46
53
  variables: {
47
54
  channelId: channel?.id?.toString(),
48
55
  parentId: parentId,
49
- limit: 25,
56
+ limit: 10,
57
+ sort: {
58
+ key: 'updatedAt',
59
+ value: ISortEnum.Desc,
60
+ },
61
+ //limit: 25,
50
62
  },
51
63
  fetchPolicy: 'cache-and-network',
52
64
  });
53
65
 
66
+ const {
67
+ data: newMessage,
68
+ loading: newMsgLoading,
69
+ error: newMsgError,
70
+ }: any = useOnChatMessageAddedSubscription({
71
+ variables: {
72
+ channelId: channel?.id?.toString(),
73
+ },
74
+ });
75
+
54
76
  useFocusEffect(
55
77
  React.useCallback(() => {
56
78
  // Do something when the screen is focused
57
- refetchMessages({ channelId: channel?.id?.toString(),parentId:parentId, limit: 25 });
79
+ refetchMessages({
80
+ channelId: channel?.id?.toString(),
81
+ parentId: parentId,
82
+ limit: 10,
83
+ sort: {
84
+ key: 'updatedAt',
85
+ value: ISortEnum.Desc,
86
+ },
87
+ //limit: 25
88
+ });
58
89
 
59
90
  return () => {
60
91
  // Do something when the screen is unfocused
@@ -63,6 +94,20 @@ export const DialogsListItemComponent: React.FC<IDialogListItemProps> = function
63
94
  }, []),
64
95
  );
65
96
 
97
+ React.useEffect(() => {
98
+ if (newMessage) {
99
+ refetchMessages({
100
+ channelId: channel?.id?.toString(),
101
+ parentId: parentId,
102
+ limit: 10,
103
+ sort: {
104
+ key: 'updatedAt',
105
+ value: ISortEnum.Desc,
106
+ },
107
+ });
108
+ }
109
+ }, [newMessage]);
110
+
66
111
  const lastMessage = useMemo(() => {
67
112
  if (!messagesQuery?.messages?.data?.length) {
68
113
  return null;
@@ -1,7 +1,7 @@
1
1
  import { cleanEnv, num, str } from 'envalid';
2
2
 
3
3
  export const config = cleanEnv(process['APP_ENV'] || process.env, {
4
- MESSAGES_PER_PAGE: num({ devDefault: 10, default: 20 }),
4
+ MESSAGES_PER_PAGE: num({ devDefault: 10, default: 10 }),
5
5
  FILES_PER_MESSAGE: num({ default: 10 }),
6
6
  INBOX_MESSEGE_PATH: str({ default: 'MainStack.Message' }),
7
7
  // THREAD_MESSEGE_PATH: str({ default: 'MainStack.Thread' }),
@@ -1,4 +1,4 @@
1
- import React, { useCallback, useEffect, useMemo, useState } from 'react';
1
+ import React, { useCallback, useEffect, useMemo, useState, useRef } from 'react';
2
2
  import {
3
3
  Box,
4
4
  Button,
@@ -31,6 +31,7 @@ import {
31
31
  useOnChatMessageAddedSubscription,
32
32
  IPreDefinedRole,
33
33
  useSendExpoNotificationOnPostMutation,
34
+ OnChatMessageAddedDocument as CHAT_MESSAGE_ADDED,
34
35
  IPost,
35
36
  } from '@messenger-box/platform-client';
36
37
  import { IExpoNotificationData, IFileInfo } from '@messenger-box/core';
@@ -55,6 +56,10 @@ const createdAtText = (value: string) => {
55
56
  return format(new Date(value), 'MMM dd, yyyy');
56
57
  };
57
58
 
59
+ interface ISubscriptionHandlerProps {
60
+ subscribeToNewMessages: () => any;
61
+ }
62
+
58
63
  interface IMessageProps extends IMessage {
59
64
  type: string;
60
65
  propsConfiguration?: any;
@@ -90,21 +95,23 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
90
95
  const [expoTokens, setExpoTokens] = useState<any[]>([]);
91
96
  const [isShowImageViewer, setImageViewer] = useState<boolean>(false);
92
97
  const [imageObject, setImageObject] = useState<any>({});
98
+ const [skip, setSkip] = useState(0);
99
+ const messageRootListRef = useRef<any>(null);
93
100
  const isFocused = useIsFocused();
94
- const { data: mongooseObjectId,refetch:refetchNewPostId } = useGetNewMongooseObjectIdQuery({
101
+ const { data: mongooseObjectId, refetch: refetchNewPostId } = useGetNewMongooseObjectIdQuery({
95
102
  fetchPolicy: 'network-only',
96
103
  //pollInterval: 5000,
97
104
  });
98
105
 
99
- const {
100
- data: newMessage,
101
- loading: newMsgLoading,
102
- error: newMsgError,
103
- }: any = useOnChatMessageAddedSubscription({
104
- variables: {
105
- channelId: channelId?.toString(),
106
- },
107
- });
106
+ // const {
107
+ // data: newMessage,
108
+ // loading: newMsgLoading,
109
+ // error: newMsgError,
110
+ // }: any = useOnChatMessageAddedSubscription({
111
+ // variables: {
112
+ // channelId: channelId?.toString(),
113
+ // },
114
+ // });
108
115
 
109
116
  const { startUpload } = useUploadFilesNative();
110
117
 
@@ -116,15 +123,20 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
116
123
  data,
117
124
  loading: messageLoading,
118
125
  refetch,
126
+ fetchMore: fetchMoreMessages,
127
+ subscribeToMore,
119
128
  }: any = useMessagesQuery({
120
129
  variables: {
121
130
  channelId: channelId?.toString(),
122
131
  parentId: null,
123
132
  limit: MESSAGES_PER_PAGE,
133
+ skip: skip,
124
134
  },
125
135
  skip: !channelId,
126
136
  fetchPolicy: 'cache-and-network',
127
- nextFetchPolicy: 'cache-first',
137
+ refetchWritePolicy: 'merge',
138
+ // fetchPolicy: 'cache-and-network',
139
+ // nextFetchPolicy: 'cache-first',
128
140
  });
129
141
 
130
142
  const {
@@ -150,11 +162,13 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
150
162
  useFocusEffect(
151
163
  React.useCallback(() => {
152
164
  // Do something when the screen is focused
165
+ setSkip(0);
153
166
  refetchChannelDetail({ id: channelId?.toString() });
154
167
  refetch({
155
168
  channelId: channelId?.toString(),
156
169
  parentId: null,
157
170
  limit: MESSAGES_PER_PAGE,
171
+ skip: 0,
158
172
  }).then(({ data }) => {
159
173
  if (!data?.messages) {
160
174
  return;
@@ -234,25 +248,49 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
234
248
 
235
249
  // }, [data, channelMessages,loadingOldMessages]);
236
250
 
251
+ // useEffect(() => {
252
+ // if ((data?.messages?.data && channelMessages.length === 0) || (data?.messages?.data && loadingOldMessages)) {
253
+ // const { data: messages, totalCount: messeageTotalCount } = data.messages;
254
+ // if (messages && messages.length > 0) {
255
+ // setChannelMessages((oldMessages: any) => uniqBy([...messages, ...oldMessages], ({ id }) => id));
256
+ // setTotalCount(messeageTotalCount);
257
+ // }
258
+ // setLoadEarlierMsg(false);
259
+ // if (loadingOldMessages) setLoadingOldMessages(false);
260
+ // }
261
+ // }, [data, loadingOldMessages, channelMessages]);
262
+
237
263
  useEffect(() => {
238
- if ((data?.messages?.data && channelMessages.length === 0) || (data?.messages?.data && loadingOldMessages)) {
264
+ if (data?.messages?.data) {
239
265
  const { data: messages, totalCount: messeageTotalCount } = data.messages;
240
- if (messages && messages.length > 0) {
266
+ if (
267
+ (messages && messages.length > 0 && messeageTotalCount > totalCount) ||
268
+ (messages && messages.length > 0 && (loadingOldMessages || channelMessages.length === 0))
269
+ ) {
241
270
  setChannelMessages((oldMessages: any) => uniqBy([...messages, ...oldMessages], ({ id }) => id));
242
271
  setTotalCount(messeageTotalCount);
272
+ setLoadEarlierMsg(false);
243
273
  }
244
- setLoadEarlierMsg(false);
245
- if (loadingOldMessages) setLoadingOldMessages(false);
246
- }
247
- }, [data, loadingOldMessages, channelMessages]);
248
274
 
249
- React.useEffect(() => {
250
- if (newMessage) {
251
- const msg = newMessage?.chatMessageAdded;
252
- setTotalCount((preCount: any) => preCount + 1);
253
- setChannelMessages((oldMessages: any) => uniqBy([...oldMessages, msg], ({ id }) => id));
275
+ if (loadingOldMessages && channelMessages) setLoadingOldMessages(false);
276
+
277
+ if (
278
+ channelMessages &&
279
+ channelMessages?.length == MESSAGES_PER_PAGE &&
280
+ totalCount > channelMessages?.length
281
+ ) {
282
+ onFetchOld();
283
+ }
254
284
  }
255
- }, [newMessage]);
285
+ }, [data, loadingOldMessages, channelMessages, totalCount]);
286
+
287
+ // React.useEffect(() => {
288
+ // if (newMessage) {
289
+ // const msg = newMessage?.chatMessageAdded;
290
+ // setTotalCount((preCount: any) => preCount + 1);
291
+ // setChannelMessages((oldMessages: any) => uniqBy([...oldMessages, msg], ({ id }) => id));
292
+ // }
293
+ // }, [newMessage]);
256
294
 
257
295
  // useEffect(() => {
258
296
  // if (data?.messages?.data) {
@@ -335,14 +373,24 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
335
373
  // }
336
374
  // }, [data, channelMessages]);
337
375
 
338
- const onFetchOld = useCallback((data: any, channelMessages: any) => {
339
- if (data?.messages?.totalCount > channelMessages.length) {
376
+ const onFetchOld = useCallback(() => {
377
+ if (totalCount > channelMessages.length && !loadingOldMessages) {
378
+ setSkip(channelMessages.length);
340
379
  setLoadEarlierMsg(true);
341
- refetch({ channelId: channelId?.toString(), parentId: null, skip: channelMessages.length })?.then(
342
- (res: any) => setLoadingOldMessages(true),
343
- );
380
+ fetchMoreMessages({
381
+ variables: {
382
+ channelId: channelId?.toString(),
383
+ parentId: null,
384
+ skip: channelMessages.length,
385
+ },
386
+ })?.then((res: any) => {
387
+ setLoadingOldMessages(true);
388
+ });
389
+ // refetch({ channelId: channelId?.toString(), parentId: null, skip: channelMessages.length })?.then(
390
+ // (res: any) => setLoadingOldMessages(true),
391
+ // );
344
392
  }
345
- }, []);
393
+ }, [totalCount, channelMessages]);
346
394
 
347
395
  // const onFetchOld = () => {
348
396
  // if(totalCount > channelMessages.length){
@@ -534,7 +582,7 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
534
582
  if (images && images.length > 0) {
535
583
  const newPostId = await refetchNewPostId();
536
584
  const postId: any = newPostId?.data?.getNewMongooseObjectId?.toString();
537
- // const postId: any = mongooseObjectId?.getNewMongooseObjectId;
585
+ // const postId: any = mongooseObjectId?.getNewMongooseObjectId;
538
586
  setLoading(true);
539
587
  const uploadResponse = await startUpload({
540
588
  file: images,
@@ -1102,19 +1150,36 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
1102
1150
  // }
1103
1151
  // };
1104
1152
 
1153
+ const onMomentumScrollBegin = ({ nativeEvent }: any) => {
1154
+ onScroll = true;
1155
+ console.log('scroll top');
1156
+ if (!loadingOldMessages && isCloseToTop(nativeEvent) && totalCount > channelMessages?.length) {
1157
+ onFetchOld();
1158
+ }
1159
+ };
1160
+
1161
+ const onEndReached = () => {
1162
+ console.log('on end reached');
1163
+ if (!onScroll) return;
1164
+ // load messages, show ActivityIndicator
1165
+ onScroll = false;
1166
+ // setLoadingOldMessages(true);
1167
+ };
1168
+
1105
1169
  return (
1106
1170
  <>
1107
1171
  {loadEarlierMsg && <Spinner />}
1108
1172
 
1109
1173
  <GiftedChat
1174
+ ref={messageRootListRef}
1110
1175
  wrapInSafeArea={false}
1111
1176
  renderLoading={() => <Spinner />}
1112
1177
  messages={messageList}
1113
- // listViewProps={{
1114
- // onEndReached: onEndReached,
1115
- // onEndReachedThreshold: 0.5,
1116
- // onMomentumScrollBegin: onMomentumScrollBegin,
1117
- // }}
1178
+ listViewProps={{
1179
+ onEndReached: onEndReached,
1180
+ onEndReachedThreshold: 0.5,
1181
+ onMomentumScrollBegin: onMomentumScrollBegin,
1182
+ }}
1118
1183
  // listViewProps={{
1119
1184
  // scrollEventThrottle: 400,
1120
1185
  // onScroll: ({ nativeEvent }) => { console.log('scroll')
@@ -1137,33 +1202,74 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
1137
1202
  //onLoadEarlier={onFetchOld}
1138
1203
  //infiniteScroll={true}
1139
1204
  renderSend={renderSend}
1140
- loadEarlier={data?.messages?.totalCount > channelMessages.length}
1141
- isLoadingEarlier={loadEarlierMsg}
1205
+ // loadEarlier={data?.messages?.totalCount > channelMessages.length}
1206
+ //isLoadingEarlier={loadEarlierMsg}
1142
1207
  //extraData={{ isLoadingEarlier: loadingOldMessages }}
1143
- renderLoadEarlier={() =>
1144
- !loadEarlierMsg && (
1145
- <Center py={2}>
1146
- <Button
1147
- onPress={() => onFetchOld(data, channelMessages)}
1148
- variant={'outline'}
1149
- _text={{ color: 'black', fontSize: 15, fontWeight: 'bold' }}
1150
- >
1151
- Load earlier messages
1152
- </Button>
1153
- </Center>
1154
- )
1155
- }
1208
+ // renderLoadEarlier={() =>
1209
+ // !loadEarlierMsg && (
1210
+ // <Center py={2}>
1211
+ // <Button
1212
+ // onPress={() => onFetchOld()}
1213
+ // variant={'outline'}
1214
+ // _text={{ color: 'black', fontSize: 15, fontWeight: 'bold' }}
1215
+ // >
1216
+ // Load earlier messages
1217
+ // </Button>
1218
+ // </Center>
1219
+ // )
1220
+ // }
1156
1221
  renderMessageText={renderMessageText}
1157
1222
  minInputToolbarHeight={50}
1158
1223
  renderActions={renderActions}
1159
1224
  renderAccessory={renderAccessory}
1160
1225
  renderMessage={renderMessage}
1161
1226
  renderChatFooter={() => (
1162
- <ImageViewerModal
1163
- isVisible={isShowImageViewer}
1164
- setVisible={setImageViewer}
1165
- modalContent={modalContent}
1166
- />
1227
+ <>
1228
+ <ImageViewerModal
1229
+ isVisible={isShowImageViewer}
1230
+ setVisible={setImageViewer}
1231
+ modalContent={modalContent}
1232
+ />
1233
+ <SubscriptionHandler
1234
+ subscribeToNewMessages={() =>
1235
+ subscribeToMore({
1236
+ document: CHAT_MESSAGE_ADDED,
1237
+ variables: {
1238
+ channelId: channelId?.toString(),
1239
+ },
1240
+ updateQuery: (prev, { subscriptionData }: any) => {
1241
+ if (!subscriptionData.data) return prev;
1242
+
1243
+ const newMessage: any = subscriptionData?.data?.chatMessageAdded;
1244
+ const previousData = prev?.messages?.data
1245
+ ? [...prev.messages.data, newMessage]
1246
+ : [];
1247
+ const totalMsgCount = prev?.messages?.totalCount + 1;
1248
+ // const merged = {
1249
+ // ...prev,
1250
+ // data: previousData,
1251
+ // totalCount: totalMsgCount,
1252
+ // };
1253
+ const merged = {
1254
+ ...prev,
1255
+ messages: {
1256
+ ...prev?.messages,
1257
+ data: previousData,
1258
+ totalCount: totalMsgCount,
1259
+ },
1260
+ };
1261
+ return merged;
1262
+ // return Object.assign({}, prev, {
1263
+ // messages: {
1264
+ // data: [...prev.messages.data, newMessage],
1265
+ // totalCount: prev.messages.totalCount + 1,
1266
+ // },
1267
+ // });
1268
+ },
1269
+ })
1270
+ }
1271
+ />
1272
+ </>
1167
1273
  )}
1168
1274
  lightboxProps={{
1169
1275
  underlayColor: 'transparent',
@@ -1175,4 +1281,9 @@ const ConversationViewComponent = ({ channelId, role }: any) => {
1175
1281
  );
1176
1282
  };
1177
1283
 
1284
+ const SubscriptionHandler = ({ subscribeToNewMessages }: ISubscriptionHandlerProps) => {
1285
+ useEffect(() => subscribeToNewMessages(), []);
1286
+ return <></>;
1287
+ };
1288
+
1178
1289
  export const ConversationView = React.memo(ConversationViewComponent);