Skip to content

Commit

Permalink
fix: folder hooks and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
zovomat committed May 11, 2022
1 parent 24ba331 commit c7b3879
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
9 changes: 4 additions & 5 deletions src/store/folder/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,18 @@ export const getSearchFolders = (): Searches => useFolderStore.getState().search

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

export const useFoldersAccordionByView = (
view: FolderView,
CustomComponent: ComponentType<{ folder: Folder }>
): Array<AccordionFolder> => {
const roots = useRoots();
const mapped = useMemo(
return useMemo(
() =>
roots
? mapNodes<Folder, AccordionFolder>(Object.values(roots), {
Expand All @@ -75,10 +74,10 @@ export const useFoldersAccordionByView = (
}),
filterFunction: folderViewFilter(view),
recursionKey: 'items',
sortFunction: sortFolders
sortFunction: sortFolders,
deep: false
})
: [],
[CustomComponent, roots, view]
);
return mapped;
};
44 changes: 21 additions & 23 deletions src/store/folder/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { sortBy } from 'lodash';
import { Folder, FolderView, LinkFolder, TreeNode } from '../../../types';
import { FOLDERS, ROOT_NAME } from '../../constants';

const hasId = (f: Folder, id: string): boolean => f.id.split(':').includes(id);
const hasId = (f: Folder | TreeNode<unknown>, id: string): boolean => f.id.split(':').includes(id);
const getOriginalId = (f: Folder): string => {
const parts = f.id.split(':');
return parts[1] ?? parts[0];
Expand All @@ -17,10 +17,7 @@ export const sortFolders = (f: Folder): string => {
if (id === FOLDERS.TRASH) {
return '~';
}
// should work fine
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return id < 17 ? id : f.name.toLowerCase();
return parseInt(id, 10) < 17 ? ` ${id}` : f.name.toLowerCase();
};

export const isTrash = (f: Folder): boolean => hasId(f, FOLDERS.TRASH);
Expand All @@ -30,40 +27,41 @@ export const isRoot = (f: Folder): boolean =>

export const folderViewFilter =
(v: FolderView) =>
(f: Folder, r?: boolean): boolean =>
f.view === v || isTrash(f) || (isRoot(f) && !r);
(deep?: boolean) =>
(f: Folder): boolean =>
f.view === v || !deep || (typeof f.view === 'undefined' && !isRoot(f));

export const filterNodes = <T>(
children: TreeNode<T>[],
f: (i: TreeNode<T>) => boolean
f: (deep?: boolean) => (i: TreeNode<T>) => boolean,
deep?: boolean
): TreeNode<T>[] =>
children.filter(f).map((i) => ({ ...i, children: filterNodes<TreeNode<T>>(i.children, f) }));
children
.filter(f(deep))
.map((i) => ({ ...i, children: filterNodes<TreeNode<T>>(i.children, f, true) }));

type MapNodesOptions<T, U> = {
mapFunction: (i: TreeNode<T>) => U;
filterFunction: (i: TreeNode<T>, inRecursion?: boolean) => boolean;
filterFunction: (deep?: boolean) => (i: TreeNode<T>) => boolean;
recursionKey: keyof U;
sortFunction: (i: TreeNode<T>) => number | string;
deep: boolean;
};
export const mapNodes = <T, U>(
children: TreeNode<T>[],
{ mapFunction, filterFunction, recursionKey, sortFunction }: MapNodesOptions<T, U>,
inRecursion?: boolean
{ mapFunction, filterFunction, recursionKey, sortFunction, deep }: MapNodesOptions<T, U>
): U[] =>
sortBy(children, sortFunction).reduce((acc, folder) => {
if (filterFunction(folder, inRecursion)) {
if (filterFunction(deep)(folder)) {
acc.push({
...mapFunction(folder),
[recursionKey]: mapNodes<TreeNode<T>, U>(
folder.children,
{
mapFunction,
filterFunction,
recursionKey,
sortFunction
},
true
)
[recursionKey]: mapNodes<TreeNode<T>, U>(folder.children, {
mapFunction,
filterFunction,
recursionKey,
sortFunction,
deep: true
})
});
}
return acc;
Expand Down

0 comments on commit c7b3879

Please sign in to comment.