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) {
38 items: Object.values(itemsById).map((item) => ({
40 shareId: item.shareId,
41 data: deobfuscateItem(item.data),
43 aliasEmail: item.aliasEmail,
44 contentFormatVersion: item.contentFormatVersion,
45 createTime: item.createTime,
46 modifyTime: item.modifyTime,
58 encrypted: format === ExportFormat.PGP,
61 version: config.APP_VERSION,