1 import type { ContactGroupLimitReachedProps } from '@proton/components/containers/contacts/modals/ContactGroupLimitReachedModal';
3 import type { MailSettings } from '../../interfaces';
4 import type { ContactEmail } from '../../interfaces/contacts';
5 import { DEFAULT_MAILSETTINGS } from '../../mail/mailSettings';
8 * Check that the user can add other contacts to a contact group.
10 export const hasReachedContactGroupMembersLimit = (
11 numbersOfContacts: number,
12 mailSettings?: MailSettings,
15 const { RecipientLimit } = mailSettings || DEFAULT_MAILSETTINGS;
17 return strict ? numbersOfContacts < RecipientLimit : numbersOfContacts <= RecipientLimit;
21 * Contact groups are limited to 100 contacts. When editing a contact, we do not save directly since the contact might not exist.
22 * Instead, we're doing a delayed save. However, we still need to check that adding the contact to the contact group will be a valid operation.
23 * If the change is valid, we return them, otherwise we need to remove the contact group from the changes requested, and display a modal
25 export const getContactGroupsDelayedSaveChanges = ({
33 userContactEmails: ContactEmail[];
34 changes: { [groupID: string]: boolean };
35 model: { [groupID: string]: number };
36 initialModel: { [groupID: string]: number };
37 onLimitReached?: (props: ContactGroupLimitReachedProps) => void;
38 mailSettings?: MailSettings;
40 // Get number of contacts in saved contact groups
41 const groupIDs = Object.keys(changes);
43 const cannotAddContactInGroupIDs: string[] = [];
45 groupIDs.forEach((groupID) => {
46 const groupExistingMembers =
48 userContactEmails.filter(({ LabelIDs = [] }: { LabelIDs: string[] }) => LabelIDs.includes(groupID));
50 // Check that adding the current contact would not exceed the limit
51 const canAddContact = hasReachedContactGroupMembersLimit(groupExistingMembers.length, mailSettings);
54 cannotAddContactInGroupIDs.push(groupID);
58 // If some addition were exceeding the limit, we remove them from the change array and display a modal to inform the user
59 if (cannotAddContactInGroupIDs.length > 0) {
60 const updatedChanges = Object.entries(model).reduce<{
61 [groupID: string]: boolean;
62 }>((acc, [groupID, isChecked]) => {
63 if (isChecked !== initialModel[groupID] && !cannotAddContactInGroupIDs.includes(groupID)) {
64 acc[groupID] = isChecked === 1;
69 onLimitReached?.({ groupIDs: cannotAddContactInGroupIDs });
70 return updatedChanges;