Skip to content

Commit c3ceb8c

Browse files
authored
fix: remove return null check from OverlayProvider and Chat component (#2039)
* fix: remove return null check from OverlayProvider and Chat component * chore: update yarn lock files * refactor: moving the translator state inside the useStreami18n hook * fix: failing tests for giphy
1 parent 91bad48 commit c3ceb8c

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

package/src/components/Attachment/__tests__/Giphy.test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ import { generateMember } from '../../../mock-builders/generator/member';
2121
import { generateMessage } from '../../../mock-builders/generator/message';
2222
import { generateUser } from '../../../mock-builders/generator/user';
2323
import { getTestClientWithUser } from '../../../mock-builders/mock';
24+
import { Streami18n } from '../../../utils/Streami18n';
2425
import { ImageLoadingFailedIndicator } from '../../Attachment/ImageLoadingFailedIndicator';
2526
import { ImageLoadingIndicator } from '../../Attachment/ImageLoadingIndicator';
2627
import { Channel } from '../../Channel/Channel';
2728
import { Chat } from '../../Chat/Chat';
2829
import { MessageList } from '../../MessageList/MessageList';
2930
import { Giphy } from '../Giphy';
3031

32+
const streami18n = new Streami18n({
33+
language: 'en',
34+
});
35+
3136
describe('Giphy', () => {
3237
const getAttachmentComponent = (props) => {
3338
const message = generateMessage();
@@ -241,8 +246,8 @@ describe('Giphy', () => {
241246
await channel.watch();
242247

243248
const { getByTestId, queryByTestId } = render(
244-
<OverlayProvider>
245-
<Chat client={chatClient}>
249+
<OverlayProvider i18nInstance={streami18n}>
250+
<Chat client={chatClient} i18nInstance={streami18n}>
246251
<Channel channel={channel}>
247252
<MessageList />
248253
</Channel>
@@ -260,8 +265,8 @@ describe('Giphy', () => {
260265

261266
it('giphy attachment UI should render within the message list', async () => {
262267
const { queryByTestId } = render(
263-
<OverlayProvider>
264-
<Chat client={chatClient}>
268+
<OverlayProvider i18nInstance={streami18n}>
269+
<Chat client={chatClient} i18nInstance={streami18n}>
265270
<Channel channel={channel}>
266271
<MessageList />
267272
</Channel>
@@ -276,8 +281,8 @@ describe('Giphy', () => {
276281

277282
it('should render a error indicator in giphy image', async () => {
278283
const { getByA11yLabel, getByAccessibilityHint, queryByTestId } = render(
279-
<OverlayProvider>
280-
<Chat client={chatClient}>
284+
<OverlayProvider i18nInstance={streami18n}>
285+
<Chat client={chatClient} i18nInstance={streami18n}>
281286
<Channel channel={channel}>
282287
<MessageList />
283288
</Channel>
@@ -294,8 +299,8 @@ describe('Giphy', () => {
294299

295300
it('should render a loading indicator in giphy image and when successful render the image', async () => {
296301
const { getByA11yLabel, getByAccessibilityHint } = render(
297-
<OverlayProvider>
298-
<Chat client={chatClient}>
302+
<OverlayProvider i18nInstance={streami18n}>
303+
<Chat client={chatClient} i18nInstance={streami18n}>
299304
<Channel channel={channel}>
300305
<MessageList />
301306
</Channel>

package/src/components/Chat/Chat.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React, { PropsWithChildren, useEffect, useState } from 'react';
22
import { Image, Platform } from 'react-native';
33

4-
import Dayjs from 'dayjs';
5-
64
import type { Channel, StreamChat } from 'stream-chat';
75

86
import { useAppSettings } from './hooks/useAppSettings';
@@ -21,7 +19,6 @@ import type { Theme } from '../../contexts/themeContext/utils/theme';
2119
import {
2220
DEFAULT_USER_LANGUAGE,
2321
TranslationProvider,
24-
TranslatorFunctions,
2522
} from '../../contexts/translationContext/TranslationContext';
2623
import { useStreami18n } from '../../hooks/useStreami18n';
2724
import init from '../../init';
@@ -149,15 +146,9 @@ const ChatWithContext = <
149146
} = props;
150147

151148
const [channel, setChannel] = useState<Channel<StreamChatGenerics>>();
152-
const [translators, setTranslators] = useState<TranslatorFunctions>({
153-
t: (key: string) => key,
154-
tDateTimeParser: (input?: string | number | Date) => Dayjs(input),
155-
});
156149

157-
/**
158-
* Setup translators
159-
*/
160-
const loadingTranslators = useStreami18n(setTranslators, i18nInstance);
150+
// Setup translators
151+
const translators = useStreami18n(i18nInstance);
161152

162153
/**
163154
* Setup connection event listeners
@@ -226,8 +217,6 @@ const ChatWithContext = <
226217
enableOfflineSupport,
227218
});
228219

229-
if (loadingTranslators) return null;
230-
231220
return (
232221
<ChatProvider<StreamChatGenerics> value={chatContext}>
233222
<TranslationProvider

package/src/contexts/overlayContext/OverlayProvider.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Animated, {
1010
} from 'react-native-reanimated';
1111

1212
import type BottomSheet from '@gorhom/bottom-sheet';
13-
import Dayjs from 'dayjs';
1413

1514
import { OverlayContext, OverlayProviderProps } from './OverlayContext';
1615

@@ -36,7 +35,6 @@ import { ThemeProvider } from '../themeContext/ThemeContext';
3635
import {
3736
DEFAULT_USER_LANGUAGE,
3837
TranslationProvider,
39-
TranslatorFunctions,
4038
} from '../translationContext/TranslationContext';
4139

4240
/**
@@ -142,17 +140,13 @@ export const OverlayProvider = <
142140

143141
const bottomSheetRef = useRef<BottomSheet>(null);
144142

145-
const [translators, setTranslators] = useState<TranslatorFunctions>({
146-
t: (key: string) => key,
147-
tDateTimeParser: (input?: string | number | Date) => Dayjs(input),
148-
});
149143
const [overlay, setOverlay] = useState(value?.overlay || 'none');
150144

151145
const overlayOpacity = useSharedValue(0);
152146
const { height, width } = Dimensions.get('screen');
153147

154148
// Setup translators
155-
const loadingTranslators = useStreami18n(setTranslators, i18nInstance);
149+
const translators = useStreami18n(i18nInstance);
156150

157151
useEffect(() => {
158152
const backAction = () => {
@@ -216,8 +210,6 @@ export const OverlayProvider = <
216210
translucentStatusBar,
217211
};
218212

219-
if (loadingTranslators) return null;
220-
221213
return (
222214
<TranslationProvider value={{ ...translators, userLanguage: DEFAULT_USER_LANGUAGE }}>
223215
<OverlayContext.Provider value={overlayContext}>

package/src/hooks/useStreami18n.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { useEffect, useState } from 'react';
22

3+
import Dayjs from 'dayjs';
4+
35
import { useIsMountedRef } from './useIsMountedRef';
46

57
import type { TranslatorFunctions } from '../contexts/translationContext/TranslationContext';
68
import { Streami18n } from '../utils/Streami18n';
79

8-
export const useStreami18n = (
9-
setTranslators: React.Dispatch<React.SetStateAction<TranslatorFunctions>>,
10-
i18nInstance?: Streami18n,
11-
) => {
12-
const [loadingTranslators, setLoadingTranslators] = useState(true);
10+
export const useStreami18n = (i18nInstance?: Streami18n) => {
11+
const [translators, setTranslators] = useState<TranslatorFunctions>({
12+
t: (key: string) => key,
13+
tDateTimeParser: (input?: string | number | Date) => Dayjs(input),
14+
});
1315
const isMounted = useIsMountedRef();
16+
1417
useEffect(() => {
1518
let streami18n: Streami18n;
1619

@@ -28,20 +31,21 @@ export const useStreami18n = (
2831
streami18n.addOnLanguageChangeListener((t) => {
2932
updateTFunction(t);
3033
});
34+
3135
const { unsubscribe: unsubscribeOnTFuncOverrideListener } =
3236
streami18n.addOnTFunctionOverrideListener((t) => {
3337
updateTFunction(t);
3438
});
39+
3540
streami18n.getTranslators().then((translator) => {
3641
if (translator && isMounted.current) setTranslators(translator);
3742
});
3843

39-
setLoadingTranslators(false);
4044
return () => {
4145
unsubscribeOnTFuncOverrideListener();
4246
unsubscribeOnLanguageChangeListener();
4347
};
4448
}, [i18nInstance]);
4549

46-
return loadingTranslators;
50+
return translators;
4751
};

0 commit comments

Comments
 (0)