stream-chat-react-native-core 4.6.1 → 4.7.0-beta.3

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.
Files changed (222) hide show
  1. package/lib/commonjs/components/Attachment/Attachment.js +22 -13
  2. package/lib/commonjs/components/Attachment/Attachment.js.map +1 -1
  3. package/lib/commonjs/components/Attachment/Card.js +23 -14
  4. package/lib/commonjs/components/Attachment/Card.js.map +1 -1
  5. package/lib/commonjs/components/Attachment/Gallery.js +72 -37
  6. package/lib/commonjs/components/Attachment/Gallery.js.map +1 -1
  7. package/lib/commonjs/components/Attachment/VideoThumbnail.js +83 -0
  8. package/lib/commonjs/components/Attachment/VideoThumbnail.js.map +1 -0
  9. package/lib/commonjs/components/Attachment/hooks/useGoToURL.js +1 -1
  10. package/lib/commonjs/components/Attachment/hooks/useGoToURL.js.map +1 -1
  11. package/lib/commonjs/components/Attachment/utils/buildGallery/buildThumbnail.js +1 -0
  12. package/lib/commonjs/components/Attachment/utils/buildGallery/buildThumbnail.js.map +1 -1
  13. package/lib/commonjs/components/Attachment/utils/getAspectRatio.js +5 -5
  14. package/lib/commonjs/components/Attachment/utils/getAspectRatio.js.map +1 -1
  15. package/lib/commonjs/components/Channel/Channel.js +19 -14
  16. package/lib/commonjs/components/Channel/Channel.js.map +1 -1
  17. package/lib/commonjs/components/Channel/hooks/useCreateMessagesContext.js +4 -2
  18. package/lib/commonjs/components/Channel/hooks/useCreateMessagesContext.js.map +1 -1
  19. package/lib/commonjs/components/ChannelPreview/hooks/useChannelPreviewDisplayName.js +43 -28
  20. package/lib/commonjs/components/ChannelPreview/hooks/useChannelPreviewDisplayName.js.map +1 -1
  21. package/lib/commonjs/components/Chat/hooks/useIsOnline.js +6 -6
  22. package/lib/commonjs/components/Chat/hooks/useIsOnline.js.map +1 -1
  23. package/lib/commonjs/components/ImageGallery/ImageGallery.js +167 -345
  24. package/lib/commonjs/components/ImageGallery/ImageGallery.js.map +1 -1
  25. package/lib/commonjs/components/ImageGallery/components/AnimatedGalleryImage.js +4 -4
  26. package/lib/commonjs/components/ImageGallery/components/AnimatedGalleryImage.js.map +1 -1
  27. package/lib/commonjs/components/ImageGallery/components/AnimatedGalleryVideo.js +225 -0
  28. package/lib/commonjs/components/ImageGallery/components/AnimatedGalleryVideo.js.map +1 -0
  29. package/lib/commonjs/components/ImageGallery/components/ImageGalleryFooter.js +102 -40
  30. package/lib/commonjs/components/ImageGallery/components/ImageGalleryFooter.js.map +1 -1
  31. package/lib/commonjs/components/ImageGallery/components/ImageGalleryVideoControl.js +145 -0
  32. package/lib/commonjs/components/ImageGallery/components/ImageGalleryVideoControl.js.map +1 -0
  33. package/lib/commonjs/components/ImageGallery/components/ImageGrid.js +31 -34
  34. package/lib/commonjs/components/ImageGallery/components/ImageGrid.js.map +1 -1
  35. package/lib/commonjs/components/ImageGallery/components/ProgressControl.js +153 -0
  36. package/lib/commonjs/components/ImageGallery/components/ProgressControl.js.map +1 -0
  37. package/lib/commonjs/components/ImageGallery/hooks/useImageGalleryGestures.js +373 -0
  38. package/lib/commonjs/components/ImageGallery/hooks/useImageGalleryGestures.js.map +1 -0
  39. package/lib/commonjs/components/Message/Message.js +23 -13
  40. package/lib/commonjs/components/Message/Message.js.map +1 -1
  41. package/lib/commonjs/components/Message/MessageSimple/utils/renderText.js +22 -15
  42. package/lib/commonjs/components/Message/MessageSimple/utils/renderText.js.map +1 -1
  43. package/lib/commonjs/components/Message/hooks/useCreateMessageContext.js +4 -2
  44. package/lib/commonjs/components/Message/hooks/useCreateMessageContext.js.map +1 -1
  45. package/lib/commonjs/components/MessageOverlay/MessageOverlay.js +25 -23
  46. package/lib/commonjs/components/MessageOverlay/MessageOverlay.js.map +1 -1
  47. package/lib/commonjs/components/Reply/Reply.js +32 -12
  48. package/lib/commonjs/components/Reply/Reply.js.map +1 -1
  49. package/lib/commonjs/components/Spinner/Spinner.js +12 -4
  50. package/lib/commonjs/components/Spinner/Spinner.js.map +1 -1
  51. package/lib/commonjs/components/index.js +26 -0
  52. package/lib/commonjs/components/index.js.map +1 -1
  53. package/lib/commonjs/contexts/messageContext/MessageContext.js +2 -2
  54. package/lib/commonjs/contexts/messageContext/MessageContext.js.map +1 -1
  55. package/lib/commonjs/contexts/messageInputContext/MessageInputContext.js +15 -4
  56. package/lib/commonjs/contexts/messageInputContext/MessageInputContext.js.map +1 -1
  57. package/lib/commonjs/contexts/messageOverlayContext/MessageOverlayContext.js +2 -2
  58. package/lib/commonjs/contexts/messageOverlayContext/MessageOverlayContext.js.map +1 -1
  59. package/lib/commonjs/contexts/messagesContext/MessagesContext.js +2 -2
  60. package/lib/commonjs/contexts/messagesContext/MessagesContext.js.map +1 -1
  61. package/lib/commonjs/contexts/themeContext/utils/theme.js +10 -1
  62. package/lib/commonjs/contexts/themeContext/utils/theme.js.map +1 -1
  63. package/lib/commonjs/icons/Pause.js +61 -0
  64. package/lib/commonjs/icons/Pause.js.map +1 -0
  65. package/lib/commonjs/icons/Play.js +42 -0
  66. package/lib/commonjs/icons/Play.js.map +1 -0
  67. package/lib/commonjs/icons/index.js +26 -0
  68. package/lib/commonjs/icons/index.js.map +1 -1
  69. package/lib/commonjs/mock-builders/api/queryMembers.js +98 -1
  70. package/lib/commonjs/mock-builders/api/queryMembers.js.map +1 -1
  71. package/lib/commonjs/native.js +7 -1
  72. package/lib/commonjs/native.js.map +1 -1
  73. package/lib/commonjs/utils/getUrlOfImageAttachment.js +1 -1
  74. package/lib/commonjs/utils/getUrlOfImageAttachment.js.map +1 -1
  75. package/lib/commonjs/version.json +1 -1
  76. package/lib/module/components/Attachment/Attachment.js +22 -13
  77. package/lib/module/components/Attachment/Attachment.js.map +1 -1
  78. package/lib/module/components/Attachment/Card.js +23 -14
  79. package/lib/module/components/Attachment/Card.js.map +1 -1
  80. package/lib/module/components/Attachment/Gallery.js +72 -37
  81. package/lib/module/components/Attachment/Gallery.js.map +1 -1
  82. package/lib/module/components/Attachment/VideoThumbnail.js +83 -0
  83. package/lib/module/components/Attachment/VideoThumbnail.js.map +1 -0
  84. package/lib/module/components/Attachment/hooks/useGoToURL.js +1 -1
  85. package/lib/module/components/Attachment/hooks/useGoToURL.js.map +1 -1
  86. package/lib/module/components/Attachment/utils/buildGallery/buildThumbnail.js +1 -0
  87. package/lib/module/components/Attachment/utils/buildGallery/buildThumbnail.js.map +1 -1
  88. package/lib/module/components/Attachment/utils/getAspectRatio.js +5 -5
  89. package/lib/module/components/Attachment/utils/getAspectRatio.js.map +1 -1
  90. package/lib/module/components/Channel/Channel.js +19 -14
  91. package/lib/module/components/Channel/Channel.js.map +1 -1
  92. package/lib/module/components/Channel/hooks/useCreateMessagesContext.js +4 -2
  93. package/lib/module/components/Channel/hooks/useCreateMessagesContext.js.map +1 -1
  94. package/lib/module/components/ChannelPreview/hooks/useChannelPreviewDisplayName.js +43 -28
  95. package/lib/module/components/ChannelPreview/hooks/useChannelPreviewDisplayName.js.map +1 -1
  96. package/lib/module/components/Chat/hooks/useIsOnline.js +6 -6
  97. package/lib/module/components/Chat/hooks/useIsOnline.js.map +1 -1
  98. package/lib/module/components/ImageGallery/ImageGallery.js +167 -345
  99. package/lib/module/components/ImageGallery/ImageGallery.js.map +1 -1
  100. package/lib/module/components/ImageGallery/components/AnimatedGalleryImage.js +4 -4
  101. package/lib/module/components/ImageGallery/components/AnimatedGalleryImage.js.map +1 -1
  102. package/lib/module/components/ImageGallery/components/AnimatedGalleryVideo.js +225 -0
  103. package/lib/module/components/ImageGallery/components/AnimatedGalleryVideo.js.map +1 -0
  104. package/lib/module/components/ImageGallery/components/ImageGalleryFooter.js +102 -40
  105. package/lib/module/components/ImageGallery/components/ImageGalleryFooter.js.map +1 -1
  106. package/lib/module/components/ImageGallery/components/ImageGalleryVideoControl.js +145 -0
  107. package/lib/module/components/ImageGallery/components/ImageGalleryVideoControl.js.map +1 -0
  108. package/lib/module/components/ImageGallery/components/ImageGrid.js +31 -34
  109. package/lib/module/components/ImageGallery/components/ImageGrid.js.map +1 -1
  110. package/lib/module/components/ImageGallery/components/ProgressControl.js +153 -0
  111. package/lib/module/components/ImageGallery/components/ProgressControl.js.map +1 -0
  112. package/lib/module/components/ImageGallery/hooks/useImageGalleryGestures.js +373 -0
  113. package/lib/module/components/ImageGallery/hooks/useImageGalleryGestures.js.map +1 -0
  114. package/lib/module/components/Message/Message.js +23 -13
  115. package/lib/module/components/Message/Message.js.map +1 -1
  116. package/lib/module/components/Message/MessageSimple/utils/renderText.js +22 -15
  117. package/lib/module/components/Message/MessageSimple/utils/renderText.js.map +1 -1
  118. package/lib/module/components/Message/hooks/useCreateMessageContext.js +4 -2
  119. package/lib/module/components/Message/hooks/useCreateMessageContext.js.map +1 -1
  120. package/lib/module/components/MessageOverlay/MessageOverlay.js +25 -23
  121. package/lib/module/components/MessageOverlay/MessageOverlay.js.map +1 -1
  122. package/lib/module/components/Reply/Reply.js +32 -12
  123. package/lib/module/components/Reply/Reply.js.map +1 -1
  124. package/lib/module/components/Spinner/Spinner.js +12 -4
  125. package/lib/module/components/Spinner/Spinner.js.map +1 -1
  126. package/lib/module/components/index.js +26 -0
  127. package/lib/module/components/index.js.map +1 -1
  128. package/lib/module/contexts/messageContext/MessageContext.js +2 -2
  129. package/lib/module/contexts/messageContext/MessageContext.js.map +1 -1
  130. package/lib/module/contexts/messageInputContext/MessageInputContext.js +15 -4
  131. package/lib/module/contexts/messageInputContext/MessageInputContext.js.map +1 -1
  132. package/lib/module/contexts/messageOverlayContext/MessageOverlayContext.js +2 -2
  133. package/lib/module/contexts/messageOverlayContext/MessageOverlayContext.js.map +1 -1
  134. package/lib/module/contexts/messagesContext/MessagesContext.js +2 -2
  135. package/lib/module/contexts/messagesContext/MessagesContext.js.map +1 -1
  136. package/lib/module/contexts/themeContext/utils/theme.js +10 -1
  137. package/lib/module/contexts/themeContext/utils/theme.js.map +1 -1
  138. package/lib/module/icons/Pause.js +61 -0
  139. package/lib/module/icons/Pause.js.map +1 -0
  140. package/lib/module/icons/Play.js +42 -0
  141. package/lib/module/icons/Play.js.map +1 -0
  142. package/lib/module/icons/index.js +26 -0
  143. package/lib/module/icons/index.js.map +1 -1
  144. package/lib/module/mock-builders/api/queryMembers.js +98 -1
  145. package/lib/module/mock-builders/api/queryMembers.js.map +1 -1
  146. package/lib/module/native.js +7 -1
  147. package/lib/module/native.js.map +1 -1
  148. package/lib/module/utils/getUrlOfImageAttachment.js +1 -1
  149. package/lib/module/utils/getUrlOfImageAttachment.js.map +1 -1
  150. package/lib/module/version.json +1 -1
  151. package/lib/typescript/components/Attachment/Gallery.d.ts +1 -1
  152. package/lib/typescript/components/Attachment/VideoThumbnail.d.ts +4 -0
  153. package/lib/typescript/components/Attachment/utils/buildGallery/types.d.ts +1 -0
  154. package/lib/typescript/components/Attachment/utils/getAspectRatio.d.ts +1 -1
  155. package/lib/typescript/components/Channel/Channel.d.ts +1 -1
  156. package/lib/typescript/components/Channel/hooks/useCreateMessagesContext.d.ts +1 -1
  157. package/lib/typescript/components/ChannelPreview/hooks/__tests__/useChannelPreviewDisplayName.test.d.ts +1 -0
  158. package/lib/typescript/components/ChannelPreview/hooks/useChannelPreviewDisplayName.d.ts +4 -4
  159. package/lib/typescript/components/Chat/hooks/useIsOnline.d.ts +1 -1
  160. package/lib/typescript/components/ImageGallery/ImageGallery.d.ts +2 -0
  161. package/lib/typescript/components/ImageGallery/components/AnimatedGalleryVideo.d.ts +27 -0
  162. package/lib/typescript/components/ImageGallery/components/ImageGalleryFooter.d.ts +18 -2
  163. package/lib/typescript/components/ImageGallery/components/ImageGalleryVideoControl.d.ts +3 -0
  164. package/lib/typescript/components/ImageGallery/components/ProgressControl.d.ts +5 -0
  165. package/lib/typescript/components/ImageGallery/hooks/useImageGalleryGestures.d.ts +36 -0
  166. package/lib/typescript/components/Message/Message.d.ts +9 -3
  167. package/lib/typescript/components/Message/hooks/useCreateMessageContext.d.ts +1 -1
  168. package/lib/typescript/components/Spinner/Spinner.d.ts +7 -1
  169. package/lib/typescript/components/index.d.ts +2 -0
  170. package/lib/typescript/contexts/messageContext/MessageContext.d.ts +3 -1
  171. package/lib/typescript/contexts/messageOverlayContext/MessageOverlayContext.d.ts +1 -0
  172. package/lib/typescript/contexts/messagesContext/MessagesContext.d.ts +2 -0
  173. package/lib/typescript/contexts/themeContext/utils/theme.d.ts +9 -0
  174. package/lib/typescript/icons/Pause.d.ts +3 -0
  175. package/lib/typescript/icons/Play.d.ts +3 -0
  176. package/lib/typescript/icons/index.d.ts +2 -0
  177. package/lib/typescript/mock-builders/api/queryMembers.d.ts +161 -0
  178. package/lib/typescript/mock-builders/api/utils.d.ts +7 -0
  179. package/lib/typescript/mock-builders/generator/user.d.ts +30 -0
  180. package/lib/typescript/mock-builders/mock.d.ts +3 -0
  181. package/lib/typescript/native.d.ts +66 -1
  182. package/package.json +1 -1
  183. package/src/components/Attachment/Attachment.tsx +11 -7
  184. package/src/components/Attachment/Card.tsx +3 -0
  185. package/src/components/Attachment/Gallery.tsx +75 -36
  186. package/src/components/Attachment/VideoThumbnail.tsx +45 -0
  187. package/src/components/Attachment/hooks/useGoToURL.ts +1 -1
  188. package/src/components/Attachment/utils/buildGallery/buildThumbnail.ts +1 -0
  189. package/src/components/Attachment/utils/buildGallery/types.ts +1 -0
  190. package/src/components/Attachment/utils/getAspectRatio.ts +7 -5
  191. package/src/components/Channel/Channel.tsx +4 -0
  192. package/src/components/Channel/hooks/useCreateMessagesContext.ts +2 -0
  193. package/src/components/ChannelPreview/hooks/__tests__/useChannelPreviewDisplayName.test.tsx +83 -0
  194. package/src/components/ChannelPreview/hooks/useChannelPreviewDisplayName.ts +43 -26
  195. package/src/components/Chat/hooks/useIsOnline.ts +7 -7
  196. package/src/components/ImageGallery/ImageGallery.tsx +145 -667
  197. package/src/components/ImageGallery/components/AnimatedGalleryImage.tsx +4 -4
  198. package/src/components/ImageGallery/components/AnimatedGalleryVideo.tsx +224 -0
  199. package/src/components/ImageGallery/components/ImageGalleryFooter.tsx +114 -19
  200. package/src/components/ImageGallery/components/ImageGalleryVideoControl.tsx +103 -0
  201. package/src/components/ImageGallery/components/ImageGrid.tsx +12 -25
  202. package/src/components/ImageGallery/components/ProgressControl.tsx +107 -0
  203. package/src/components/ImageGallery/hooks/useImageGalleryGestures.tsx +722 -0
  204. package/src/components/Message/Message.tsx +18 -7
  205. package/src/components/Message/MessageSimple/utils/renderText.tsx +6 -1
  206. package/src/components/Message/hooks/useCreateMessageContext.ts +2 -0
  207. package/src/components/MessageOverlay/MessageOverlay.tsx +2 -0
  208. package/src/components/Reply/Reply.tsx +36 -23
  209. package/src/components/Spinner/Spinner.tsx +11 -4
  210. package/src/components/index.ts +2 -0
  211. package/src/contexts/messageContext/MessageContext.tsx +2 -0
  212. package/src/contexts/messageInputContext/MessageInputContext.tsx +11 -0
  213. package/src/contexts/messageOverlayContext/MessageOverlayContext.tsx +1 -0
  214. package/src/contexts/messagesContext/MessagesContext.tsx +2 -0
  215. package/src/contexts/themeContext/utils/theme.ts +18 -0
  216. package/src/icons/Pause.tsx +14 -0
  217. package/src/icons/Play.tsx +17 -0
  218. package/src/icons/index.ts +2 -0
  219. package/src/mock-builders/api/queryMembers.js +98 -0
  220. package/src/native.ts +59 -1
  221. package/src/utils/getUrlOfImageAttachment.ts +1 -1
  222. package/src/version.json +1 -1
@@ -82,6 +82,7 @@ import { FileAttachmentGroup as FileAttachmentGroupDefault } from '../Attachment
82
82
  import { FileIcon as FileIconDefault } from '../Attachment/FileIcon';
83
83
  import { Gallery as GalleryDefault } from '../Attachment/Gallery';
84
84
  import { Giphy as GiphyDefault } from '../Attachment/Giphy';
85
+ import { VideoThumbnail as VideoThumbnailDefault } from '../Attachment/VideoThumbnail';
85
86
  import { AutoCompleteSuggestionHeader as AutoCompleteSuggestionHeaderDefault } from '../AutoCompleteInput/AutoCompleteSuggestionHeader';
86
87
  import { AutoCompleteSuggestionItem as AutoCompleteSuggestionItemDefault } from '../AutoCompleteInput/AutoCompleteSuggestionItem';
87
88
  import { AutoCompleteSuggestionList as AutoCompleteSuggestionListDefault } from '../AutoCompleteInput/AutoCompleteSuggestionList';
@@ -287,6 +288,7 @@ export type ChannelPropsWithContext<
287
288
  | 'TypingIndicator'
288
289
  | 'TypingIndicatorContainer'
289
290
  | 'UrlPreview'
291
+ | 'VideoThumbnail'
290
292
  >
291
293
  > &
292
294
  Partial<
@@ -534,6 +536,7 @@ const ChannelWithContext = <
534
536
  TypingIndicatorContainer = TypingIndicatorContainerDefault,
535
537
  UploadProgressIndicator = UploadProgressIndicatorDefault,
536
538
  UrlPreview = CardDefault,
539
+ VideoThumbnail = VideoThumbnailDefault,
537
540
  watcherCount,
538
541
  watchers,
539
542
  } = props;
@@ -1738,6 +1741,7 @@ const ChannelWithContext = <
1738
1741
  TypingIndicatorContainer,
1739
1742
  updateMessage,
1740
1743
  UrlPreview,
1744
+ VideoThumbnail,
1741
1745
  });
1742
1746
 
1743
1747
  const suggestionsContext = {
@@ -80,6 +80,7 @@ export const useCreateMessagesContext = <
80
80
  TypingIndicatorContainer,
81
81
  updateMessage,
82
82
  UrlPreview,
83
+ VideoThumbnail,
83
84
  }: MessagesContextValue<StreamChatGenerics> & {
84
85
  /**
85
86
  * To ensure we allow re-render, when channel is changed
@@ -166,6 +167,7 @@ export const useCreateMessagesContext = <
166
167
  TypingIndicatorContainer,
167
168
  updateMessage,
168
169
  UrlPreview,
170
+ VideoThumbnail,
169
171
  }),
170
172
  [
171
173
  additionalTouchablePropsLength,
@@ -0,0 +1,83 @@
1
+ import React from 'react';
2
+ import { Text } from 'react-native';
3
+
4
+ import { render, waitFor } from '@testing-library/react-native';
5
+
6
+ import type { DefaultStreamChatGenerics } from 'src/types/types';
7
+ import type { Channel, ChannelMemberResponse, DefaultGenerics, StreamChat } from 'stream-chat';
8
+
9
+ import {
10
+ GROUP_CHANNEL_MEMBERS_MOCK,
11
+ ONE_CHANNEL_MEMBER_MOCK,
12
+ ONE_MEMBER_WITH_EMPTY_USER_MOCK,
13
+ } from '../../../../mock-builders/api/queryMembers';
14
+ import { generateUser } from '../../../../mock-builders/generator/user';
15
+ import { getTestClientWithUser } from '../../../../mock-builders/mock';
16
+ import {
17
+ getChannelPreviewDisplayName,
18
+ useChannelPreviewDisplayName,
19
+ } from '../useChannelPreviewDisplayName';
20
+
21
+ describe('useChannelPreviewDisplayName', () => {
22
+ const clientUser = generateUser();
23
+ let chatClient: StreamChat<DefaultGenerics> | StreamChat<DefaultStreamChatGenerics>;
24
+ const CHARACTER_LENGTH = 15;
25
+
26
+ beforeEach(async () => {
27
+ chatClient = await getTestClientWithUser(clientUser);
28
+ });
29
+
30
+ it('should return a channel display name', async () => {
31
+ const channelName = 'okechukwu';
32
+
33
+ const TestComponent = () => {
34
+ const channelDisplayName = useChannelPreviewDisplayName(
35
+ {
36
+ data: { name: channelName },
37
+ } as unknown as Channel<DefaultGenerics>,
38
+ CHARACTER_LENGTH,
39
+ );
40
+ return <Text>{channelDisplayName}</Text>;
41
+ };
42
+
43
+ const { getByText } = render(<TestComponent />);
44
+
45
+ await waitFor(() => {
46
+ expect(getByText(channelName)).toBeTruthy();
47
+ });
48
+ });
49
+
50
+ it('should return the full channelName when channelName length is less than characterLength', () => {
51
+ const channelName = 'okechukwu';
52
+ const currentUserId = chatClient.userID;
53
+
54
+ const displayName = getChannelPreviewDisplayName({
55
+ channelName,
56
+ characterLimit: CHARACTER_LENGTH,
57
+ currentUserId,
58
+ members: ONE_CHANNEL_MEMBER_MOCK,
59
+ });
60
+
61
+ expect(displayName).toEqual(channelName);
62
+ });
63
+
64
+ it.each([
65
+ [CHARACTER_LENGTH, GROUP_CHANNEL_MEMBERS_MOCK, 'okechukwu nwagba', 'ben, nick, q,...+2'],
66
+ [CHARACTER_LENGTH, ONE_CHANNEL_MEMBER_MOCK, 'okechukwu nwagba', 'okechukwu nw...'],
67
+ [CHARACTER_LENGTH, ONE_MEMBER_WITH_EMPTY_USER_MOCK, 'okechukwu nwagba', 'Unknown User...'],
68
+ ])(
69
+ 'getChannelPreviewDisplayName(%i, %p, %s) result in %s',
70
+ (characterLength, members, currentUserId, expected) => {
71
+ const displayName = getChannelPreviewDisplayName({
72
+ characterLimit: characterLength,
73
+ currentUserId,
74
+ members: members as unknown as Record<
75
+ string,
76
+ ChannelMemberResponse<DefaultStreamChatGenerics>
77
+ >,
78
+ });
79
+
80
+ expect(displayName).toEqual(expected);
81
+ },
82
+ );
83
+ });
@@ -1,48 +1,66 @@
1
1
  import { useEffect, useState } from 'react';
2
2
 
3
- import type { Channel } from 'stream-chat';
3
+ import type { Channel, ChannelMemberResponse } from 'stream-chat';
4
4
 
5
5
  import { useChatContext } from '../../../contexts/chatContext/ChatContext';
6
6
 
7
7
  import type { DefaultStreamChatGenerics } from '../../../types/types';
8
8
  import { vw } from '../../../utils/utils';
9
9
 
10
- const maxCharacterLengthDefault = (vw(100) - 16) / 6;
10
+ const DEFAULT_MAX_CHARACTER_LENGTH = (vw(100) - 16) / 6;
11
+
12
+ const ELLIPSIS = `...`;
13
+
14
+ const getMemberName = (member: ChannelMemberResponse) =>
15
+ member.user?.name || member.user?.id || 'Unknown User';
11
16
 
12
17
  export const getChannelPreviewDisplayName = <
13
18
  StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
14
19
  >({
15
20
  channelName,
21
+ characterLimit,
16
22
  currentUserId,
17
- maxCharacterLength,
18
23
  members,
19
24
  }: {
20
- maxCharacterLength: number;
25
+ characterLimit: number;
21
26
  channelName?: string;
22
27
  currentUserId?: string;
23
28
  members?: Channel<StreamChatGenerics>['state']['members'];
24
- }) => {
29
+ }): string => {
25
30
  if (channelName) return channelName;
26
31
 
27
32
  const channelMembers = Object.values(members || {});
33
+
28
34
  const otherMembers = channelMembers.filter((member) => member.user?.id !== currentUserId);
35
+ otherMembers.sort((prevUser, nextUser) =>
36
+ (prevUser?.user?.name ?? '')
37
+ .toLowerCase()
38
+ .localeCompare((nextUser?.user?.name ?? '').toLocaleUpperCase()),
39
+ );
40
+
41
+ const createChannelNameSuffix = (remainingNumberOfMembers: number) =>
42
+ remainingNumberOfMembers <= 1 ? `${ELLIPSIS}` : `,${ELLIPSIS}+${remainingNumberOfMembers}`;
29
43
 
30
- const name = otherMembers.slice(0).reduce((returnString, currentMember, index, originalArray) => {
31
- const returnStringLength = returnString.length;
32
- const currentMemberName = currentMember.user?.name || currentMember.user?.id || 'Unknown User';
33
- // a rough approximation of when the +Number shows up
34
- if (returnStringLength + (currentMemberName.length + 2) < maxCharacterLength) {
35
- if (returnStringLength) {
36
- returnString += `, ${currentMemberName}`;
37
- } else {
38
- returnString = currentMemberName;
39
- }
44
+ const name = otherMembers.reduce((result, currentMember, index, originalArray) => {
45
+ if (result.length >= characterLimit) {
46
+ return result;
47
+ }
48
+
49
+ const currentMemberName = getMemberName(currentMember);
50
+
51
+ const resultHasSpaceForCurrentMemberName =
52
+ result.length + (currentMemberName.length + ELLIPSIS.length) < characterLimit;
53
+ if (resultHasSpaceForCurrentMemberName) {
54
+ return result.length > 0 ? `${result}, ${currentMemberName}, ` : currentMemberName;
40
55
  } else {
41
- const remainingMembers = originalArray.length - index;
42
- returnString += `, +${remainingMembers}`;
43
- originalArray.splice(1); // exit early
56
+ const remainingNumberOfMembers = originalArray.length - index;
57
+ const truncateLimit = characterLimit - (ELLIPSIS.length + result.length);
58
+ const tuncatedCurrentMemberName = `${currentMemberName.slice(0, truncateLimit)}`;
59
+
60
+ const channelNameSuffix = createChannelNameSuffix(remainingNumberOfMembers);
61
+
62
+ return `${result}${tuncatedCurrentMemberName}${channelNameSuffix}`;
44
63
  }
45
- return returnString;
46
64
  }, '');
47
65
 
48
66
  return name;
@@ -56,17 +74,16 @@ export const useChannelPreviewDisplayName = <
56
74
  ) => {
57
75
  const { client } = useChatContext<StreamChatGenerics>();
58
76
 
59
- const currentUserId = client.userID;
60
- const members = channel?.state.members;
77
+ const currentUserId = client?.userID;
78
+ const members = channel?.state?.members;
61
79
  const numOfMembers = Object.keys(members || {}).length;
62
80
  const channelName = channel?.data?.name;
63
- const maxCharacterLength = characterLength || maxCharacterLengthDefault;
64
-
81
+ const characterLimit = characterLength || DEFAULT_MAX_CHARACTER_LENGTH;
65
82
  const [displayName, setDisplayName] = useState(
66
83
  getChannelPreviewDisplayName({
67
84
  channelName,
85
+ characterLimit,
68
86
  currentUserId,
69
- maxCharacterLength,
70
87
  members,
71
88
  }),
72
89
  );
@@ -75,12 +92,12 @@ export const useChannelPreviewDisplayName = <
75
92
  setDisplayName(
76
93
  getChannelPreviewDisplayName({
77
94
  channelName,
95
+ characterLimit,
78
96
  currentUserId,
79
- maxCharacterLength,
80
97
  members,
81
98
  }),
82
99
  );
83
- }, [channelName, currentUserId, maxCharacterLength, numOfMembers]);
100
+ }, [channelName, currentUserId, characterLimit, numOfMembers]);
84
101
 
85
102
  return displayName;
86
103
  };
@@ -13,7 +13,7 @@ import type { DefaultStreamChatGenerics } from '../../../types/types';
13
13
  /**
14
14
  * Disconnect the websocket connection when app goes to background,
15
15
  * and reconnect when app comes to foreground.
16
- * We do this to make sure, user receives push notifications when app is in the background.
16
+ * We do this to make sure the user receives push notifications when app is in the background.
17
17
  * You can't receive push notification until you have active websocket connection.
18
18
  */
19
19
  export const useIsOnline = <
@@ -25,10 +25,10 @@ export const useIsOnline = <
25
25
  const [isOnline, setIsOnline] = useState(true);
26
26
  const [connectionRecovering, setConnectionRecovering] = useState(false);
27
27
  const isMounted = useIsMountedRef();
28
- const clientExits = !!client;
28
+ const clientExists = !!client;
29
29
 
30
30
  const onBackground = useCallback(() => {
31
- if (!closeConnectionOnBackground) return;
31
+ if (!closeConnectionOnBackground || !clientExists) return;
32
32
 
33
33
  for (const cid in client.activeChannels) {
34
34
  const channel = client.activeChannels[cid];
@@ -37,13 +37,13 @@ export const useIsOnline = <
37
37
 
38
38
  client.closeConnection();
39
39
  setIsOnline(false);
40
- }, [closeConnectionOnBackground, client]);
40
+ }, [closeConnectionOnBackground, client, clientExists]);
41
41
 
42
42
  const onForeground = useCallback(() => {
43
- if (!closeConnectionOnBackground) return;
43
+ if (!closeConnectionOnBackground || !clientExists) return;
44
44
 
45
45
  client.openConnection();
46
- }, [closeConnectionOnBackground, client]);
46
+ }, [closeConnectionOnBackground, client, clientExists]);
47
47
 
48
48
  useAppStateListener(onForeground, onBackground);
49
49
 
@@ -101,7 +101,7 @@ export const useIsOnline = <
101
101
  chatListeners.forEach((listener) => listener.unsubscribe?.());
102
102
  unsubscribeNetInfo?.();
103
103
  };
104
- }, [clientExits]);
104
+ }, [clientExists]);
105
105
 
106
106
  return { connectionRecovering, isOnline };
107
107
  };