Skip to content

Commit

Permalink
feat: add sorting to mails folders
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliano176 authored Aug 31, 2022
1 parent 75e80a9 commit 4e8d9da
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,18 @@ export const EMAIL_VALIDATION_REGEX =
export const ROOT_NAME = 'USER_ROOT';

export const DR_VALUES = ['auto', 'enabled', 'disabled'];

export const FOLDER_VIEW = {
search_folder: 'search folder',
tag: 'tag',
conversation: 'conversation',
message: 'message',
contact: 'contact',
document: 'document',
appointment: 'appointment',
virtual_conversation: 'virtual conversation',
remote_folder: 'remote folder',
wiki: 'wiki',
task: 'task',
chat: 'chat'
};
10 changes: 8 additions & 2 deletions src/store/folder/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AccordionFolder,
FolderView
} from '../../../types';
import { FOLDER_VIEW } from '../../constants';
import { useFolderStore } from './store';
import { filterNodes, folderViewFilter, isRoot, mapNodes, sortFolders } from './utils';

Expand Down Expand Up @@ -50,9 +51,14 @@ export const getSearchFolders = (): Searches => useFolderStore.getState().search

export const useFoldersByView = (view: FolderView): Array<Folder> => {
const roots = useRoots();
const sortFunction = useMemo(
() => (view === FOLDER_VIEW.message ? sortFolders : undefined),
[view]
);
return useMemo(
() => (roots ? filterNodes<Folder>(Object.values(roots), folderViewFilter(view)) : []),
[roots, view]
() =>
roots ? filterNodes<Folder>(Object.values(roots), folderViewFilter(view), sortFunction) : [],
[roots, sortFunction, view]
);
};

Expand Down
9 changes: 6 additions & 3 deletions src/store/folder/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ export const folderViewFilter =
export const filterNodes = <T>(
children: TreeNode<T>[],
f: (deep?: boolean) => (i: TreeNode<T>) => boolean,
sortFunction?: (i: TreeNode<T>) => number | string,
deep?: boolean
): TreeNode<T>[] =>
children
): TreeNode<T>[] => {
const childrenSorted = sortFunction ? sortBy(children, sortFunction) : children;
return childrenSorted
.filter(f(deep))
.map((i) => ({ ...i, children: filterNodes<TreeNode<T>>(i.children, f, true) }));
.map((i) => ({ ...i, children: filterNodes<TreeNode<T>>(i.children, f, sortFunction, true) }));
};

type MapNodesOptions<T, U> = {
mapFunction: (i: TreeNode<T>) => U;
Expand Down

0 comments on commit 4e8d9da

Please sign in to comment.