1 import type { Label, UserModel } from '@proton/shared/lib/interfaces';
2 import orderBy from '@proton/utils/orderBy';
3 import range from '@proton/utils/range';
5 import { FREE_USER_FOLDERS_LIMIT, FREE_USER_LABELS_LIMIT, ROOT_FOLDER } from '../constants';
6 import type { Folder, FolderWithSubFolders } from '../interfaces/Folder';
8 export const order = (folders: Folder[] = []) => orderBy(folders, 'Order');
10 export const getParents = (folders: Folder[] = []) => {
11 return folders.reduce<{ [key: string]: Folder[] }>((acc, item) => {
12 const { ParentID = ROOT_FOLDER } = item;
13 acc[ParentID] = acc[ParentID] || [];
14 acc[ParentID].push(item);
19 export const buildTreeview = (folders: FolderWithSubFolders[] = []) => {
20 const parents = getParents(folders);
21 const build = (parentID: string | number = ROOT_FOLDER): FolderWithSubFolders[] => {
22 if (!Array.isArray(parents[parentID])) {
25 return order(parents[parentID]).map((item) => ({
27 subfolders: build(item.ID),
33 export const formatFolderName = (time = 0, name = '', separator = ' ') =>
38 export const hasReachedFolderLimit = (user: UserModel, userFolders: Folder[]) => {
39 const { hasPaidMail } = user;
41 return !hasPaidMail && userFolders.length >= FREE_USER_FOLDERS_LIMIT;
44 export const hasReachedLabelLimit = (user: UserModel, userLabels: Label[]) => {
45 const { hasPaidMail } = user;
47 return !hasPaidMail && userLabels.length >= FREE_USER_LABELS_LIMIT;