Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Use consistent Display Name sorting code throughout the webapp #5159 (#…
Browse files Browse the repository at this point in the history
…5213)

* Use consistent Display Name sorting code throughout the webapp #5159

* fixed broken sorting of teams and channels
  • Loading branch information
saturninoabril authored and enahum committed Jan 31, 2017
1 parent a09dd56 commit 749c9ee
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 90 deletions.
2 changes: 1 addition & 1 deletion actions/global_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ export function redirectUserToDefaultTeam() {
}

if (myTeams.length > 0) {
myTeams = myTeams.sort((a, b) => a.display_name.localeCompare(b.display_name));
myTeams = myTeams.sort(Utils.sortTeamsByDisplayName);
teamId = myTeams[0].id;
}
}
Expand Down
5 changes: 2 additions & 3 deletions components/admin_console/admin_navbar_dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ReactDOM from 'react-dom';

import TeamStore from 'stores/team_store.jsx';
import Constants from 'utils/constants.jsx';
import {sortTeamsByDisplayName} from 'utils/utils.jsx';
import * as GlobalActions from 'actions/global_actions.jsx';

import {FormattedMessage} from 'react-intl';
Expand Down Expand Up @@ -64,9 +65,7 @@ export default class AdminNavbarDropdown extends React.Component {
}

// Sort teams alphabetically with display_name
teamsArray.sort((teamA, teamB) =>
teamA.display_name.localeCompare(teamB.display_name)
);
teamsArray = teamsArray.sort(sortTeamsByDisplayName);

for (const team of teamsArray) {
teams.push(
Expand Down
17 changes: 2 additions & 15 deletions components/admin_console/admin_sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,6 @@ export default class AdminSidebar extends React.Component {
document.title = Utils.localizeMessage('sidebar_right_menu.console', 'System Console') + ' - ' + currentSiteName;
}

sortTeams(a, b) {
const teamA = a.display_name.toLowerCase();
const teamB = b.display_name.toLowerCase();

if (teamA < teamB) {
return -1;
}
if (teamA > teamB) {
return 1;
}
return 0;
}

renderAddTeamButton() {
const addTeamTooltip = (
<Tooltip id='add-team-tooltip'>
Expand Down Expand Up @@ -159,15 +146,15 @@ export default class AdminSidebar extends React.Component {

renderTeams() {
const teams = [];
const teamsArray = [];
let teamsArray = [];

Reflect.ownKeys(this.state.selectedTeams).forEach((key) => {
if (this.state.teams[key]) {
teamsArray.push(this.state.teams[key]);
}
});

teamsArray.sort(this.sortTeams);
teamsArray = teamsArray.sort(Utils.sortTeamsByDisplayName);

for (let i = 0; i < teamsArray.length; i++) {
const team = teamsArray[i];
Expand Down
14 changes: 4 additions & 10 deletions components/admin_console/select_team_modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

import ReactDOM from 'react-dom';
import {FormattedMessage} from 'react-intl';

import {Modal} from 'react-bootstrap';

import React from 'react';

import {sortTeamsByDisplayName} from 'utils/utils.jsx';

export default class SelectTeamModal extends React.Component {
constructor(props) {
super(props);

this.doSubmit = this.doSubmit.bind(this);
this.doCancel = this.doCancel.bind(this);
this.compare = this.compare.bind(this);
}

doSubmit(e) {
Expand All @@ -25,24 +24,19 @@ export default class SelectTeamModal extends React.Component {
this.props.onModalDismissed();
}

compare(a, b) {
return a.display_name.localeCompare(b.display_name);
}

render() {
if (this.props.teams == null) {
return <div/>;
}

const options = [];
const teamsArray = [];
let teamsArray = [];

Reflect.ownKeys(this.props.teams).forEach((key) => {
teamsArray.push(this.props.teams[key]);
});

teamsArray.sort(this.compare);

teamsArray = teamsArray.sort(sortTeamsByDisplayName);
for (let i = 0; i < teamsArray.length; i++) {
const team = teamsArray[i];
options.push(
Expand Down
10 changes: 3 additions & 7 deletions components/channel_select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React from 'react';
import Constants from 'utils/constants.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import * as Utils from 'utils/utils.jsx';
import {sortChannelsByDisplayName} from 'utils/channel_utils.jsx';
import * as AsyncClient from 'utils/async_client.jsx';

export default class ChannelSelect extends React.Component {
Expand All @@ -32,12 +33,11 @@ export default class ChannelSelect extends React.Component {

this.handleChannelChange = this.handleChannelChange.bind(this);
this.filterChannels = this.filterChannels.bind(this);
this.compareByDisplayName = this.compareByDisplayName.bind(this);

AsyncClient.getMoreChannels(true);

this.state = {
channels: ChannelStore.getAll().filter(this.filterChannels).sort(this.compareByDisplayName)
channels: ChannelStore.getAll().filter(this.filterChannels).sort(sortChannelsByDisplayName)
};
}

Expand All @@ -52,7 +52,7 @@ export default class ChannelSelect extends React.Component {
handleChannelChange() {
this.setState({
channels: ChannelStore.getAll().concat(ChannelStore.getMoreAll()).
filter(this.filterChannels).sort(this.compareByDisplayName)
filter(this.filterChannels).sort(sortChannelsByDisplayName)
});
}

Expand All @@ -64,10 +64,6 @@ export default class ChannelSelect extends React.Component {
return false;
}

compareByDisplayName(channelA, channelB) {
return channelA.display_name.localeCompare(channelB.display_name);
}

render() {
const options = [
<option
Expand Down
9 changes: 5 additions & 4 deletions components/suggestion/search_channel_provider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ChannelStore from 'stores/channel_store.jsx';

import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import {Constants, ActionTypes} from 'utils/constants.jsx';
import {sortChannelsByDisplayName} from 'utils/channel_utils.jsx';

import React from 'react';

Expand Down Expand Up @@ -51,7 +52,7 @@ export default class SearchChannelProvider extends Provider {
const publicChannels = data;

const localChannels = ChannelStore.getAll();
const privateChannels = [];
let privateChannels = [];

for (const id of Object.keys(localChannels)) {
const channel = localChannels[id];
Expand All @@ -60,15 +61,15 @@ export default class SearchChannelProvider extends Provider {
}
}

const filteredPublicChannels = [];
let filteredPublicChannels = [];
publicChannels.forEach((item) => {
if (item.name.startsWith(channelPrefix)) {
filteredPublicChannels.push(item);
}
});

privateChannels.sort((a, b) => a.name.localeCompare(b.name));
filteredPublicChannels.sort((a, b) => a.name.localeCompare(b.name));
privateChannels = privateChannels.sort(sortChannelsByDisplayName);
filteredPublicChannels = filteredPublicChannels.sort(sortChannelsByDisplayName);

const channels = filteredPublicChannels.concat(privateChannels);
const channelNames = channels.map((channel) => channel.name);
Expand Down
17 changes: 4 additions & 13 deletions components/suggestion/switch_channel_provider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Client from 'client/web_client.jsx';
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import {Constants, ActionTypes} from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';
import {sortChannelsByDisplayName} from 'utils/channel_utils.jsx';

import React from 'react';

Expand Down Expand Up @@ -105,19 +106,9 @@ export default class SwitchChannelProvider extends Provider {
userMap[user.id] = user;
}

channels.sort((a, b) => {
if (a.display_name === b.display_name) {
if (a.type !== Constants.DM_CHANNEL && b.type === Constants.DM_CHANNEL) {
return -1;
} else if (a.type === Constants.DM_CHANNEL && b.type !== Constants.DM_CHANNEL) {
return 1;
}
return a.name.localeCompare(b.name);
}
return a.display_name.localeCompare(b.display_name);
});

const channelNames = channels.map((channel) => channel.name);
const channelNames = channels.
sort(sortChannelsByDisplayName).
map((channel) => channel.name);

AppDispatcher.handleServerAction({
type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
Expand Down
30 changes: 15 additions & 15 deletions components/team_sidebar/team_sidebar_controller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ export default class TeamSidebar extends React.Component {
}

const teams = myTeams.
sort((a, b) => a.display_name.localeCompare(b.display_name)).
map((team) => {
return (
<TeamButton
key={'switch_team_' + team.name}
url={`/${team.name}`}
tip={team.display_name}
active={team.id === this.state.currentTeamId}
isMobile={this.state.isMobile}
displayName={team.display_name}
unread={team.unread}
mentions={team.mentions}
/>
);
});
sort(Utils.sortTeamsByDisplayName).
map((team) => {
return (
<TeamButton
key={'switch_team_' + team.name}
url={`/${team.name}`}
tip={team.display_name}
active={team.id === this.state.currentTeamId}
isMobile={this.state.isMobile}
displayName={team.display_name}
unread={team.unread}
mentions={team.mentions}
/>
);
});

if (moreTeams) {
teams.push(
Expand Down
8 changes: 4 additions & 4 deletions stores/channel_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ class ChannelStoreClass extends EventEmitter {
}

if (!Utils) {
Utils = require('utils/utils.jsx'); //eslint-disable-line global-require
Utils = require('utils/channel_utils.jsx'); //eslint-disable-line global-require
}

channels.sort(Utils.sortByDisplayName);
channels = channels.sort(Utils.sortChannelsByDisplayName);
this.storeChannels(channels);
}

Expand Down Expand Up @@ -286,10 +286,10 @@ class ChannelStoreClass extends EventEmitter {
const teamChannels = this.moreChannels[teamId] || {};

if (!Utils) {
Utils = require('utils/utils.jsx'); //eslint-disable-line global-require
Utils = require('utils/channel_utils.jsx'); //eslint-disable-line global-require
}

return Object.keys(teamChannels).map((cid) => teamChannels[cid]).sort(Utils.sortByDisplayName);
return Object.keys(teamChannels).map((cid) => teamChannels[cid]).sort(Utils.sortChannelsByDisplayName);
}

storeStats(stats) {
Expand Down
7 changes: 5 additions & 2 deletions utils/channel_utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import LocalizationStore from 'stores/localization_store.jsx';
export function buildDisplayableChannelList(persistentChannels) {
const missingDMChannels = createMissingDirectChannels(persistentChannels);

const channels = persistentChannels.concat(missingDMChannels).map(completeDirectChannelInfo).filter(isNotDeletedChannel);
channels.sort(sortChannelsByDisplayName);
const channels = persistentChannels.
concat(missingDMChannels).
map(completeDirectChannelInfo).
filter(isNotDeletedChannel).
sort(sortChannelsByDisplayName);

const favoriteChannels = channels.filter(isFavoriteChannel);
const notFavoriteChannels = channels.filter(not(isFavoriteChannel));
Expand Down
22 changes: 6 additions & 16 deletions utils/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1093,25 +1093,15 @@ export function windowHeight() {
return $(window).height();
}

// Use when sorting multiple channels or teams by their `display_name` field
export function sortByDisplayName(a, b) {
let aDisplayName = '';
let bDisplayName = '';
// Use when sorting multiple teams by their `display_name` field
export function sortTeamsByDisplayName(a, b) {
const locale = LocalizationStore.getLocale();

if (a && a.display_name) {
aDisplayName = a.display_name.toLowerCase();
}
if (b && b.display_name) {
bDisplayName = b.display_name.toLowerCase();
if (a.display_name !== b.display_name) {
return a.display_name.localeCompare(b.display_name, locale, {numeric: true});
}

if (aDisplayName < bDisplayName) {
return -1;
}
if (aDisplayName > bDisplayName) {
return 1;
}
return 0;
return a.name.localeCompare(b.name, locale, {numeric: true});
}

export function getChannelTerm(channelType) {
Expand Down

0 comments on commit 749c9ee

Please sign in to comment.