@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.
@@ -5,7 +5,11 @@ import { useSelector, useDispatch } from 'react-redux';
5
5
  import { useNavigation, useRoute, useIsFocused, useFocusEffect } from '@react-navigation/native';
6
6
  import { orderBy, uniqBy, startCase } from 'lodash';
7
7
  import { ThreadViewItem } from '../components/ThreadsViewItem';
8
- import { useThreadMessagesQuery, useOnThreadCreatedUpdatedSubscription } from '@messenger-box/platform-client';
8
+ import {
9
+ useThreadMessagesQuery,
10
+ OnThreadCreatedUpdatedDocument as THREAD_CHAT_ADDED,
11
+ useOnThreadCreatedUpdatedSubscription,
12
+ } from '@messenger-box/platform-client';
9
13
  import { userSelector } from '@adminide-stack/user-auth0-client';
10
14
  import { CHANGE_SETTINGS_ACTION } from '@admin-layout/client';
11
15
  import { config } from '../config';
@@ -18,6 +22,11 @@ export interface ThreadsViewProps {
18
22
  refetchChannelDetail: (id: any) => Promise<any>;
19
23
  }
20
24
 
25
+ interface IThreadSubscriptionHandlerProps {
26
+ subscribeToNewMessages: () => any;
27
+ channelId: string | undefined;
28
+ }
29
+
21
30
  const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelDetail }: ThreadsViewProps) => {
22
31
  const { params } = useRoute<any>();
23
32
  const auth = useSelector(userSelector);
@@ -32,11 +41,13 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
32
41
  loading: threadLoading,
33
42
  error,
34
43
  refetch,
44
+ subscribeToMore,
35
45
  } = useThreadMessagesQuery({
36
46
  variables: {
37
47
  channelId: channelId?.toString(),
38
48
  role: role?.toString(),
39
49
  limit: MESSAGES_PER_PAGE,
50
+ repliesLimit2: 5,
40
51
  },
41
52
  fetchPolicy: 'cache-and-network',
42
53
  });
@@ -53,16 +64,17 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
53
64
  channelId: channelId?.toString(),
54
65
  role: role?.toString(),
55
66
  limit: MESSAGES_PER_PAGE,
56
- }).then(({ data }) => {
57
- if (!data?.threadMessages?.data) {
58
- return;
59
- }
60
-
61
- if (data?.threadMessages?.data?.length) {
62
- const { data: newThreads } = data?.threadMessages;
63
- setThreadsData((oldThreads: any) => uniqBy([...oldThreads, ...newThreads], ({ id }) => id));
64
- }
65
67
  });
68
+ // .then(({ data }) => {
69
+ // if (!data?.threadMessages?.data) {
70
+ // return;
71
+ // }
72
+
73
+ // if (data?.threadMessages?.data?.length) {
74
+ // const { data: newThreads } = data?.threadMessages;
75
+ // setThreadsData((oldThreads: any) => uniqBy([...oldThreads, ...newThreads], ({ id }) => id));
76
+ // }
77
+ // });
66
78
 
67
79
  return () => {
68
80
  // Do something when the screen is unfocused
@@ -71,12 +83,12 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
71
83
  }, [channelId]),
72
84
  );
73
85
 
74
- React.useEffect(() => {
75
- if (data?.threadMessages?.data?.length) {
76
- const { data: newThreads } = data?.threadMessages;
77
- setThreadsData((oldThreads: any) => uniqBy([...oldThreads, ...newThreads], ({ id }) => id));
78
- }
79
- }, [data]);
86
+ // React.useEffect(() => {
87
+ // if (data?.threadMessages?.data?.length) {
88
+ // const { data: newThreads } = data?.threadMessages;
89
+ // setThreadsData((oldThreads: any) => uniqBy([...oldThreads, ...newThreads], ({ id }) => id));
90
+ // }
91
+ // }, [data]);
80
92
 
81
93
  // React.useEffect(() => {
82
94
  // if (threadCreatedUpdated?.threadCreatedUpdated?.data) {
@@ -102,11 +114,18 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
102
114
  // setThreadsData((oldThreads: any) => uniqBy([...oldThreads, newThreads], ({ id }) => id));
103
115
  }, []);
104
116
 
117
+ // const threads = React.useMemo(() => {
118
+ // if (!threadData?.length) return null;
119
+ // return orderBy(threadData, ['updatedAt'], ['desc']) || [];
120
+ // //return threadData || [];
121
+ // }, [threadData]);
122
+
105
123
  const threads = React.useMemo(() => {
106
- if (!threadData?.length) return null;
107
- return orderBy(threadData, ['updatedAt'], ['desc']) || [];
108
- //return threadData || [];
109
- }, [threadData]);
124
+ if (!data?.threadMessages?.data?.length) return null;
125
+ const { data: newThreads } = data?.threadMessages;
126
+ const threadsFiltered = newThreads ? uniqBy([...newThreads], ({ id }: any) => id) : [];
127
+ return orderBy(threadsFiltered, ['updatedAt'], ['desc']) || [];
128
+ }, [data]);
110
129
 
111
130
  const threadReplies = React.useMemo(() => {
112
131
  if (!threads?.length) return null;
@@ -114,11 +133,6 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
114
133
  //return threadData || [];
115
134
  }, [threads]);
116
135
 
117
- // const threads = React.useMemo(() => {
118
- // if (!data?.threadMessages?.data?.length) return null;
119
- // return data?.threadMessages?.data || [];
120
- // }, [data]);
121
-
122
136
  const handleSelectThread = useCallback((id: any, title: any, postParentId: any) => {
123
137
  navigation.navigate(config.THREAD_MESSEGE_PATH as any, {
124
138
  channelId: id,
@@ -149,16 +163,17 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
149
163
  channelId: channelId?.toString(),
150
164
  role: role?.toString(),
151
165
  limit: MESSAGES_PER_PAGE,
152
- }).then(({ data }) => {
153
- if (!data?.threadMessages?.data) {
154
- return;
155
- }
156
-
157
- if (data?.threadMessages?.data?.length) {
158
- const { data: newThreads } = data?.threadMessages;
159
- setThreadsData((oldThreads: any) => uniqBy([...oldThreads, ...newThreads], ({ id }) => id));
160
- }
161
166
  });
167
+ // .then(({ data }) => {
168
+ // if (!data?.threadMessages?.data) {
169
+ // return;
170
+ // }
171
+
172
+ // if (data?.threadMessages?.data?.length) {
173
+ // const { data: newThreads } = data?.threadMessages;
174
+ // setThreadsData((oldThreads: any) => uniqBy([...oldThreads, ...newThreads], ({ id }) => id));
175
+ // }
176
+ // });
162
177
  }, []);
163
178
 
164
179
  const fetchMoreThreads = useCallback(() => {
@@ -222,6 +237,48 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
222
237
  </Box>
223
238
  </>
224
239
  )}
240
+ ListFooterComponent={() => (
241
+ <>
242
+ <SubscriptionHandler
243
+ channelId={channelId}
244
+ subscribeToNewMessages={() =>
245
+ subscribeToMore({
246
+ document: THREAD_CHAT_ADDED,
247
+ variables: {
248
+ channelId: channelId?.toString(),
249
+ postParentId: null,
250
+ },
251
+ updateQuery: (prev, { subscriptionData }: any) => {
252
+ if (!subscriptionData.data) return prev;
253
+ const newPostThreadData: any =
254
+ subscriptionData?.data?.threadCreatedUpdated?.data;
255
+ const newMessage: any =
256
+ subscriptionData?.data?.threadCreatedUpdated?.lastMessage;
257
+ const data = prev?.threadMessages?.data?.map((t: any) =>
258
+ t.id === newPostThreadData?.id
259
+ ? {
260
+ ...t,
261
+ replies: [newMessage, ...t?.replies],
262
+ replyCount: newPostThreadData?.replyCount,
263
+ lastReplyAt: newPostThreadData?.lastReplyAt,
264
+ updatedAt: newPostThreadData?.updatedAt,
265
+ }
266
+ : t,
267
+ );
268
+
269
+ return Object.assign({}, prev, {
270
+ threadMessages: {
271
+ ...prev?.threadMessages,
272
+ // totalCount: prev?.threadMessages?.totalCount + 1,
273
+ data: data,
274
+ },
275
+ });
276
+ },
277
+ })
278
+ }
279
+ />
280
+ </>
281
+ )}
225
282
  keyExtractor={(item, index) => 'threads-view-key' + index}
226
283
  onEndReached={fetchMoreThreads}
227
284
  onEndReachedThreshold={0.1}
@@ -230,4 +287,9 @@ const ThreadsViewComponent = ({ channelId, role, channelsDetail, refetchChannelD
230
287
  );
231
288
  };
232
289
 
290
+ const SubscriptionHandler = ({ subscribeToNewMessages, channelId }: IThreadSubscriptionHandlerProps) => {
291
+ useEffect(() => subscribeToNewMessages(), [channelId]);
292
+ return <></>;
293
+ };
294
+
233
295
  export const ThreadsView = React.memo(ThreadsViewComponent);