MM-53609 - remove channels from archived teams during search (#24053)

* MM-53609 - remove channels from archived teams during search

* filter channels after the channels from server are fetched

* filter only channels, not DM or GM

* add unit tests; prevent blinking when searching for terms
This commit is contained in:
Pablo Andrés Vélez Vidal 2023-07-31 23:23:47 +02:00 committed by GitHub
parent aa88f8bf59
commit ef3aec40ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 5 deletions

View File

@ -43,7 +43,9 @@ jest.mock('mattermost-redux/actions/channels', () => ({
name: 'other_user',
display_name: 'other_user',
delete_at: 0,
}],
team_id: 'currentTeamId',
},
],
})),
}));
@ -119,6 +121,7 @@ describe('components/SwitchChannelProvider', () => {
id: 'currentTeamId',
display_name: 'test',
type: 'O',
delete_at: 0,
},
},
},
@ -827,6 +830,59 @@ describe('components/SwitchChannelProvider', () => {
expect(results.terms).toEqual(expectedOrder);
});
it('should filter out channels belonging to archived teams', async () => {
const modifiedState = {
...defaultState,
entities: {
...defaultState.entities,
channels: {
...defaultState.entities.channels,
myMembers: {
channel_1: {},
channel_2: {},
},
channels: {
channel_1: {
id: 'channel_1',
type: 'O',
name: 'channel_1',
display_name: 'channel 1',
delete_at: 0,
team_id: 'currentTeamId',
},
channel_2: {
id: 'channel_2',
type: 'O',
name: 'channel_2',
display_name: 'channel 2',
delete_at: 0,
team_id: 'archivedTeam',
},
},
},
},
};
getState.mockClear();
const switchProvider = new SwitchChannelProvider();
const store = mockStore(modifiedState);
getState.mockImplementation(store.getState);
const searchText = 'chan';
const resultsCallback = jest.fn();
switchProvider.startNewRequest();
await switchProvider.fetchUsersAndChannels(searchText, resultsCallback);
const channelsFromActiveTeams = [
'channel_1',
];
expect(resultsCallback).toBeCalledWith(expect.objectContaining({
terms: channelsFromActiveTeams,
}));
});
it('Should show threads as the first item in the list if search term matches', async () => {
const modifiedState = {
...defaultState,

View File

@ -31,6 +31,7 @@ import {
import {getMyPreferences, isGroupChannelManuallyVisible, isCollapsedThreadsEnabled} from 'mattermost-redux/selectors/entities/preferences';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {
getActiveTeamsList,
getCurrentTeamId,
getMyTeams,
getTeam,
@ -441,7 +442,8 @@ export default class SwitchChannelProvider extends Provider {
}
// Dispatch suggestions for local data (filter out deleted and archived channels from local store data)
const channels = getChannelsInAllTeams(getState()).concat(getDirectAndGroupChannels(getState())).filter((c) => c.delete_at === 0);
let channels = getChannelsInAllTeams(getState()).concat(getDirectAndGroupChannels(getState())).filter((c) => c.delete_at === 0);
channels = this.removeChannelsFromArchivedTeams(channels);
const users = searchProfilesMatchingWithTerm(getState(), channelPrefix, false);
const formattedData = this.formatList(channelPrefix, [ThreadsChannel, ...channels], users, true, true);
if (formattedData) {
@ -494,10 +496,13 @@ export default class SwitchChannelProvider extends Provider {
const currentUserId = getCurrentUserId(state);
// filter out deleted and archived channels from local store data
const localChannelData = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state)).filter((c) => c.delete_at === 0) || [];
let localChannelData = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state)).filter((c) => c.delete_at === 0) || [];
localChannelData = this.removeChannelsFromArchivedTeams(localChannelData);
const localUserData = searchProfilesMatchingWithTerm(state, channelPrefix, false);
const localFormattedData = this.formatList(channelPrefix, [ThreadsChannel, ...localChannelData], localUserData);
const remoteChannelData = channelsFromServer.concat(getGroupChannels(state)) || [];
let remoteChannelData = channelsFromServer.concat(getGroupChannels(state)) || [];
remoteChannelData = this.removeChannelsFromArchivedTeams(remoteChannelData);
const remoteUserData = usersFromServer.users || [];
const remoteFormattedData = this.formatList(channelPrefix, remoteChannelData, remoteUserData, false);
@ -689,9 +694,22 @@ export default class SwitchChannelProvider extends Provider {
};
}
removeChannelsFromArchivedTeams(channels: Channel[]) {
const state = getState();
const activeTeams = getActiveTeamsList(state).map((team: Team) => team.id);
const newChannels = channels.filter((channel: Channel) => {
if (!channel.team_id) {
return true;
}
return activeTeams.includes(channel.team_id);
});
return newChannels;
}
fetchAndFormatRecentlyViewedChannels(resultsCallback: ResultsCallback<WrappedChannel>) {
const state = getState();
const recentChannels = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state));
let recentChannels = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state));
recentChannels = this.removeChannelsFromArchivedTeams(recentChannels);
const wrappedRecentChannels = this.wrapChannels(recentChannels, Constants.MENTION_RECENT_CHANNELS);
const unreadChannels = getSortedAllTeamsUnreadChannels(state);
const myMembers = getMyChannelMemberships(state);