Skip to content

Commit

Permalink
Simplify how GlobalThreads fetches threads (#26984)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmhealey committed May 17, 2024
1 parent ada9c64 commit 3e734ee
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {useIntl} from 'react-intl';
import {useSelector, useDispatch, shallowEqual} from 'react-redux';
import {Link, useRouteMatch} from 'react-router-dom';

import {getThreadCounts, getThreads} from 'mattermost-redux/actions/threads';
import {getThreadCounts, getThreadsForCurrentTeam} from 'mattermost-redux/actions/threads';
import {getPost} from 'mattermost-redux/selectors/entities/posts';
import {
getThreadOrderInCurrentTeam,
Expand All @@ -30,7 +30,7 @@ import LocalStorageStore from 'stores/local_storage_store';
import LoadingScreen from 'components/loading_screen';
import NoResultsIndicator from 'components/no_results_indicator';

import {Constants, PreviousViewedTypes} from 'utils/constants';
import {PreviousViewedTypes} from 'utils/constants';

import type {GlobalState} from 'types/store/index';
import {LhsItemType, LhsPage} from 'types/store/lhs';
Expand Down Expand Up @@ -93,19 +93,6 @@ const GlobalThreads = () => {

const [isLoading, setLoading] = useState(isEmptyList);

const fetchThreads = useCallback(async (unread): Promise<{data: any}> => {
await dispatch(getThreads(
currentUserId,
currentTeamId,
{
unread,
perPage: Constants.THREADS_PAGE_SIZE,
},
));

return {data: true};
}, [currentUserId, currentTeamId]);

const isOnlySelectedThreadInList = (list: string[]) => {
return selectedThreadId && list.length === 1 && list[0] === selectedThreadId;
};
Expand All @@ -118,17 +105,17 @@ const GlobalThreads = () => {

// this is needed to jump start threads fetching
if (shouldLoadThreads) {
promises.push(fetchThreads(false));
promises.push(dispatch(getThreadsForCurrentTeam({unread: false})));
}

if (filter === ThreadFilter.unread && shouldLoadUnreadThreads) {
promises.push(fetchThreads(true));
promises.push(dispatch(getThreadsForCurrentTeam({unread: false})));
}

Promise.all(promises).then(() => {
setLoading(false);
});
}, [fetchThreads, filter, threadIds, unreadThreadIds]);
}, [filter, threadIds, unreadThreadIds]);

useEffect(() => {
if (!selectedThread && !selectedPost && !isLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {shallow} from 'enzyme';
import React from 'react';
import type {ComponentProps} from 'react';

import {getThreads} from 'mattermost-redux/actions/threads';
import {getThreadsForCurrentTeam} from 'mattermost-redux/actions/threads';

import {openModal} from 'actions/views/modals';

import Header from 'components/widgets/header';

import {Constants, WindowSizes} from 'utils/constants';
import {WindowSizes} from 'utils/constants';
import {TestHelper} from 'utils/test_helper';

import ThreadList, {ThreadFilter} from './thread_list';
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('components/threading/global_threads/thread_list', () => {
const loadMoreItems = await handleLoadMoreItems(2, 3);

expect(loadMoreItems).toEqual({data: true});
expect(getThreads).toHaveBeenCalledWith('uid', 'tid', {unread: false, perPage: Constants.THREADS_PAGE_SIZE, before: '2'});
expect(getThreadsForCurrentTeam).toHaveBeenCalledWith({unread: false, before: '2'});
expect(setState.mock.calls).toEqual([[true], [false], [true]]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {useDispatch, useSelector} from 'react-redux';
import {PlaylistCheckIcon} from '@mattermost/compass-icons/components';
import type {UserThread} from '@mattermost/types/threads';

import {getThreads, markAllThreadsInTeamRead} from 'mattermost-redux/actions/threads';
import {getThreadsForCurrentTeam, markAllThreadsInTeamRead} from 'mattermost-redux/actions/threads';
import {getInt} from 'mattermost-redux/selectors/entities/preferences';
import {getThreadCountsInCurrentTeam} from 'mattermost-redux/selectors/entities/threads';

Expand Down Expand Up @@ -149,7 +149,7 @@ const ThreadList = ({
before = data[startIndex - 2];
}

await dispatch(getThreads(currentUserId, currentTeamId, {unread, perPage: Constants.THREADS_PAGE_SIZE, before}));
await dispatch(getThreadsForCurrentTeam({unread, before}));

setLoading(false);
setHasLoaded(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import InfiniteLoader from 'react-window-infinite-loader';

import type {UserThread} from '@mattermost/types/threads';

import ThreadsConstants from 'mattermost-redux/constants/threads';

import {Constants} from 'utils/constants';

import Row from './virtualized_thread_list_row';
Expand Down Expand Up @@ -77,7 +79,7 @@ function VirtualizedThreadList({
itemCount={total}
loadMoreItems={loadMoreItems}
isItemLoaded={isItemLoaded}
minimumBatchSize={Constants.THREADS_PAGE_SIZE}
minimumBatchSize={ThreadsConstants.THREADS_PAGE_SIZE}
>
{({onItemsRendered, ref}) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import nock from 'nock';

import {
getThread as fetchThread,
getThreads as fetchThreads,
getThreadsForCurrentTeam,
getThreadCounts as fetchThreadCounts,
} from 'mattermost-redux/actions/threads';
import {Client4} from 'mattermost-redux/client';
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('Actions.Threads', () => {
expect(thread).toEqual({...mockThread, is_following: true});
});

test('getThreads', async () => {
test('getThreadsForCurrentTeam', async () => {
const [mockThread0, {threadId: threadId0}] = mockUserThread({uniq: 0});
const [mockThread1, {threadId: threadId1}] = mockUserThread({uniq: 1});
const [mockThread2, {threadId: threadId2}] = mockUserThread({uniq: 2});
Expand All @@ -139,7 +139,7 @@ describe('Actions.Threads', () => {
get((uri) => uri.includes(`/users/${currentUserId}/teams/${currentTeamId}/threads`)).
reply(200, mockResponse);

const {error, data} = await store.dispatch(fetchThreads(currentUserId, currentTeamId));
const {error, data} = await store.dispatch(getThreadsForCurrentTeam());
const state = store.getState();
const threads = getThreadsInCurrentTeam(state);
expect(error).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ export function fetchThreads(userId: string, teamId: string, {before = '', after
};
}

export function getThreads(userId: string, teamId: string, {before = '', after = '', perPage = ThreadConstants.THREADS_CHUNK_SIZE, unread = false, extended = true} = {}): ActionFuncAsync<UserThreadList> {
return async (dispatch) => {
const response = await dispatch(fetchThreads(userId, teamId, {before, after, perPage, unread, totalsOnly: false, threadsOnly: true, extended}));
export function getThreadsForCurrentTeam({before = '', after = '', unread = false} = {}): ActionFuncAsync<UserThreadList> {
return async (dispatch, getState) => {
const userId = getCurrentUserId(getState());
const teamId = getCurrentTeamId(getState());

const response = await dispatch(fetchThreads(userId, teamId, {
before,
after,
perPage: ThreadConstants.THREADS_PAGE_SIZE,
unread,
totalsOnly: false,
threadsOnly: true,
extended: true,
}));

if (response.error) {
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

export default {
THREADS_CHUNK_SIZE: 20,
THREADS_PAGE_SIZE: 25,
};
1 change: 0 additions & 1 deletion webapp/channels/src/utils/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,6 @@ export const Constants = {
MAX_ATTACHMENT_FOOTER_LENGTH: 300,
ACCEPT_STATIC_IMAGE: '.jpeg,.jpg,.png,.bmp',
ACCEPT_EMOJI_IMAGE: '.jpeg,.jpg,.png,.gif',
THREADS_PAGE_SIZE: 25,
THREADS_LOADING_INDICATOR_ITEM_ID: 'threads_loading_indicator_item_id',
THREADS_NO_RESULTS_ITEM_ID: 'threads_no_results_item_id',
TRIAL_MODAL_AUTO_SHOWN: 'trial_modal_auto_shown',
Expand Down

0 comments on commit 3e734ee

Please sign in to comment.