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: prevent flashing EmptyStateIndicator in ChannelList before the first channels page is loaded #2150

Merged
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
5 changes: 4 additions & 1 deletion src/components/ChannelList/ChannelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ const UnMemoizedChannelList = <
<List
error={channelsQueryState.error}
loadedChannels={sendChannelsToList ? loadedChannels : undefined}
loading={channelsQueryState.queryInProgress === 'reload'}
loading={
!!channelsQueryState.queryInProgress &&
['reload', 'uninitialized'].includes(channelsQueryState.queryInProgress)
}
LoadingErrorIndicator={LoadingErrorIndicator}
LoadingIndicator={LoadingIndicator}
setChannels={setChannels}
Expand Down
39 changes: 38 additions & 1 deletion src/components/ChannelList/__tests__/ChannelList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
ChannelPreviewMessenger,
} from '../../ChannelPreview';

import { ChatContext } from '../../../context/ChatContext';
import { ChatContext, useChatContext } from '../../../context/ChatContext';
import { ChannelListMessenger } from '../ChannelListMessenger';

expect.extend(toHaveNoViolations);

Expand Down Expand Up @@ -256,6 +257,42 @@ describe('ChannelList', () => {
expect(results).toHaveNoViolations();
});

it('should render loading indicator before the first channel list load and on reload', async () => {
const channelsQueryStatesHistory = [];
const channelListMessengerLoadingHistory = [];
useMockedApis(chatClient, [queryChannelsApi([testChannel1])]);

const QueryStateInterceptor = ({ children }) => {
const { channelsQueryState } = useChatContext();
channelsQueryStatesHistory.push(channelsQueryState.queryInProgress);
return children;
};

const ChannelListMessengerPropsInterceptor = (props) => {
channelListMessengerLoadingHistory.push(props.loading);
return <ChannelListMessenger {...props} />;
};

await act(() => {
render(
<Chat client={chatClient}>
<QueryStateInterceptor>
<ChannelList List={ChannelListMessengerPropsInterceptor} />
</QueryStateInterceptor>
</Chat>,
);
});

expect(channelsQueryStatesHistory).toHaveLength(3);
expect(channelListMessengerLoadingHistory).toHaveLength(3);
expect(channelsQueryStatesHistory[0]).toBe('uninitialized');
expect(channelListMessengerLoadingHistory[0]).toBe(true);
expect(channelsQueryStatesHistory[1]).toBe('reload');
expect(channelListMessengerLoadingHistory[1]).toBe(true);
expect(channelsQueryStatesHistory[2]).toBeNull();
expect(channelListMessengerLoadingHistory[2]).toBe(false);
});

it('ChannelPreview UI components should render `Avatar` when the custom prop is provided', async () => {
useMockedApis(chatClient, [queryChannelsApi([testChannel1])]);

Expand Down
12 changes: 8 additions & 4 deletions src/components/Chat/hooks/useChannelsQueryState.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Dispatch, SetStateAction, useState } from 'react';
import type { APIErrorResponse, ErrorFromResponse } from 'stream-chat';

type ChannelQueryType = 'reload' | 'load-more';
type ChannelQueryState =
| 'uninitialized' // the initial state before the first channels query is trigerred
| 'reload' // the initial channels query (loading the first page) is in progress
| 'load-more' // loading the next page of channels
| null; // at least one channels page has been loaded and there is no query in progress at the moment

export interface ChannelsQueryState {
error: ErrorFromResponse<APIErrorResponse> | null;
queryInProgress: ChannelQueryType | null;
queryInProgress: ChannelQueryState | null;
setError: Dispatch<SetStateAction<ErrorFromResponse<APIErrorResponse> | null>>;
setQueryInProgress: Dispatch<SetStateAction<ChannelQueryType | null>>;
setQueryInProgress: Dispatch<SetStateAction<ChannelQueryState | null>>;
}

export const useChannelsQueryState = (): ChannelsQueryState => {
const [error, setError] = useState<ErrorFromResponse<APIErrorResponse> | null>(null);
const [queryInProgress, setQueryInProgress] = useState<ChannelQueryType | null>(null);
const [queryInProgress, setQueryInProgress] = useState<ChannelQueryState>('uninitialized');

return {
error,
Expand Down
Loading