Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / useFolderColor.ts
blobc8156463671a7a6fe12344b51a20a72562770446
1 import { useFolders } from '@proton/mail';
2 import { useMailSettings } from '@proton/mail/mailSettings/hooks';
3 import { toMap } from '@proton/shared/lib/helpers/object';
4 import type { Folder } from '@proton/shared/lib/interfaces/Folder';
6 const useFolderColor = (folder: Folder) => {
7     const [folders] = useFolders();
8     const [mailSettings] = useMailSettings();
10     if (!mailSettings?.EnableFolderColor) {
11         return undefined;
12     }
14     if (!mailSettings?.InheritParentFolderColor) {
15         return folder.Color;
16     }
18     const folderMap = toMap(folders);
20     const getParentFolderColor = ({ ParentID, Color }: Folder): string | undefined => {
21         // ParentID is undefined for root folder
22         if (!ParentID) {
23             return Color;
24         }
26         const folder = folderMap[ParentID];
28         if (folder) {
29             return getParentFolderColor(folder);
30         }
32         return undefined;
33     };
35     return getParentFolderColor(folder);
38 export default useFolderColor;