(cherry picked from commit 1a1c8e6c08a6db5fcd2b5d17e65fa1f943d2e746) Closes #4912 Closes #4921pull/4922/head
parent
a9dd947eed
commit
7f73a2e23a
@ -1,11 +1,12 @@
|
||||
import { createSelector } from 'reselect';
|
||||
import { RootFolderAppState } from 'App/State/SettingsAppState';
|
||||
import createSortedSectionSelector from 'Store/Selectors/createSortedSectionSelector';
|
||||
import sortByName from 'Utilities/Array/sortByName';
|
||||
import RootFolder from 'typings/RootFolder';
|
||||
import sortByProp from 'Utilities/Array/sortByProp';
|
||||
|
||||
export default function createRootFoldersSelector() {
|
||||
return createSelector(
|
||||
createSortedSectionSelector('settings.rootFolders', sortByName),
|
||||
createSortedSectionSelector<RootFolder>('rootFolders', sortByProp('name')),
|
||||
(rootFolders: RootFolderAppState) => rootFolders
|
||||
);
|
||||
}
|
||||
|
@ -1,14 +1,18 @@
|
||||
import { createSelector } from 'reselect';
|
||||
import getSectionState from 'Utilities/State/getSectionState';
|
||||
|
||||
function createSortedSectionSelector(section, comparer) {
|
||||
function createSortedSectionSelector<T>(
|
||||
section: string,
|
||||
comparer: (a: T, b: T) => number
|
||||
) {
|
||||
return createSelector(
|
||||
(state) => state,
|
||||
(state) => {
|
||||
const sectionState = getSectionState(state, section, true);
|
||||
|
||||
return {
|
||||
...sectionState,
|
||||
items: [...sectionState.items].sort(comparer)
|
||||
items: [...sectionState.items].sort(comparer),
|
||||
};
|
||||
}
|
||||
);
|
@ -1,5 +0,0 @@
|
||||
function sortByName(a, b) {
|
||||
return a.name.localeCompare(b.name);
|
||||
}
|
||||
|
||||
export default sortByName;
|
@ -0,0 +1,13 @@
|
||||
import { StringKey } from 'typings/Helpers/KeysMatching';
|
||||
|
||||
export function sortByProp<
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
T extends Record<K, string>,
|
||||
K extends StringKey<T>
|
||||
>(sortKey: K) {
|
||||
return (a: T, b: T) => {
|
||||
return a[sortKey].localeCompare(b[sortKey], undefined, { numeric: true });
|
||||
};
|
||||
}
|
||||
|
||||
export default sortByProp;
|
@ -0,0 +1,7 @@
|
||||
type KeysMatching<T, V> = {
|
||||
[K in keyof T]-?: T[K] extends V ? K : never;
|
||||
}[keyof T];
|
||||
|
||||
export type StringKey<T> = KeysMatching<T, string>;
|
||||
|
||||
export default KeysMatching;
|
Loading…
Reference in new issue