stream-chat-react-native-core 4.9.0-beta.4 → 4.10.0

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 (98) hide show
  1. package/lib/commonjs/components/Attachment/Gallery.js +246 -192
  2. package/lib/commonjs/components/Attachment/Gallery.js.map +1 -1
  3. package/lib/commonjs/components/ChannelPreview/ChannelPreview.js +7 -3
  4. package/lib/commonjs/components/ChannelPreview/ChannelPreview.js.map +1 -1
  5. package/lib/commonjs/components/ChannelPreview/hooks/useLatestMessagePreview.js +2 -1
  6. package/lib/commonjs/components/ChannelPreview/hooks/useLatestMessagePreview.js.map +1 -1
  7. package/lib/commonjs/components/Chat/Chat.js +9 -5
  8. package/lib/commonjs/components/Chat/Chat.js.map +1 -1
  9. package/lib/commonjs/components/ImageGallery/ImageGallery.js +25 -22
  10. package/lib/commonjs/components/ImageGallery/ImageGallery.js.map +1 -1
  11. package/lib/commonjs/components/ImageGallery/components/ImageGalleryFooter.js +15 -15
  12. package/lib/commonjs/components/ImageGallery/components/ImageGalleryFooter.js.map +1 -1
  13. package/lib/commonjs/components/Message/Message.js +12 -8
  14. package/lib/commonjs/components/Message/Message.js.map +1 -1
  15. package/lib/commonjs/components/Message/MessageSimple/MessageTextContainer.js +7 -4
  16. package/lib/commonjs/components/Message/MessageSimple/MessageTextContainer.js.map +1 -1
  17. package/lib/commonjs/contexts/messageInputContext/MessageInputContext.js +16 -10
  18. package/lib/commonjs/contexts/messageInputContext/MessageInputContext.js.map +1 -1
  19. package/lib/commonjs/contexts/messageOverlayContext/MessageOverlayContext.js +2 -2
  20. package/lib/commonjs/contexts/messageOverlayContext/MessageOverlayContext.js.map +1 -1
  21. package/lib/commonjs/contexts/overlayContext/OverlayProvider.js +15 -13
  22. package/lib/commonjs/contexts/overlayContext/OverlayProvider.js.map +1 -1
  23. package/lib/commonjs/contexts/translationContext/TranslationContext.js +8 -4
  24. package/lib/commonjs/contexts/translationContext/TranslationContext.js.map +1 -1
  25. package/lib/commonjs/hooks/useStreami18n.js.map +1 -1
  26. package/lib/commonjs/hooks/useTranslatedMessage.js +34 -0
  27. package/lib/commonjs/hooks/useTranslatedMessage.js.map +1 -0
  28. package/lib/commonjs/mock-builders/api/getOrCreateChannel.js.map +1 -1
  29. package/lib/commonjs/mock-builders/generator/channel.js +8 -4
  30. package/lib/commonjs/mock-builders/generator/channel.js.map +1 -1
  31. package/lib/commonjs/version.json +1 -1
  32. package/lib/module/components/Attachment/Gallery.js +246 -192
  33. package/lib/module/components/Attachment/Gallery.js.map +1 -1
  34. package/lib/module/components/ChannelPreview/ChannelPreview.js +7 -3
  35. package/lib/module/components/ChannelPreview/ChannelPreview.js.map +1 -1
  36. package/lib/module/components/ChannelPreview/hooks/useLatestMessagePreview.js +2 -1
  37. package/lib/module/components/ChannelPreview/hooks/useLatestMessagePreview.js.map +1 -1
  38. package/lib/module/components/Chat/Chat.js +9 -5
  39. package/lib/module/components/Chat/Chat.js.map +1 -1
  40. package/lib/module/components/ImageGallery/ImageGallery.js +25 -22
  41. package/lib/module/components/ImageGallery/ImageGallery.js.map +1 -1
  42. package/lib/module/components/ImageGallery/components/ImageGalleryFooter.js +15 -15
  43. package/lib/module/components/ImageGallery/components/ImageGalleryFooter.js.map +1 -1
  44. package/lib/module/components/Message/Message.js +12 -8
  45. package/lib/module/components/Message/Message.js.map +1 -1
  46. package/lib/module/components/Message/MessageSimple/MessageTextContainer.js +7 -4
  47. package/lib/module/components/Message/MessageSimple/MessageTextContainer.js.map +1 -1
  48. package/lib/module/contexts/messageInputContext/MessageInputContext.js +16 -10
  49. package/lib/module/contexts/messageInputContext/MessageInputContext.js.map +1 -1
  50. package/lib/module/contexts/messageOverlayContext/MessageOverlayContext.js +2 -2
  51. package/lib/module/contexts/messageOverlayContext/MessageOverlayContext.js.map +1 -1
  52. package/lib/module/contexts/overlayContext/OverlayProvider.js +15 -13
  53. package/lib/module/contexts/overlayContext/OverlayProvider.js.map +1 -1
  54. package/lib/module/contexts/translationContext/TranslationContext.js +8 -4
  55. package/lib/module/contexts/translationContext/TranslationContext.js.map +1 -1
  56. package/lib/module/hooks/useStreami18n.js.map +1 -1
  57. package/lib/module/hooks/useTranslatedMessage.js +34 -0
  58. package/lib/module/hooks/useTranslatedMessage.js.map +1 -0
  59. package/lib/module/mock-builders/api/getOrCreateChannel.js.map +1 -1
  60. package/lib/module/mock-builders/generator/channel.js +8 -4
  61. package/lib/module/mock-builders/generator/channel.js.map +1 -1
  62. package/lib/module/version.json +1 -1
  63. package/lib/typescript/components/ChannelPreview/__tests__/ChannelPreview.test.d.ts +1 -0
  64. package/lib/typescript/components/Message/MessageSimple/__tests__/MessageTextContainer.test.d.ts +1 -0
  65. package/lib/typescript/contexts/messageOverlayContext/MessageOverlayContext.d.ts +2 -1
  66. package/lib/typescript/contexts/translationContext/TranslationContext.d.ts +7 -2
  67. package/lib/typescript/hooks/__tests__/useTranslatedMessage.test.d.ts +1 -0
  68. package/lib/typescript/hooks/useStreami18n.d.ts +2 -2
  69. package/lib/typescript/hooks/useTranslatedMessage.d.ts +3 -0
  70. package/lib/typescript/mock-builders/api/getOrCreateChannel.d.ts +13 -1
  71. package/lib/typescript/mock-builders/event/messageNew.d.ts +2 -0
  72. package/lib/typescript/mock-builders/event/messageRead.d.ts +8 -0
  73. package/lib/typescript/mock-builders/generator/channel.d.ts +19 -15
  74. package/package.json +3 -3
  75. package/src/components/Attachment/Gallery.tsx +231 -141
  76. package/src/components/ChannelPreview/ChannelPreview.tsx +7 -1
  77. package/src/components/ChannelPreview/__tests__/{ChannelPreview.test.js → ChannelPreview.test.tsx} +79 -41
  78. package/src/components/ChannelPreview/hooks/__tests__/useLatestMessagePreview.test.tsx +56 -21
  79. package/src/components/ChannelPreview/hooks/useLatestMessagePreview.ts +4 -1
  80. package/src/components/Chat/Chat.tsx +6 -3
  81. package/src/components/ImageGallery/ImageGallery.tsx +10 -6
  82. package/src/components/ImageGallery/components/ImageGalleryFooter.tsx +2 -1
  83. package/src/components/Message/Message.tsx +3 -0
  84. package/src/components/Message/MessageSimple/MessageTextContainer.tsx +7 -1
  85. package/src/components/Message/MessageSimple/__tests__/MessageTextContainer.test.tsx +108 -0
  86. package/src/components/Message/MessageSimple/__tests__/__snapshots__/{MessageTextContainer.test.js.snap → MessageTextContainer.test.tsx.snap} +0 -0
  87. package/src/contexts/messageInputContext/MessageInputContext.tsx +7 -0
  88. package/src/contexts/messageInputContext/__tests__/__snapshots__/sendMessage.test.tsx.snap +4 -0
  89. package/src/contexts/messageOverlayContext/MessageOverlayContext.tsx +2 -1
  90. package/src/contexts/overlayContext/OverlayProvider.tsx +4 -3
  91. package/src/contexts/translationContext/TranslationContext.tsx +10 -1
  92. package/src/hooks/__tests__/useTranslatedMessage.test.tsx +106 -0
  93. package/src/hooks/useStreami18n.ts +2 -2
  94. package/src/hooks/useTranslatedMessage.ts +30 -0
  95. package/src/mock-builders/api/{getOrCreateChannel.js → getOrCreateChannel.ts} +8 -1
  96. package/src/mock-builders/generator/{channel.js → channel.ts} +19 -10
  97. package/src/version.json +1 -1
  98. package/src/components/Message/MessageSimple/__tests__/MessageTextContainer.test.js +0 -65
@@ -5,11 +5,10 @@ import { waitFor } from '@testing-library/react-native';
5
5
 
6
6
  import type { DefaultStreamChatGenerics } from 'src/types/types';
7
7
 
8
- import type { DefaultGenerics, StreamChat } from 'stream-chat';
8
+ import type { DefaultGenerics, MessageResponse, StreamChat } from 'stream-chat';
9
9
 
10
10
  import { ChatContext, ChatContextValue } from '../../../../contexts/chatContext/ChatContext';
11
11
  import {
12
- CHANNEL,
13
12
  CHANNEL_WITH_DELETED_MESSAGES,
14
13
  CHANNEL_WITH_EMPTY_MESSAGE,
15
14
  CHANNEL_WITH_MENTIONED_USERS,
@@ -47,19 +46,11 @@ describe('useLatestMessagePreview', () => {
47
46
  </ChatContext.Provider>
48
47
  );
49
48
 
50
- it('should return a channel latest message', async () => {
51
- const { result } = renderHook(
52
- () => useLatestMessagePreview(CHANNEL, FORCE_UPDATE, LATEST_MESSAGE),
53
- { wrapper: ChatProvider },
54
- );
55
- await waitFor(() => {
56
- expect(result.current.previews).toBeTruthy();
57
- });
58
- });
49
+ it('should return a deleted message preview if the latest message is deleted', async () => {
50
+ const latestMessage = { type: 'deleted' } as unknown as MessageResponse;
59
51
 
60
- it('should return a deleted message preview', async () => {
61
52
  const { result } = renderHook(
62
- () => useLatestMessagePreview(CHANNEL_WITH_DELETED_MESSAGES, FORCE_UPDATE, LATEST_MESSAGE),
53
+ () => useLatestMessagePreview(CHANNEL_WITH_DELETED_MESSAGES, FORCE_UPDATE, latestMessage),
63
54
  { wrapper: ChatProvider },
64
55
  );
65
56
  await waitFor(() => {
@@ -67,9 +58,11 @@ describe('useLatestMessagePreview', () => {
67
58
  });
68
59
  });
69
60
 
70
- it('should return an "Nothing yet..." message preview', async () => {
61
+ it('should return an "Nothing yet..." message preview if channel has no messages', async () => {
62
+ const latestMessage = undefined;
63
+
71
64
  const { result } = renderHook(
72
- () => useLatestMessagePreview(CHANNEL_WITH_NO_MESSAGES, FORCE_UPDATE, LATEST_MESSAGE),
65
+ () => useLatestMessagePreview(CHANNEL_WITH_NO_MESSAGES, FORCE_UPDATE, latestMessage),
73
66
  { wrapper: ChatProvider },
74
67
  );
75
68
  await waitFor(() => {
@@ -77,11 +70,12 @@ describe('useLatestMessagePreview', () => {
77
70
  });
78
71
  });
79
72
 
80
- it('should return latest message preview', async () => {
73
+ it('should use latestMessage if provided', async () => {
81
74
  const { result } = renderHook(
82
75
  () => useLatestMessagePreview(CHANNEL_WITH_MESSAGES_TEXT, FORCE_UPDATE, LATEST_MESSAGE),
83
76
  { wrapper: ChatProvider },
84
77
  );
78
+
85
79
  await waitFor(() => {
86
80
  expect(result.current.previews).toEqual([
87
81
  { bold: true, text: '@okechukwu: ' },
@@ -91,10 +85,13 @@ describe('useLatestMessagePreview', () => {
91
85
  });
92
86
 
93
87
  it('should return a channel with an empty message preview', async () => {
88
+ const latestMessage = {} as unknown as MessageResponse;
89
+
94
90
  const { result } = renderHook(
95
- () => useLatestMessagePreview(CHANNEL_WITH_EMPTY_MESSAGE, FORCE_UPDATE, LATEST_MESSAGE),
91
+ () => useLatestMessagePreview(CHANNEL_WITH_EMPTY_MESSAGE, FORCE_UPDATE, latestMessage),
96
92
  { wrapper: ChatProvider },
97
93
  );
94
+
98
95
  await waitFor(() => {
99
96
  expect(result.current.previews).toEqual([
100
97
  { bold: false, text: '' },
@@ -104,8 +101,16 @@ describe('useLatestMessagePreview', () => {
104
101
  });
105
102
 
106
103
  it('should return a mentioned user (@Max) message preview', async () => {
104
+ const latestMessage = {
105
+ mentioned_users: [{ id: 'Max', name: 'Max' }],
106
+ text: 'Max',
107
+ user: {
108
+ id: 'okechukwu',
109
+ },
110
+ } as unknown as MessageResponse;
111
+
107
112
  const { result } = renderHook(
108
- () => useLatestMessagePreview(CHANNEL_WITH_MENTIONED_USERS, FORCE_UPDATE, LATEST_MESSAGE),
113
+ () => useLatestMessagePreview(CHANNEL_WITH_MENTIONED_USERS, FORCE_UPDATE, latestMessage),
109
114
  { wrapper: ChatProvider },
110
115
  );
111
116
  await waitFor(() => {
@@ -117,8 +122,15 @@ describe('useLatestMessagePreview', () => {
117
122
  });
118
123
 
119
124
  it('should return the latest command preview', async () => {
125
+ const latestMessage = {
126
+ command: 'giphy',
127
+ user: {
128
+ id: 'okechukwu',
129
+ },
130
+ } as unknown as MessageResponse;
131
+
120
132
  const { result } = renderHook(
121
- () => useLatestMessagePreview(CHANNEL_WITH_MESSAGES_COMMAND, FORCE_UPDATE, LATEST_MESSAGE),
133
+ () => useLatestMessagePreview(CHANNEL_WITH_MESSAGES_COMMAND, FORCE_UPDATE, latestMessage),
122
134
  { wrapper: ChatProvider },
123
135
  );
124
136
  await waitFor(() => {
@@ -130,11 +142,18 @@ describe('useLatestMessagePreview', () => {
130
142
  });
131
143
 
132
144
  it('should return an attachment preview', async () => {
145
+ const latestMessage = {
146
+ attachments: ['arbitrary value'],
147
+ user: {
148
+ id: 'okechukwu',
149
+ },
150
+ } as unknown as MessageResponse;
151
+
133
152
  const { result } = renderHook(
134
- () =>
135
- useLatestMessagePreview(CHANNEL_WITH_MESSAGES_ATTACHMENTS, FORCE_UPDATE, LATEST_MESSAGE),
153
+ () => useLatestMessagePreview(CHANNEL_WITH_MESSAGES_ATTACHMENTS, FORCE_UPDATE, latestMessage),
136
154
  { wrapper: ChatProvider },
137
155
  );
156
+
138
157
  await waitFor(() => {
139
158
  expect(result.current.previews).toEqual([
140
159
  { bold: true, text: '@okechukwu: ' },
@@ -142,4 +161,20 @@ describe('useLatestMessagePreview', () => {
142
161
  ]);
143
162
  });
144
163
  });
164
+
165
+ it('should default to messages from the channel state if latestMessage is undefined', async () => {
166
+ const latestMessage = undefined;
167
+
168
+ const { result } = renderHook(
169
+ () => useLatestMessagePreview(CHANNEL_WITH_MESSAGES_TEXT, FORCE_UPDATE, latestMessage),
170
+ { wrapper: ChatProvider },
171
+ );
172
+
173
+ await waitFor(() => {
174
+ expect(result.current.previews).toEqual([
175
+ { bold: true, text: '@okechukwu: ' },
176
+ { bold: false, text: 'jkbkbiubicbi' },
177
+ ]);
178
+ });
179
+ });
145
180
  });
@@ -207,7 +207,10 @@ const getLatestMessagePreview = <
207
207
  status: MessageReadStatus.NOT_SENT_BY_CURRENT_USER,
208
208
  };
209
209
  }
210
- const message = lastMessage || messages.length ? messages[messages.length - 1] : undefined;
210
+
211
+ const channelStateLastMessage = messages.length ? messages[messages.length - 1] : undefined;
212
+
213
+ const message = lastMessage !== undefined ? lastMessage : channelStateLastMessage;
211
214
 
212
215
  return {
213
216
  created_at: getLatestMessageDisplayDate(message, tDateTimeParser),
@@ -15,8 +15,9 @@ import { useOverlayContext } from '../../contexts/overlayContext/OverlayContext'
15
15
  import { DeepPartial, ThemeProvider } from '../../contexts/themeContext/ThemeContext';
16
16
  import type { Theme } from '../../contexts/themeContext/utils/theme';
17
17
  import {
18
- TranslationContextValue,
18
+ DEFAULT_USER_LANGUAGE,
19
19
  TranslationProvider,
20
+ TranslatorFunctions,
20
21
  } from '../../contexts/translationContext/TranslationContext';
21
22
  import { useStreami18n } from '../../hooks/useStreami18n';
22
23
  import init from '../../init';
@@ -129,7 +130,7 @@ const ChatWithContext = <
129
130
  const { children, client, closeConnectionOnBackground = true, i18nInstance, style } = props;
130
131
 
131
132
  const [channel, setChannel] = useState<Channel<StreamChatGenerics>>();
132
- const [translators, setTranslators] = useState<TranslationContextValue>({
133
+ const [translators, setTranslators] = useState<TranslatorFunctions>({
133
134
  t: (key: string) => key,
134
135
  tDateTimeParser: (input?: string | number | Date) => Dayjs(input),
135
136
  });
@@ -178,7 +179,9 @@ const ChatWithContext = <
178
179
 
179
180
  return (
180
181
  <ChatProvider<StreamChatGenerics> value={chatContext}>
181
- <TranslationProvider value={translators}>
182
+ <TranslationProvider
183
+ value={{ ...translators, userLanguage: client.user?.language || DEFAULT_USER_LANGUAGE }}
184
+ >
182
185
  <ThemeProvider style={style}>{children}</ThemeProvider>
183
186
  </TranslationProvider>
184
187
  </ChatProvider>
@@ -273,21 +273,25 @@ export const ImageGallery = <
273
273
 
274
274
  const attachmentPhotos = attachmentImages.map((a) => {
275
275
  const imageUrl = getUrlOfImageAttachment(a) as string;
276
+ const giphyURL = a.giphy?.[giphyVersion]?.url || a.thumb_url || a.image_url;
276
277
 
277
278
  return {
278
279
  channelId: cur.cid,
279
280
  created_at: cur.created_at,
280
281
  id: `photoId-${cur.id}-${imageUrl}`,
281
282
  messageId: cur.id,
282
- mime_type: a.mime_type,
283
+ mime_type: a.type === 'giphy' ? 'image/gif' : a.mime_type,
283
284
  original_height: a.original_height,
284
285
  original_width: a.original_width,
285
286
  type: a.type,
286
- uri: getResizedImageUrl({
287
- height: screenHeight,
288
- url: imageUrl,
289
- width: screenWidth,
290
- }),
287
+ uri:
288
+ a.type === 'giphy'
289
+ ? giphyURL
290
+ : getResizedImageUrl({
291
+ height: screenHeight,
292
+ url: imageUrl,
293
+ width: screenWidth,
294
+ }),
291
295
  user: cur.user,
292
296
  user_id: cur.user_id,
293
297
  };
@@ -171,7 +171,8 @@ export const ImageGalleryFooterWithContext = <
171
171
  }-${selectedIndex}.${extension}`,
172
172
  fromUrl: photo.uri,
173
173
  });
174
- await shareImage({ type: photo.mime_type, url: localFile });
174
+ // `image/jpeg` is added for the case where the mime_type isn't available for a file/image
175
+ await shareImage({ type: photo.mime_type ?? 'image/jpeg', url: localFile });
175
176
  await deleteFile({ uri: localFile });
176
177
  } catch (error) {
177
178
  console.log(error);
@@ -503,6 +503,8 @@ const MessageWithContext = <
503
503
  updateMessage,
504
504
  });
505
505
 
506
+ const { userLanguage } = useTranslationContext();
507
+
506
508
  const showMessageOverlay = async (messageReactions = false, error = errorOrFailed) => {
507
509
  await dismissKeyboard();
508
510
 
@@ -552,6 +554,7 @@ const MessageWithContext = <
552
554
  ownCapabilities,
553
555
  supportedReactions,
554
556
  threadList,
557
+ userLanguage,
555
558
  videos: attachments.videos,
556
559
  });
557
560
 
@@ -14,7 +14,9 @@ import {
14
14
  import { useTheme } from '../../../contexts/themeContext/ThemeContext';
15
15
 
16
16
  import type { MarkdownStyle, Theme } from '../../../contexts/themeContext/utils/theme';
17
+ import { useTranslatedMessage } from '../../../hooks/useTranslatedMessage';
17
18
  import type { DefaultStreamChatGenerics } from '../../../types/types';
19
+ import type { MessageType } from '../../MessageList/hooks/useMessageList';
18
20
 
19
21
  const styles = StyleSheet.create({
20
22
  textContainer: { maxWidth: 250, paddingHorizontal: 16 },
@@ -75,6 +77,10 @@ const MessageTextContainerWithContext = <
75
77
  },
76
78
  } = theme;
77
79
 
80
+ const translatedMessage = useTranslatedMessage<StreamChatGenerics>(
81
+ message,
82
+ ) as MessageType<StreamChatGenerics>;
83
+
78
84
  if (!message.text) return null;
79
85
 
80
86
  const markdownStyles = { ...markdown, ...markdownStylesProp };
@@ -94,7 +100,7 @@ const MessageTextContainerWithContext = <
94
100
  ...markdownStyles,
95
101
  ...(onlyEmojis ? onlyEmojiMarkdown : {}),
96
102
  },
97
- message,
103
+ message: translatedMessage,
98
104
  messageOverlay,
99
105
  messageTextNumberOfLines,
100
106
  onLongPress,
@@ -0,0 +1,108 @@
1
+ import React from 'react';
2
+ import { Text } from 'react-native';
3
+
4
+ import { cleanup, render, waitFor } from '@testing-library/react-native';
5
+
6
+ import { OverlayProvider } from '../../../../contexts/overlayContext/OverlayProvider';
7
+ import { ThemeProvider } from '../../../../contexts/themeContext/ThemeContext';
8
+ import { defaultTheme } from '../../../../contexts/themeContext/utils/theme';
9
+ import { getOrCreateChannelApi } from '../../../../mock-builders/api/getOrCreateChannel';
10
+ import { useMockedApis } from '../../../../mock-builders/api/useMockedApis';
11
+ import { generateChannelResponse } from '../../../../mock-builders/generator/channel';
12
+ import {
13
+ generateMessage,
14
+ generateStaticMessage,
15
+ } from '../../../../mock-builders/generator/message';
16
+ import { generateStaticUser } from '../../../../mock-builders/generator/user';
17
+ import { getTestClientWithUser } from '../../../../mock-builders/mock';
18
+ import { Channel } from '../../../Channel/Channel';
19
+ import { Chat } from '../../../Chat/Chat';
20
+ import type { MessageType } from '../../../MessageList/hooks/useMessageList';
21
+ import { MessageList } from '../../../MessageList/MessageList';
22
+ import { MessageTextContainer } from '../MessageTextContainer';
23
+
24
+ afterEach(cleanup);
25
+
26
+ describe('MessageTextContainer', () => {
27
+ it('should render message text container', async () => {
28
+ const staticUser = generateStaticUser(1);
29
+ const message = generateMessage({
30
+ user: { ...staticUser, image: undefined },
31
+ });
32
+ const { getByTestId, getByText, rerender, toJSON } = render(
33
+ <ThemeProvider style={defaultTheme}>
34
+ <MessageTextContainer message={message as unknown as MessageType} />
35
+ </ThemeProvider>,
36
+ );
37
+
38
+ await waitFor(() => {
39
+ expect(getByTestId('message-text-container')).toBeTruthy();
40
+ expect(getByText(message.text)).toBeTruthy();
41
+ });
42
+
43
+ rerender(
44
+ <ThemeProvider style={defaultTheme}>
45
+ <MessageTextContainer
46
+ message={message as unknown as MessageType}
47
+ MessageText={({ message }) => <Text testID='message-text'>{message?.text}</Text>}
48
+ />
49
+ </ThemeProvider>,
50
+ );
51
+
52
+ await waitFor(() => {
53
+ expect(getByTestId('message-text-container')).toBeTruthy();
54
+ expect(getByTestId('message-text')).toBeTruthy();
55
+ expect(getByText(message.text)).toBeTruthy();
56
+ });
57
+
58
+ const staticMessage = generateStaticMessage('Hello World', {
59
+ user: staticUser,
60
+ });
61
+
62
+ rerender(
63
+ <ThemeProvider style={defaultTheme}>
64
+ <MessageTextContainer message={staticMessage as unknown as MessageType} />
65
+ </ThemeProvider>,
66
+ );
67
+
68
+ await waitFor(() => {
69
+ expect(toJSON()).toMatchSnapshot();
70
+ });
71
+ });
72
+
73
+ it('should display a translated message if applicable', async () => {
74
+ const chatClient = await getTestClientWithUser({ id: 'mads', language: 'no' });
75
+
76
+ const message = {
77
+ i18n: {
78
+ no_text: 'Hallo verden!',
79
+ },
80
+ text: 'Hello world!',
81
+ };
82
+
83
+ const mockedChannel = generateChannelResponse({
84
+ id: 'chans',
85
+ messages: [message],
86
+ });
87
+
88
+ useMockedApis(chatClient, [getOrCreateChannelApi(mockedChannel)]);
89
+ const channel = chatClient.channel('messaging', 'chans');
90
+ await channel.watch();
91
+
92
+ const TestComponent = () => (
93
+ <OverlayProvider>
94
+ <Chat client={chatClient}>
95
+ <Channel channel={channel}>
96
+ <MessageList />
97
+ </Channel>
98
+ </Chat>
99
+ </OverlayProvider>
100
+ );
101
+
102
+ const { getByText } = render(<TestComponent />);
103
+
104
+ await waitFor(() => {
105
+ expect(getByText(message.i18n.no_text)).toBeTruthy();
106
+ });
107
+ });
108
+ });
@@ -669,10 +669,14 @@ export const MessageInputProvider = <
669
669
  }
670
670
  }
671
671
 
672
+ // To get the mime type of the image from the file name and send it as an response for an image
673
+ const mime_type: string | boolean = lookup(image.file.filename as string);
674
+
672
675
  if (image.state === FileState.UPLOADED || image.state === FileState.FINISHED) {
673
676
  attachments.push({
674
677
  fallback: image.file.name,
675
678
  image_url: image.url,
679
+ mime_type,
676
680
  original_height: image.height,
677
681
  original_width: image.width,
678
682
  type: 'image',
@@ -689,11 +693,14 @@ export const MessageInputProvider = <
689
693
  sending.current = false;
690
694
  return;
691
695
  }
696
+ const mime_type: string | boolean = lookup(file.file.name as string);
697
+
692
698
  if (file.state === FileState.UPLOADED || file.state === FileState.FINISHED) {
693
699
  if (file.file.type?.startsWith('image/')) {
694
700
  attachments.push({
695
701
  fallback: file.file.name,
696
702
  image_url: file.url,
703
+ mime_type,
697
704
  type: 'image',
698
705
  } as Attachment<StreamChatGenerics>);
699
706
  } else if (file.file.type?.startsWith('audio/')) {
@@ -6,6 +6,7 @@ Object {
6
6
  Object {
7
7
  "fallback": undefined,
8
8
  "image_url": undefined,
9
+ "mime_type": false,
9
10
  "original_height": undefined,
10
11
  "original_width": undefined,
11
12
  "type": "image",
@@ -65,6 +66,7 @@ Object {
65
66
  Object {
66
67
  "fallback": "dummy.pdf",
67
68
  "image_url": undefined,
69
+ "mime_type": "application/pdf",
68
70
  "type": "image",
69
71
  },
70
72
  ],
@@ -85,6 +87,7 @@ Object {
85
87
  Object {
86
88
  "fallback": undefined,
87
89
  "image_url": undefined,
90
+ "mime_type": false,
88
91
  "original_height": undefined,
89
92
  "original_width": undefined,
90
93
  "type": "image",
@@ -92,6 +95,7 @@ Object {
92
95
  Object {
93
96
  "fallback": undefined,
94
97
  "image_url": undefined,
98
+ "mime_type": false,
95
99
  "original_height": undefined,
96
100
  "original_width": undefined,
97
101
  "type": "image",
@@ -1,6 +1,6 @@
1
1
  import React, { PropsWithChildren, useContext } from 'react';
2
2
 
3
- import type { Attachment } from 'stream-chat';
3
+ import type { Attachment, TranslationLanguages } from 'stream-chat';
4
4
 
5
5
  import { useResettableState } from './hooks/useResettableState';
6
6
 
@@ -43,6 +43,7 @@ export type MessageOverlayData<
43
43
  ownCapabilities?: OwnCapabilitiesContextValue;
44
44
  supportedReactions?: ReactionData[];
45
45
  threadList?: boolean;
46
+ userLanguage?: TranslationLanguages;
46
47
  videos?: Attachment<StreamChatGenerics>[];
47
48
  };
48
49
 
@@ -35,8 +35,9 @@ import { ImageGalleryProvider } from '../imageGalleryContext/ImageGalleryContext
35
35
  import { MessageOverlayProvider } from '../messageOverlayContext/MessageOverlayContext';
36
36
  import { ThemeProvider } from '../themeContext/ThemeContext';
37
37
  import {
38
- TranslationContextValue,
38
+ DEFAULT_USER_LANGUAGE,
39
39
  TranslationProvider,
40
+ TranslatorFunctions,
40
41
  } from '../translationContext/TranslationContext';
41
42
 
42
43
  /**
@@ -127,7 +128,7 @@ export const OverlayProvider = <
127
128
 
128
129
  const bottomSheetRef = useRef<BottomSheet>(null);
129
130
 
130
- const [translators, setTranslators] = useState<TranslationContextValue>({
131
+ const [translators, setTranslators] = useState<TranslatorFunctions>({
131
132
  t: (key: string) => key,
132
133
  tDateTimeParser: (input?: string | number | Date) => Dayjs(input),
133
134
  });
@@ -198,7 +199,7 @@ export const OverlayProvider = <
198
199
  if (loadingTranslators) return null;
199
200
 
200
201
  return (
201
- <TranslationProvider value={translators}>
202
+ <TranslationProvider value={{ ...translators, userLanguage: DEFAULT_USER_LANGUAGE }}>
202
203
  <OverlayContext.Provider value={overlayContext}>
203
204
  <MessageOverlayProvider<StreamChatGenerics>>
204
205
  <AttachmentPickerProvider value={attachmentPickerContext}>
@@ -5,10 +5,14 @@ import Dayjs from 'dayjs';
5
5
  import type { TFunction } from 'i18next';
6
6
  import type { Moment } from 'moment';
7
7
 
8
+ import type { TranslationLanguages } from 'stream-chat';
9
+
8
10
  import type { DefaultStreamChatGenerics } from '../../types/types';
9
11
  import { getDisplayName } from '../utils/getDisplayName';
10
12
  import { isTestEnvironment } from '../utils/isTestEnvironment';
11
13
 
14
+ export const DEFAULT_USER_LANGUAGE: TranslationLanguages = 'en';
15
+
12
16
  export const isDayOrMoment = (output: TDateTimeParserOutput): output is Dayjs.Dayjs | Moment =>
13
17
  (output as Dayjs.Dayjs | Moment).isSame != null;
14
18
 
@@ -18,14 +22,19 @@ export type TDateTimeParserOutput = string | number | Date | Dayjs.Dayjs | Momen
18
22
 
19
23
  export type TDateTimeParser = (input?: TDateTimeParserInput) => TDateTimeParserOutput;
20
24
 
21
- export type TranslationContextValue = {
25
+ export type TranslatorFunctions = {
22
26
  t: TFunction | ((key: string) => string);
23
27
  tDateTimeParser: TDateTimeParser;
24
28
  };
25
29
 
30
+ export type TranslationContextValue = TranslatorFunctions & {
31
+ userLanguage: TranslationLanguages;
32
+ };
33
+
26
34
  const defaultTranslationContextValue: TranslationContextValue = {
27
35
  t: (key: string) => key,
28
36
  tDateTimeParser: (input) => Dayjs(input),
37
+ userLanguage: DEFAULT_USER_LANGUAGE,
29
38
  };
30
39
 
31
40
  export const TranslationContext = React.createContext<TranslationContextValue>(
@@ -0,0 +1,106 @@
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 { MessageResponse } from 'stream-chat';
7
+
8
+ import {
9
+ MessageOverlayContextValue,
10
+ MessageOverlayProvider,
11
+ } from '../../contexts/messageOverlayContext/MessageOverlayContext';
12
+ import {
13
+ TranslationContextValue,
14
+ TranslationProvider,
15
+ } from '../../contexts/translationContext/TranslationContext';
16
+ import { useTranslatedMessage } from '../useTranslatedMessage';
17
+
18
+ type TestComponentProps = {
19
+ message: MessageResponse;
20
+ };
21
+
22
+ const TestComponent = ({ message }: TestComponentProps) => {
23
+ const translatedMessage = useTranslatedMessage(message);
24
+ return <Text>{translatedMessage.text}</Text>;
25
+ };
26
+
27
+ describe('useTranslatedMessage', () => {
28
+ it("doesn't translate a message if there isn't a translation available for the userLanguage", async () => {
29
+ const message = {
30
+ i18n: {
31
+ nl_text: 'Hallo wereld!',
32
+ },
33
+ text: 'Hello world!',
34
+ } as MessageResponse;
35
+
36
+ const { getByText } = render(
37
+ <TranslationProvider value={{ userLanguage: 'es' } as TranslationContextValue}>
38
+ <TestComponent message={message} />
39
+ </TranslationProvider>,
40
+ );
41
+
42
+ await waitFor(() => {
43
+ expect(getByText('Hello world!')).toBeTruthy();
44
+ });
45
+ });
46
+
47
+ it('returns a new message with the text translated if userLanguage is set and the translation exists', async () => {
48
+ const message = {
49
+ i18n: {
50
+ no_text: 'Hallo verden!',
51
+ },
52
+ text: 'Hello world!',
53
+ } as MessageResponse;
54
+
55
+ const { getByText } = render(
56
+ <TranslationProvider value={{ userLanguage: 'no' } as TranslationContextValue}>
57
+ <TestComponent message={message} />
58
+ </TranslationProvider>,
59
+ );
60
+
61
+ await waitFor(() => {
62
+ expect(getByText('Hallo verden!')).toBeTruthy();
63
+ });
64
+ });
65
+
66
+ it("returns the original text if the message doesn't contain any translations", async () => {
67
+ const message = {
68
+ text: 'Hello world!',
69
+ } as MessageResponse;
70
+
71
+ const { getByText } = render(<TestComponent message={message} />);
72
+
73
+ await waitFor(() => {
74
+ expect(getByText('Hello world!')).toBeTruthy();
75
+ });
76
+ });
77
+
78
+ it('uses userLanguage from messageOverlayData if it is set', async () => {
79
+ const message = {
80
+ i18n: {
81
+ nl_text: 'Hallo wereld!',
82
+ no_text: 'Hallo verden!',
83
+ },
84
+ text: 'Hello world!',
85
+ } as MessageResponse;
86
+
87
+ /**
88
+ * The reason for the as unknown as MessageOverlayContextValue is that the provider
89
+ * uses useResettableState, wrapping the input to fit in the data structure.
90
+ *
91
+ * In practice, the userLanguage will be set with the setData call and not directly
92
+ * in the provider.
93
+ * */
94
+ const { getByText } = render(
95
+ <MessageOverlayProvider
96
+ value={{ userLanguage: 'nl' } as unknown as MessageOverlayContextValue}
97
+ >
98
+ <TestComponent message={message} />
99
+ </MessageOverlayProvider>,
100
+ );
101
+
102
+ await waitFor(() => {
103
+ expect(getByText('Hallo wereld!')).toBeTruthy();
104
+ });
105
+ });
106
+ });
@@ -2,14 +2,14 @@ import { useEffect, useState } from 'react';
2
2
 
3
3
  import { useIsMountedRef } from './useIsMountedRef';
4
4
 
5
- import type { TranslationContextValue } from '../contexts/translationContext/TranslationContext';
5
+ import type { TranslatorFunctions } from '../contexts/translationContext/TranslationContext';
6
6
  import { Streami18n } from '../utils/Streami18n';
7
7
 
8
8
  export const useStreami18n = ({
9
9
  i18nInstance,
10
10
  setTranslators,
11
11
  }: {
12
- setTranslators: React.Dispatch<React.SetStateAction<TranslationContextValue>>;
12
+ setTranslators: React.Dispatch<React.SetStateAction<TranslatorFunctions>>;
13
13
  i18nInstance?: Streami18n;
14
14
  }) => {
15
15
  const [loadingTranslators, setLoadingTranslators] = useState(true);