Skip to content

Commit 11173e1

Browse files
authored
fix: on pagination keep a unique list of channels in the ChannelList (#2115)
1 parent db18efd commit 11173e1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/components/ChannelList/__tests__/ChannelList.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,35 @@ describe('ChannelList', () => {
324324
expect(results).toHaveNoViolations();
325325
});
326326

327+
it('should show unique channels', async () => {
328+
useMockedApis(chatClientUthred, [queryChannelsApi([testChannel1, testChannel2])]);
329+
const ChannelPreview = (props) => <div data-testid={props.channel.id} role='listitem' />;
330+
render(
331+
<Chat client={chatClientUthred}>
332+
<ChannelList filters={{}} options={{ limit: 2 }} Preview={ChannelPreview} />
333+
</Chat>,
334+
);
335+
336+
await waitFor(() => {
337+
expect(screen.getByTestId(testChannel1.channel.id)).toBeInTheDocument();
338+
expect(screen.getByTestId(testChannel2.channel.id)).toBeInTheDocument();
339+
expect(screen.getAllByRole('listitem')).toHaveLength(2);
340+
});
341+
342+
useMockedApis(chatClientUthred, [queryChannelsApi([testChannel1, testChannel3])]);
343+
344+
await act(() => {
345+
fireEvent.click(screen.getByTestId('load-more-button'));
346+
});
347+
348+
await waitFor(() => {
349+
expect(screen.getByTestId(testChannel1.channel.id)).toBeInTheDocument();
350+
expect(screen.getByTestId(testChannel2.channel.id)).toBeInTheDocument();
351+
expect(screen.getByTestId(testChannel3.channel.id)).toBeInTheDocument();
352+
expect(screen.getAllByRole('listitem')).toHaveLength(3);
353+
});
354+
});
355+
327356
describe('Default and custom active channel', () => {
328357
let setActiveChannel;
329358
const watchersConfig = { limit: 20, offset: 0 };

src/components/ChannelList/hooks/usePaginatedChannels.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useMemo, useState } from 'react';
2+
import uniqBy from 'lodash.uniqby';
23

34
import { MAX_QUERY_CHANNELS_LIMIT } from '../utils';
45

@@ -52,7 +53,9 @@ export const usePaginatedChannels = <
5253
const channelQueryResponse = await client.queryChannels(filters, sort || {}, newOptions);
5354

5455
const newChannels =
55-
queryType === 'reload' ? channelQueryResponse : [...channels, ...channelQueryResponse];
56+
queryType === 'reload'
57+
? channelQueryResponse
58+
: uniqBy([...channels, ...channelQueryResponse], 'cid');
5659

5760
setChannels(newChannels);
5861
setHasNextPage(channelQueryResponse.length >= newOptions.limit);

0 commit comments

Comments
 (0)