Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / selectors / export.ts
blob79e885d659676ed55c8fb4bf935fbf834fc805d9
1 import type { PassConfig } from '@proton/pass/hooks/usePassConfig';
2 import { type ExportData, ExportFormat, type ExportedVault } from '@proton/pass/lib/export/types';
3 import { deobfuscateItem } from '@proton/pass/lib/items/item.obfuscation';
4 import { isB2BAdmin } from '@proton/pass/lib/organization/helpers';
5 import { unwrapOptimisticState } from '@proton/pass/store/optimistic/utils/transformers';
6 import { selectShare } from '@proton/pass/store/selectors/shares';
7 import { selectPassPlan, selectUser } from '@proton/pass/store/selectors/user';
8 import type { State } from '@proton/pass/store/types';
9 import { BitField, type ShareType } from '@proton/pass/types';
11 import { SelectorError } from './errors';
12 import { selectOrganizationSettings } from './organization';
14 export const selectExportData =
15     ({ config, format }: { config: PassConfig; format: ExportFormat }) =>
16     (state: State): ExportData => {
17         const user = selectUser(state);
18         const plan = selectPassPlan(state);
19         const orgSettings = selectOrganizationSettings(state);
20         const b2bAdmin = user ? isB2BAdmin(user, plan) : false;
21         const orgExportDisabled = orgSettings?.ExportMode === BitField.ACTIVE;
23         /** Safe-guard export data selector against organization exporting policies */
24         const exportDisabled = !b2bAdmin && orgExportDisabled;
25         if (exportDisabled) throw new SelectorError('Export disabled for org members');
27         const itemsByShareId = unwrapOptimisticState(state.items.byShareId);
29         const vaults = Object.fromEntries(
30             Object.entries(itemsByShareId).reduce<[string, ExportedVault][]>((shares, [shareId, itemsById]) => {
31                 const share = selectShare<ShareType.Vault>(shareId)(state);
33                 if (share && share.owner) {
34                     shares.push([
35                         shareId,
36                         {
37                             ...share.content,
38                             items: Object.values(itemsById).map((item) => ({
39                                 itemId: item.itemId,
40                                 shareId: item.shareId,
41                                 data: deobfuscateItem(item.data),
42                                 state: item.state,
43                                 aliasEmail: item.aliasEmail,
44                                 contentFormatVersion: item.contentFormatVersion,
45                                 createTime: item.createTime,
46                                 modifyTime: item.modifyTime,
47                                 pinned: item.pinned,
48                             })),
49                         },
50                     ]);
51                 }
53                 return shares;
54             }, [])
55         );
57         return {
58             encrypted: format === ExportFormat.PGP,
59             userId: user?.ID,
60             vaults,
61             version: config.APP_VERSION,
62         };
63     };