Sort servers list

This commit is contained in:
Elias Nahum
2022-02-03 11:44:49 -03:00
parent fb50dd3550
commit 16524314aa
2 changed files with 17 additions and 3 deletions

View File

@@ -23,7 +23,7 @@ export const subscribeAllServers = (observer: (servers: ServersModel[]) => void)
const db = DatabaseManager.appDatabase?.database;
return db?.
get(SERVERS).
query(Q.sortBy('display_name', Q.asc)).
query().
observeWithColumns(['last_active_at']).
subscribe(observer);
};

View File

@@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {useIntl} from 'react-intl';
import {IntlShape, useIntl} from 'react-intl';
import {StyleSheet} from 'react-native';
import ServerIcon from '@components/server_icon';
@@ -34,6 +34,20 @@ const styles = StyleSheet.create({
},
});
const sortServers = (servers: ServersModel[], intl: IntlShape) => {
function serverName(s: ServersModel) {
if (s.displayName === s.url) {
return intl.formatMessage({id: 'servers.default', defaultMessage: 'Default Server'});
}
return s.displayName;
}
return servers.sort((a, b) => {
return serverName(a).localeCompare(serverName(b));
});
};
export default function Servers() {
const intl = useIntl();
const [total, setTotal] = useState<UnreadMessages>({mentions: 0, unread: false});
@@ -70,7 +84,7 @@ export default function Servers() {
};
const serversObserver = async (servers: ServersModel[]) => {
registeredServers.current = servers;
registeredServers.current = sortServers(servers, intl);
// unsubscribe mentions from servers that were removed
const allUrls = servers.map((s) => s.url);