Skip to content

Commit 22f7e6c

Browse files
committed
fix: properly cover rerendering issues
1 parent 63895f2 commit 22f7e6c

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

package/src/components/ChannelList/ChannelList.tsx

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

33
import type { FlatList } from 'react-native-gesture-handler';
44

@@ -16,16 +16,16 @@ import { ChannelListHeaderErrorIndicator } from './ChannelListHeaderErrorIndicat
1616
import { ChannelListHeaderNetworkDownIndicator } from './ChannelListHeaderNetworkDownIndicator';
1717
import { ChannelListLoadingIndicator } from './ChannelListLoadingIndicator';
1818
import { ChannelListMessenger, ChannelListMessengerProps } from './ChannelListMessenger';
19-
import { useAddedToChannelNotification } from './hooks/listeners/useAddedToChannelNotification';
20-
import { useChannelDeleted } from './hooks/listeners/useChannelDeleted';
21-
import { useChannelHidden } from './hooks/listeners/useChannelHidden';
22-
import { useChannelTruncated } from './hooks/listeners/useChannelTruncated';
23-
import { useChannelUpdated } from './hooks/listeners/useChannelUpdated';
24-
import { useChannelVisible } from './hooks/listeners/useChannelVisible';
25-
import { useNewMessage } from './hooks/listeners/useNewMessage';
26-
import { useNewMessageNotification } from './hooks/listeners/useNewMessageNotification';
27-
import { useRemovedFromChannelNotification } from './hooks/listeners/useRemovedFromChannelNotification';
28-
import { useUserPresence } from './hooks/listeners/useUserPresence';
19+
// import { useAddedToChannelNotification } from './hooks/listeners/useAddedToChannelNotification';
20+
// import { useChannelDeleted } from './hooks/listeners/useChannelDeleted';
21+
// import { useChannelHidden } from './hooks/listeners/useChannelHidden';
22+
// import { useChannelTruncated } from './hooks/listeners/useChannelTruncated';
23+
// import { useChannelUpdated } from './hooks/listeners/useChannelUpdated';
24+
// import { useChannelVisible } from './hooks/listeners/useChannelVisible';
25+
// import { useNewMessage } from './hooks/listeners/useNewMessage';
26+
// import { useNewMessageNotification } from './hooks/listeners/useNewMessageNotification';
27+
// import { useRemovedFromChannelNotification } from './hooks/listeners/useRemovedFromChannelNotification';
28+
// import { useUserPresence } from './hooks/listeners/useUserPresence';
2929
import { useCreateChannelsContext } from './hooks/useCreateChannelsContext';
3030
import { usePaginatedChannels } from './hooks/usePaginatedChannels';
3131
import { Skeleton as SkeletonDefault } from './Skeleton';
@@ -248,15 +248,15 @@ export const ChannelList = <
248248
lockChannelOrder = false,
249249
maxUnreadCount = 255,
250250
numberOfSkeletons = 6,
251-
onAddedToChannel,
252-
onChannelDeleted,
253-
onChannelHidden,
254-
onChannelTruncated,
255-
onChannelUpdated,
256-
onChannelVisible,
257-
onNewMessage,
258-
onNewMessageNotification,
259-
onRemovedFromChannel,
251+
// onAddedToChannel,
252+
// onChannelDeleted,
253+
// onChannelHidden,
254+
// onChannelTruncated,
255+
// onChannelUpdated,
256+
// onChannelVisible,
257+
// onNewMessage,
258+
// onNewMessageNotification,
259+
// onRemovedFromChannel,
260260
onSelect,
261261
options = DEFAULT_OPTIONS,
262262
Preview = ChannelPreviewMessenger,
@@ -272,20 +272,27 @@ export const ChannelList = <
272272
} = props;
273273

274274
const [forceUpdate, setForceUpdate] = useState(0);
275-
const [channelManager, setChannelManager] = useState<ChannelManager<StreamChatGenerics> | null>(
276-
null,
277-
);
278275
const { client, enableOfflineSupport } = useChatContext<StreamChatGenerics>();
276+
const [channelManager, setChannelManager] = useState<ChannelManager<StreamChatGenerics>>(
277+
client.createChannelManager({ options: { lockChannelOrder } }),
278+
);
279+
const clientRef = useRef(client);
279280

280281
useEffect(() => {
281-
const manager = new ChannelManager<StreamChatGenerics>({ client });
282-
manager.registerSubscriptions();
283-
setChannelManager(manager);
282+
if (clientRef.current !== client) {
283+
const manager = client.createChannelManager({ options: { lockChannelOrder } });
284+
setChannelManager(manager);
285+
clientRef.current = client;
286+
}
287+
}, [client, lockChannelOrder]);
288+
289+
useEffect(() => {
290+
channelManager.registerSubscriptions();
284291

285292
return () => {
286-
manager?.unregisterSubscriptions();
293+
channelManager.unregisterSubscriptions();
287294
};
288-
}, [client]);
295+
}, [channelManager]);
289296

290297
const {
291298
channels,
@@ -299,12 +306,12 @@ export const ChannelList = <
299306
reloadList,
300307
staticChannelsActive,
301308
} = usePaginatedChannels<StreamChatGenerics>({
309+
channelManager,
302310
enableOfflineSupport,
303311
filters,
304312
options,
305313
setForceUpdate,
306314
sort,
307-
channelManager,
308315
});
309316

310317
// Setup event listeners

package/src/components/ChannelList/hooks/usePaginatedChannels.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ const waitSeconds = (seconds: number) =>
2626

2727
type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
2828
{
29+
channelManager: ChannelManager<StreamChatGenerics>;
2930
enableOfflineSupport: boolean;
3031
filters: ChannelFilters<StreamChatGenerics>;
3132
options: ChannelOptions;
3233
setForceUpdate: React.Dispatch<React.SetStateAction<number>>;
3334
sort: ChannelSort<StreamChatGenerics>;
34-
channelManager?: ChannelManager<StreamChatGenerics>;
3535
};
3636

3737
const DEFAULT_OPTIONS = {
@@ -45,7 +45,9 @@ type QueryType = 'queryLocalDB' | 'reload' | 'refresh' | 'loadChannels';
4545

4646
export type QueryChannels = (queryType?: QueryType, retryCount?: number) => Promise<void>;
4747

48-
const selector = (nextValue: ChannelManagerState) =>
48+
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
49+
nextValue: ChannelManagerState<StreamChatGenerics>,
50+
) =>
4951
({
5052
channels: nextValue.channels,
5153
pagination: nextValue.pagination,
@@ -215,6 +217,7 @@ export const usePaginatedChannels = <
215217
const sortStr = useMemo(() => JSON.stringify(sort), [sort]);
216218

217219
useEffect(() => {
220+
console.log('TRIGGERING', !channelManager);
218221
const loadOfflineChannels = async () => {
219222
if (!client?.user?.id) return;
220223

@@ -294,7 +297,9 @@ export const usePaginatedChannels = <
294297
// : (activeQueryType.current === 'reload' || activeQueryType.current === null) &&
295298
// channels === null,
296299
loadingChannels:
297-
activeQueryType.current === 'queryLocalDB' || !channelManager ? true : pagination?.isLoading,
300+
activeQueryType.current === 'queryLocalDB' || !channelManager
301+
? true
302+
: pagination?.isLoading || channels == null,
298303
loadingNextPage: pagination?.isLoadingNext,
299304
loadNextPage: channelManager?.loadNext,
300305
refreshing: activeQueryType.current === 'refresh',

0 commit comments

Comments
 (0)