Skip to content
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/*
24
8 changes: 7 additions & 1 deletion examples/vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ const filters: ChannelFilters = {
type: 'messaging',
archived: false,
};
const options: ChannelOptions = { limit: 5, presence: true, state: true };
const options: ChannelOptions = {
// limit: 10,
// message_limit: 10,
// member_limit: 10,
presence: true,
state: true,
};
const sort: ChannelSort = { pinned_at: 1, last_message_at: -1, updated_at: -1 };

// @ts-ignore
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
"i18next": "^25.2.1",
"linkifyjs": "^4.3.2",
"lodash.debounce": "^4.0.8",
"lodash.defaultsdeep": "^4.6.1",
"lodash.mergewith": "^4.6.2",
"lodash.throttle": "^4.1.1",
"lodash.uniqby": "^4.7.0",
Expand Down
14 changes: 1 addition & 13 deletions src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import React, {
} from 'react';
import clsx from 'clsx';
import debounce from 'lodash.debounce';
import defaultsDeep from 'lodash.defaultsdeep';
import throttle from 'lodash.throttle';
import type {
APIErrorResponse,
Expand Down Expand Up @@ -65,7 +64,6 @@ import {
import { CHANNEL_CONTAINER_ID } from './constants';
import {
DEFAULT_HIGHLIGHT_DURATION,
DEFAULT_INITIAL_CHANNEL_PAGE_SIZE,
DEFAULT_JUMP_TO_PAGE_SIZE,
DEFAULT_NEXT_CHANNEL_PAGE_SIZE,
DEFAULT_THREAD_PAGE_SIZE,
Expand Down Expand Up @@ -293,7 +291,7 @@ const ChannelInner = (
activeUnreadHandler,
allowConcurrentAudioPlayback,
channel,
channelQueryOptions: propChannelQueryOptions,
channelQueryOptions,
children,
doDeleteMessageRequest,
doMarkReadRequest,
Expand All @@ -308,16 +306,6 @@ const ChannelInner = (
skipMessageDataMemoization,
} = props;

const channelQueryOptions: ChannelQueryOptions & {
messages: { limit: number };
} = useMemo(
() =>
defaultsDeep(propChannelQueryOptions, {
messages: { limit: DEFAULT_INITIAL_CHANNEL_PAGE_SIZE },
}),
[propChannelQueryOptions],
);

const { client, customClasses, latestMessageDatesByChannels, mutes, searchController } =
useChatContext('Channel');
const { t } = useTranslationContext('Channel');
Expand Down
67 changes: 43 additions & 24 deletions src/components/Channel/__tests__/Channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,11 @@ describe('Channel', () => {
it('should watch the current channel on mount', async () => {
const watchSpy = jest.spyOn(channel, 'watch');

await renderComponent({ channel, chatClient });
await renderComponent({
channel,
channelQueryOptions: { messages: { limit: 25 } },
chatClient,
});

await waitFor(() => {
expect(watchSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -380,9 +384,12 @@ describe('Channel', () => {
]);
let hasMore;
await act(() => {
renderComponent({ channel, chatClient }, ({ hasMore: contextHasMore }) => {
hasMore = contextHasMore;
});
renderComponent(
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ hasMore: contextHasMore }) => {
hasMore = contextHasMore;
},
);
});

await waitFor(() => {
Expand All @@ -395,9 +402,12 @@ describe('Channel', () => {
queryChannelWithNewMessages(Array.from({ length: 25 }, generateMessage), channel),
]);
let hasMore;
await renderComponent({ channel, chatClient }, ({ hasMore: contextHasMore }) => {
hasMore = contextHasMore;
});
await renderComponent(
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ hasMore: contextHasMore }) => {
hasMore = contextHasMore;
},
);

await waitFor(() => {
expect(hasMore).toBe(true);
Expand Down Expand Up @@ -761,15 +771,21 @@ describe('Channel', () => {
const limit = 10;
it("should initiate the hasMore flag with the current message set's pagination hasPrev value", async () => {
let hasMore;
await renderComponent({ channel, chatClient }, ({ hasMore: hasMoreCtx }) => {
hasMore = hasMoreCtx;
});
await renderComponent(
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ hasMore: hasMoreCtx }) => {
hasMore = hasMoreCtx;
},
);
expect(hasMore).toBe(true);

channel.state.messageSets[0].pagination.hasPrev = false;
await renderComponent({ channel, chatClient }, ({ hasMore: hasMoreCtx }) => {
hasMore = hasMoreCtx;
});
await renderComponent(
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ hasMore: hasMoreCtx }) => {
hasMore = hasMoreCtx;
},
);
expect(hasMore).toBe(false);
});
it('should be able to load more messages', async () => {
Expand All @@ -779,7 +795,7 @@ describe('Channel', () => {
const newMessages = [generateMessage()];

await renderComponent(
{ channel, chatClient },
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ loadMore, messages: contextMessages }) => {
if (!contextMessages.find((message) => message.id === newMessages[0].id)) {
// Our new message is not yet passed as part of channel context. Call loadMore and mock API response to include it.
Expand Down Expand Up @@ -837,7 +853,7 @@ describe('Channel', () => {
.fill(null)
.map(() => generateMessage());
await renderComponent(
{ channel, chatClient },
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ hasMore, loadMore, messages: contextMessages }) => {
if (!contextMessages.some((message) => message.id === newMessages[0].id)) {
// Our new messages are not yet passed as part of channel context. Call loadMore and mock API response to include it.
Expand All @@ -859,12 +875,15 @@ describe('Channel', () => {
const queryPromise = new Promise(() => {});
let isLoadingMore = false;

await renderComponent({ channel, chatClient }, ({ loadingMore, loadMore }) => {
// return a promise that hasn't resolved yet, so loadMore will be stuck in the 'await' part of the function
jest.spyOn(channel, 'query').mockImplementationOnce(() => queryPromise);
loadMore();
isLoadingMore = loadingMore;
});
await renderComponent(
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ loadingMore, loadMore }) => {
// return a promise that hasn't resolved yet, so loadMore will be stuck in the 'await' part of the function
jest.spyOn(channel, 'query').mockImplementationOnce(() => queryPromise);
loadMore();
isLoadingMore = loadingMore;
},
);
await waitFor(() => expect(isLoadingMore).toBe(true));
});

Expand Down Expand Up @@ -912,7 +931,7 @@ describe('Channel', () => {
let queryNextPageSpy;
let contextMessageCount;
await renderComponent(
{ channel, chatClient },
{ channel, channelQueryOptions: { messages: { limit: 25 } }, chatClient },
({ loadMore, messages: contextMessages }) => {
queryNextPageSpy = jest.spyOn(channel, 'query');
contextMessageCount = contextMessages.length;
Expand All @@ -935,9 +954,9 @@ describe('Channel', () => {
expect(chatClient.axiosInstance.post.mock.calls[1][1]).toMatchObject(
expect.objectContaining({
data: {},
messages: { id_lt: firstPageMessages[0].id, limit: 100 },
messages: { id_lt: firstPageMessages[0].id, limit: 25 },
state: true,
watchers: { limit: 100 },
watchers: { limit: 25 },
}),
);
expect(contextMessageCount).toBe(
Expand Down
7 changes: 2 additions & 5 deletions src/components/ChannelList/ChannelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
useComponentContext,
} from '../../context';
import { NullComponent } from '../UtilityComponents';
import { MAX_QUERY_CHANNELS_LIMIT, moveChannelUpwards } from './utils';
import { moveChannelUpwards } from './utils';
import type { CustomQueryChannelsFn } from './hooks/usePaginatedChannels';
import type { ChannelListMessengerProps } from './ChannelListMessenger';
import type { ChannelPreviewUIComponentProps } from '../ChannelPreview/ChannelPreview';
Expand Down Expand Up @@ -234,10 +234,7 @@ const UnMemoizedChannelList = (props: ChannelListProps) => {
channels: Array<Channel>,
setChannels: React.Dispatch<React.SetStateAction<Array<Channel>>>,
) => {
if (
!channels.length ||
channels.length > (options?.limit || MAX_QUERY_CHANNELS_LIMIT)
) {
if (!channels.length) {
return;
}

Expand Down
50 changes: 45 additions & 5 deletions src/components/ChannelList/__tests__/ChannelList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ describe('ChannelList', () => {
const props = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -291,6 +292,7 @@ describe('ChannelList', () => {
channelRenderFilterFn: customFilterFunction,
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -634,7 +636,13 @@ describe('ChannelList', () => {
customActiveChannel={testChannel2.channel.id}
filters={{}}
List={ChannelListComponent}
options={{ presence: true, state: true, watch: true }}
options={{
limit: 25,
message_limit: 25,
presence: true,
state: true,
watch: true,
}}
Preview={ChannelPreviewComponent}
setActiveChannel={setActiveChannel}
setActiveChannelOnMount
Expand Down Expand Up @@ -931,6 +939,7 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
renderChannels,
};
Expand All @@ -957,6 +966,7 @@ describe('ChannelList', () => {
const props = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};
const sendNewMessageOnChannel3 = async () => {
Expand Down Expand Up @@ -1065,7 +1075,13 @@ describe('ChannelList', () => {
<ChannelList
filters={{}}
List={ChannelListComponent}
options={{ presence: true, state: true, watch: true }}
options={{
limit: 25,
message_limit: 25,
presence: true,
state: true,
watch: true,
}}
Preview={ChannelPreviewComponent}
/>
</Chat>,
Expand Down Expand Up @@ -1104,7 +1120,13 @@ describe('ChannelList', () => {
filters={{}}
List={ChannelListComponent}
onMessageNew={onMessageNew}
options={{ presence: true, state: true, watch: true }}
options={{
limit: 25,
message_limit: 25,
presence: true,
state: true,
watch: true,
}}
Preview={ChannelPreviewComponent}
/>
</Chat>,
Expand All @@ -1131,7 +1153,13 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { presence: true, state: true, watch: true },
options: {
limit: 25,
message_limit: 25,
presence: true,
state: true,
watch: true,
},
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -1200,6 +1228,7 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -1263,6 +1292,7 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -1331,6 +1361,7 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -1420,6 +1451,7 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -1487,7 +1519,13 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { presence: true, state: true, watch: true },
options: {
limit: 25,
message_limit: 25,
presence: true,
state: true,
watch: true,
},
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -1555,6 +1593,7 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down Expand Up @@ -1595,6 +1634,7 @@ describe('ChannelList', () => {
const channelListProps = {
filters: {},
List: ChannelListComponent,
options: { limit: 25, message_limit: 25 },
Preview: ChannelPreviewComponent,
};

Expand Down
Loading