Skip to content

Commit 2ef6b2e

Browse files
Fixing refresh/reload related UX on channellist
1 parent 39fdb36 commit 2ef6b2e

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

src/components/Channel/Channel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
SendMessageAPIResponse,
1414
StreamChat,
1515
Message as StreamMessage,
16+
UpdatedMessage,
1617
} from 'stream-chat';
17-
import { v4 as uuidv4 } from 'uuid';
1818

1919
import { EmptyStateIndicator as EmptyStateIndicatorDefault } from '../Indicators/EmptyStateIndicator';
2020
import { LoadingErrorIndicator as LoadingErrorIndicatorDefault } from '../Indicators/LoadingErrorIndicator';
@@ -36,7 +36,7 @@ import {
3636
ThreadProvider,
3737
} from '../../contexts/threadContext/ThreadContext';
3838
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
39-
import { emojiData as emojiDataDefault } from '../../utils/utils';
39+
import { emojiData as emojiDataDefault, randomId } from '../../utils/utils';
4040

4141
import type { LoadingErrorProps } from '../Indicators/LoadingErrorIndicator';
4242
import type { LoadingProps } from '../Indicators/LoadingIndicator';
@@ -111,7 +111,7 @@ export type ChannelProps<
111111
*/
112112
doUpdateMessageRequest?: (
113113
channelId: string,
114-
updatedMessage: StreamMessage<At, Me, Us>,
114+
updatedMessage: UpdatedMessage<At, Ch, Co, Me, Re, Us>,
115115
) => ReturnType<StreamChat<At, Ch, Co, Ev, Me, Re, Us>['updateMessage']>;
116116
emojiData?: MessagesContextValue<At, Ch, Co, Ev, Me, Re, Us>['emojiData'];
117117
/**
@@ -454,7 +454,7 @@ export const Channel = <
454454
attachments,
455455
created_at: new Date(),
456456
html: text,
457-
id: `${client.userID}-${uuidv4()}`,
457+
id: `${client.userID}-${randomId()}`,
458458
mentioned_users:
459459
mentioned_users?.map((userId) => ({
460460
id: userId,

src/components/ChannelList/ChannelListMessenger.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export type ChannelListMessengerProps<
7171
* Function to refresh the channel list that is similar to `reloadList`, but it doesn't wipe out existing channels
7272
* from UI before loading the new ones
7373
*/
74-
refreshList?: () => Promise<void>;
74+
refreshList?: () => undefined | Promise<void>;
7575

7676
/**
7777
* Removes all the existing channels from UI and loads fresh channels
@@ -152,19 +152,22 @@ export const ChannelListMessenger = <
152152
}, [loadingChannels]);
153153

154154
const HeaderIndicator: React.FC = () => {
155+
if (loadingChannels) return null;
156+
155157
if (!isOnline) {
156158
return <HeaderNetworkDownIndicator />;
157159
} else if (error) {
158160
return <HeaderErrorIndicator onPress={refreshList} />;
159161
}
162+
160163
return null;
161164
};
162165

163166
const renderItem = (channel: Channel<At, Ch, Co, Ev, Me, Re, Us>) => (
164167
<ChannelPreview<At, Ch, Co, Ev, Me, Re, Us> {...props} channel={channel} />
165168
);
166169

167-
if (error && !refreshing && !channels?.length) {
170+
if (error && !refreshing && !loadingChannels && !channels?.length) {
168171
return (
169172
<LoadingErrorIndicator
170173
error={error}

src/components/ChannelList/hooks/usePaginatedChannels.ts

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

33
import { MAX_QUERY_CHANNELS_LIMIT } from '../utils';
44

@@ -61,6 +61,7 @@ export const usePaginatedChannels = <
6161
const [loadingNextPage, setLoadingNextPage] = useState(false);
6262
const [offset, setOffset] = useState(0);
6363
const [refreshing, setRefreshing] = useState(false);
64+
const lastRefresh = useRef(new Date());
6465

6566
const queryChannels = async (
6667
queryType = '',
@@ -120,7 +121,18 @@ export const usePaginatedChannels = <
120121
};
121122

122123
const loadNextPage = hasNextPage ? queryChannels : undefined;
123-
const refreshList = () => queryChannels('refresh');
124+
const refreshList = () => {
125+
const now = new Date();
126+
// Only allow pull-to-refresh 5 seconds after last successful refresh.
127+
// @ts-ignore
128+
if (now - lastRefresh.current < 5000 && !error) {
129+
return;
130+
}
131+
132+
lastRefresh.current = new Date();
133+
return queryChannels('refresh');
134+
};
135+
124136
const reloadList = () => queryChannels('reload');
125137

126138
useEffect(() => {

src/components/MessageList/MessageList.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
TouchableOpacity,
77
View,
88
} from 'react-native';
9-
import { v4 as uuidv4 } from 'uuid';
109

1110
import {
1211
DateSeparatorProps,
@@ -61,6 +60,7 @@ import type {
6160
} from '../../types/types';
6261

6362
import { NetworkDownIndicator as DefaultNetworkDownIndicator } from './NetworkDownIndicator';
63+
import { randomId } from 'src/utils/utils';
6464

6565
const ListContainer = (styled(FlatList)`
6666
flex: 1;
@@ -87,13 +87,13 @@ const keyExtractor = <
8787
? typeof item.created_at === 'string'
8888
? item.created_at
8989
: item.created_at.toISOString()
90-
: uuidv4())
90+
: randomId())
9191
);
9292
}
9393
if (item.date && typeof item.date !== 'string') {
9494
return item.date.toISOString();
9595
}
96-
return uuidv4();
96+
return randomId();
9797
};
9898

9999
export type MessageListProps<

src/utils/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,15 @@ export const MESSAGE_ACTIONS = {
376376

377377
export const makeImageCompatibleUrl = (url: string) =>
378378
(url.indexOf('//') === 0 ? `https:${url}` : url).trim();
379+
380+
/** adopted from https://github.com/ai/nanoid/blob/master/non-secure/index.js */
381+
const alphabet =
382+
'ModuleSymbhasOwnPr0123456789ABCDEFGHNRVfgctiUvzKqYTJkLxpZXIjQW';
383+
export const randomId = () => {
384+
let id = '';
385+
for (let i = 0; i < 21; i++) {
386+
// eslint-disable-next-line no-bitwise
387+
id += alphabet[(Math.random() * 64) | 0];
388+
}
389+
return id;
390+
};

0 commit comments

Comments
 (0)