Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep a list of unique channels in the ChannelList when paginating #2115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/components/ChannelList/__tests__/ChannelList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,35 @@ describe('ChannelList', () => {
expect(results).toHaveNoViolations();
});

it('should show unique channels', async () => {
useMockedApis(chatClientUthred, [queryChannelsApi([testChannel1, testChannel2])]);
const ChannelPreview = (props) => <div data-testid={props.channel.id} role='listitem' />;
render(
<Chat client={chatClientUthred}>
<ChannelList filters={{}} options={{ limit: 2 }} Preview={ChannelPreview} />
</Chat>,
);

await waitFor(() => {
expect(screen.getByTestId(testChannel1.channel.id)).toBeInTheDocument();
expect(screen.getByTestId(testChannel2.channel.id)).toBeInTheDocument();
expect(screen.getAllByRole('listitem')).toHaveLength(2);
});

useMockedApis(chatClientUthred, [queryChannelsApi([testChannel1, testChannel3])]);

await act(() => {
fireEvent.click(screen.getByTestId('load-more-button'));
});

await waitFor(() => {
expect(screen.getByTestId(testChannel1.channel.id)).toBeInTheDocument();
expect(screen.getByTestId(testChannel2.channel.id)).toBeInTheDocument();
expect(screen.getByTestId(testChannel3.channel.id)).toBeInTheDocument();
expect(screen.getAllByRole('listitem')).toHaveLength(3);
});
});

describe('Default and custom active channel', () => {
let setActiveChannel;
const watchersConfig = { limit: 20, offset: 0 };
Expand Down
5 changes: 4 additions & 1 deletion src/components/ChannelList/hooks/usePaginatedChannels.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useMemo, useState } from 'react';
import uniqBy from 'lodash.uniqby';

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

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

const newChannels =
queryType === 'reload' ? channelQueryResponse : [...channels, ...channelQueryResponse];
queryType === 'reload'
? channelQueryResponse
: uniqBy([...channels, ...channelQueryResponse], 'cid');

setChannels(newChannels);
setHasNextPage(channelQueryResponse.length >= newOptions.limit);
Expand Down
Loading