Skip to content

Commit ad323ff

Browse files
Merge pull request #446 from GetStream/vishal/channel-list-fix1
Updates/fixes around network recovery
2 parents d803bab + d7edd78 commit ad323ff

File tree

6 files changed

+57
-31
lines changed

6 files changed

+57
-31
lines changed

src/components/Attachment/Gallery.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ export const Gallery = <At extends UnknownType = DefaultAttachmentType>(
143143
const immutableGalleryImages = images.reduce((returnArray, currentImage) => {
144144
const url = currentImage.image_url || currentImage.thumb_url;
145145
if (url) {
146-
returnArray.push({ url: makeImageCompatibleUrl(url) } as Immutable<
147-
IImageInfo
148-
>);
146+
returnArray.push({
147+
url: makeImageCompatibleUrl(url),
148+
} as Immutable<IImageInfo>);
149149
}
150150
return returnArray;
151151
}, [] as Immutable<IImageInfo>[]);

src/components/ChannelList/ChannelList.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, useState } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22

33
import {
44
ChannelListMessenger,
@@ -306,6 +306,11 @@ export const ChannelList = <
306306
sort,
307307
});
308308

309+
useEffect(() => {
310+
// TODO: Use sync api endpoint to refresh the list, instead of calling queryChannels.
311+
refreshList();
312+
}, [forceUpdate]);
313+
309314
// Setup event listeners
310315
useAddedToChannelNotification({
311316
onAddedToChannel,

src/components/ChannelList/hooks/usePaginatedChannels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const usePaginatedChannels = <
7878

7979
const newOptions = {
8080
limit: options?.limit ?? MAX_QUERY_CHANNELS_LIMIT,
81-
offset: queryType === 'reload' ? 0 : offset,
81+
offset: queryType === 'reload' || queryType === 'refresh' ? 0 : offset,
8282
...options,
8383
};
8484

@@ -90,7 +90,7 @@ export const usePaginatedChannels = <
9090
);
9191

9292
let newChannels;
93-
if (queryType === 'reload') {
93+
if (queryType === 'reload' || queryType === 'refresh') {
9494
newChannels = channelQueryResponse;
9595
} else {
9696
newChannels = [...channels, ...channelQueryResponse];

src/components/ChannelPreview/ChannelPreview.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const UnMemoizedChannelPreview = <
5454
>(
5555
props: ChannelPreviewProps<At, Ch, Co, Ev, Me, Re, Us>,
5656
) => {
57-
const { channel, Preview = ChannelPreviewMessenger } = props;
57+
const { channel, forceUpdate, Preview = ChannelPreviewMessenger } = props;
5858

5959
const { client } = useChatContext<At, Ch, Co, Ev, Me, Re, Us>();
6060

@@ -88,6 +88,10 @@ const UnMemoizedChannelPreview = <
8888
};
8989
}, []);
9090

91+
useEffect(() => {
92+
setUnread(channel.countUnread());
93+
}, [forceUpdate]);
94+
9195
useEffect(() => {
9296
const handleReadEvent = (event: Event<At, Ch, Co, Ev, Me, Re, Us>) => {
9397
if (event.user?.id === client.userID) {
@@ -124,7 +128,9 @@ const areEqual = <
124128
const { last_message_at: previousLast } = prevProps.channel.state;
125129
const { last_message_at: nextLast } = nextProps.channel.state;
126130

127-
return previousLast === nextLast;
131+
return (
132+
previousLast === nextLast && prevProps.forceUpdate === nextProps.forceUpdate
133+
);
128134
};
129135

130136
export const ChannelPreview = React.memo(

src/components/MessageList/MessageList.tsx

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
ThreadContextValue,
4343
useThreadContext,
4444
} from '../../contexts/threadContext/ThreadContext';
45-
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
4645
import { styled } from '../../styles/styledComponents';
4746

4847
import type { UserResponse } from 'stream-chat';
@@ -61,20 +60,7 @@ import type {
6160
UnknownType,
6261
} from '../../types/types';
6362

64-
const ErrorNotification = styled.View`
65-
align-items: center;
66-
background-color: #fae6e8;
67-
color: red;
68-
padding: 5px;
69-
z-index: 10;
70-
${({ theme }) => theme.messageList.errorNotification.css}
71-
`;
72-
73-
const ErrorNotificationText = styled.Text`
74-
background-color: #fae6e8;
75-
color: red;
76-
${({ theme }) => theme.messageList.errorNotificationText.css}
77-
`;
63+
import { NetworkDownIndicator as DefaultNetworkDownIndicator } from './NetworkDownIndicator';
7864

7965
const ListContainer = (styled(FlatList)`
8066
flex: 1;
@@ -185,6 +171,12 @@ export type MessageListProps<
185171
MessageSystem?: React.ComponentType<
186172
MessageSystemProps<At, Ch, Co, Ev, Me, Re, Us>
187173
>;
174+
/**
175+
* Custom UI component to render network down indicator
176+
*
177+
* Defaults to and accepts same props as: [NetworkDownIndicator](https://getstream.github.io/stream-chat-react-native/#NetworkDownIndicator)
178+
*/
179+
NetworkDownIndicator?: React.ComponentType;
188180
/** Turn off grouping of messages by user */
189181
noGroupByUser?: boolean;
190182
onListScroll?: ScrollViewProps['onScroll'];
@@ -261,6 +253,7 @@ export const MessageList = <
261253
onThreadSelect,
262254
setFlatListRef,
263255
threadList,
256+
NetworkDownIndicator = DefaultNetworkDownIndicator,
264257
TypingIndicator = DefaultTypingIndicator,
265258
} = props;
266259

@@ -280,7 +273,6 @@ export const MessageList = <
280273
Message: MessageFromContext,
281274
} = useMessagesContext<At, Ch, Co, Ev, Me, Re, Us>();
282275
const { loadMoreThread } = useThreadContext<At, Ch, Co, Ev, Me, Re, Us>();
283-
const { t } = useTranslationContext();
284276

285277
const messageList = useMessageList<At, Ch, Co, Ev, Me, Re, Us>({
286278
inverted,
@@ -455,13 +447,7 @@ export const MessageList = <
455447
showNotification={newMessagesNotification}
456448
/>
457449
)}
458-
{!isOnline && (
459-
<ErrorNotification testID='error-notification'>
460-
<ErrorNotificationText>
461-
{t('Connection failure, reconnecting now ...')}
462-
</ErrorNotificationText>
463-
</ErrorNotification>
464-
)}
450+
{!isOnline && <NetworkDownIndicator />}
465451
</View>
466452
{
467453
// Mask for edit state
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
3+
import { styled } from '../../styles/styledComponents';
4+
5+
const ErrorNotification = styled.View`
6+
align-items: center;
7+
background-color: #fae6e8;
8+
color: red;
9+
padding: 5px;
10+
z-index: 10;
11+
${({ theme }) => theme.messageList.errorNotification.css}
12+
`;
13+
14+
const ErrorNotificationText = styled.Text`
15+
background-color: #fae6e8;
16+
color: red;
17+
${({ theme }) => theme.messageList.errorNotificationText.css}
18+
`;
19+
20+
export const NetworkDownIndicator = () => {
21+
const { t } = useTranslationContext();
22+
return (
23+
<ErrorNotification testID='error-notification'>
24+
<ErrorNotificationText>
25+
{t('Connection failure, reconnecting now ...')}
26+
</ErrorNotificationText>
27+
</ErrorNotification>
28+
);
29+
};

0 commit comments

Comments
 (0)