Files
mattermost-mobile/app/components/team_sidebar/team_list/index.ts
Mattermost Build f1a06396c6 Filter unused preferences (#7015) (#7061)
* small preferences refactor

* filter unused preferences and fix removal of preferences in the db

* Feedback review

(cherry picked from commit 64a59aad55)

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2023-01-30 21:12:26 +02:00

61 lines
2.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable max-nested-callbacks */
import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';
import {of as of$, combineLatest} from 'rxjs';
import {switchMap, map} from 'rxjs/operators';
import {Preferences} from '@constants';
import {queryPreferencesByCategoryAndName} from '@queries/servers/preference';
import {queryJoinedTeams, queryMyTeams} from '@queries/servers/team';
import TeamList from './team_list';
import type {WithDatabaseArgs} from '@typings/database/database';
const withTeams = withObservables([], ({database}: WithDatabaseArgs) => {
const myTeams = queryMyTeams(database).observe();
const teamIds = queryJoinedTeams(database).observe().pipe(
map((ts) => ts.map((t) => ({id: t.id, displayName: t.displayName}))),
);
const order = queryPreferencesByCategoryAndName(database, Preferences.CATEGORIES.TEAMS_ORDER).
observeWithColumns(['value']).pipe(
switchMap((p) => (p.length ? of$(p[0].value.split(',')) : of$([]))),
);
const myOrderedTeams = combineLatest([myTeams, order, teamIds]).pipe(
map(([ts, o, tids]) => {
let ids: string[] = o;
if (!o.length) {
ids = tids.
sort((a, b) => a.displayName.toLocaleLowerCase().localeCompare(b.displayName.toLocaleLowerCase())).
map((t) => t.id);
}
const indexes: {[x: string]: number} = {};
const originalIndexes: {[x: string]: number} = {};
ids.forEach((v, i) => {
indexes[v] = i;
});
ts.forEach((t, i) => {
originalIndexes[t.id] = i;
});
return ts.sort((a, b) => {
if ((indexes[a.id] != null) || (indexes[b.id] != null)) {
return (indexes[a.id] ?? tids.length) - (indexes[b.id] ?? tids.length);
}
return (originalIndexes[a.id] - originalIndexes[b.id]);
});
}),
);
return {
myOrderedTeams,
};
});
export default withDatabase(withTeams(TeamList));