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] Directory Table's Sort Function #21921

Merged
merged 4 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions client/views/directory/ChannelsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { useFormatDate } from '../../hooks/useFormatDate';
import RoomTags from './RoomTags';
import { useQuery } from './hooks';

const style = { whiteSpace: 'nowrap', textOverflow: 'ellipsis', overflow: 'hidden' };
const style = {
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
};

function ChannelsTable() {
const t = useTranslation();
Expand Down Expand Up @@ -102,7 +106,7 @@ function ChannelsTable() {

const channelRoute = useRoute('channel');

const { value: data = { result: [] } } = useEndpointData('directory', query);
const { value: data = {} } = useEndpointData('directory', query);

const onClick = useMemo(
() => (name) => (e) => {
Expand Down
8 changes: 6 additions & 2 deletions client/views/directory/TeamsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { useFormatDate } from '../../hooks/useFormatDate';
import RoomTags from './RoomTags';
import { useQuery } from './hooks';

const style = { whiteSpace: 'nowrap', textOverflow: 'ellipsis', overflow: 'hidden' };
const style = {
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
};

function TeamsTable() {
const t = useTranslation();
Expand Down Expand Up @@ -79,7 +83,7 @@ function TeamsTable() {

const query = useQuery(params, sort, 'teams');

const { value: data = { result: [] } } = useEndpointData('directory', query);
const { value: data = {} } = useEndpointData('directory', query);

const onClick = useMemo(
() => (name, type) => (e) => {
Expand Down
2 changes: 1 addition & 1 deletion client/views/directory/UserTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function UserTable({ workspace = 'local' }) {
{username}
</Box>
</Box>
<MarkdownText fontScale='p1' color='hint' content={bio} />
<MarkdownText variant='inline' fontScale='p1' color='hint' content={bio} />
</Box>
</Box>
</Flex.Container>
Expand Down
60 changes: 57 additions & 3 deletions server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ const getChannels = (user, canViewAnon, searchTerm, sort, pagination) => {
const teams = Promise.await(Team.getAllPublicTeams());
const teamIds = teams.map(({ _id }) => _id);

const sortBelongsTo = Object.keys(sort).includes('belongsTo');

let sortChannels = sort;
if (sortBelongsTo) {
sortChannels = { name: 1 };
}

const result = Rooms.findByNameOrFNameAndTypeIncludingTeamRooms(searchTerm, 'c', teamIds, {
...pagination,
sort: {
featured: -1,
...sort,
...sortChannels,
},
fields: {
t: 1,
Expand Down Expand Up @@ -86,6 +93,25 @@ const getChannels = (user, canViewAnon, searchTerm, sort, pagination) => {
return room;
});

if (sortBelongsTo) {
results.sort((a, b) => {
const nameA = a.belongsTo ? a.belongsTo.toUpperCase() : '';
const nameB = b.belongsTo ? b.belongsTo.toUpperCase() : '';
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}

return 0;
});

if (sort.belongsTo === -1) {
results.reverse();
}
}

return {
total,
results,
Expand All @@ -101,13 +127,20 @@ const getTeams = (user, searchTerm, sort, pagination) => {
return;
}

const sortChannelsCount = Object.keys(sort).includes('channelsCount');

let sortTeams = sort;
if (sortChannelsCount) {
sortTeams = { name: 1 };
}

const userSubs = Subscriptions.cachedFindByUserId(user._id).fetch();
const ids = userSubs.map((sub) => sub.rid);
const result = Rooms.findContainingNameOrFNameInIdsAsTeamMain(searchTerm, ids, {
...pagination,
sort: {
featured: -1,
...sort,
...sortTeams,
},
fields: {
t: 1,
Expand All @@ -132,6 +165,23 @@ const getTeams = (user, searchTerm, sort, pagination) => {
roomsCount: getChannelsCountForTeam(room.teamId),
}));

if (sortChannelsCount) {
rooms.sort((a, b) => {
if (a.roomsCount < b.roomsCount) {
return -1;
}
if (a.roomsCount > b.roomsCount) {
return 1;
}

return 0;
});

if (sort.channelsCount === -1) {
rooms.reverse();
}
}

return {
total: result.count(), // count ignores the `skip` and `limit` options
results: rooms,
Expand Down Expand Up @@ -208,7 +258,11 @@ Meteor.methods({
return;
}

if (!['name', 'createdAt', 'usersCount', ['channels', 'teams'].includes(...type) ? ['usernames', 'lastMessage'] : [], ...type === 'users' ? ['username', 'email', 'bio'] : []].includes(sortBy)) {
const teamFilters = type === 'teams' ? ['channelsCount'] : [];
const channelFilters = ['channels', 'teams'].includes(type) ? ['usernames', 'lastMessage', 'belongsTo'] : [];
const userFilters = type === 'users' ? ['username', 'email', 'bio'] : [];

if (!['name', 'createdAt', 'usersCount', ...channelFilters, ...userFilters, ...teamFilters].includes(sortBy)) {
return;
}

Expand Down