diff --git a/api-extractor/carbonio-shell-ui.api.md b/api-extractor/carbonio-shell-ui.api.md index a642cc7a2..dc05a1728 100644 --- a/api-extractor/carbonio-shell-ui.api.md +++ b/api-extractor/carbonio-shell-ui.api.md @@ -104,6 +104,8 @@ export type AccountSettings = { type AccountSettingsAttrs = { zimbraFeatureOptionsEnabled?: BooleanString; zimbraIdentityMaxNumEntries?: number; + zimbraMailAlias?: string | Array; + zimbraAllowFromAddress?: string | Array; [key: string]: string | number | Array | undefined; }; @@ -1731,11 +1733,11 @@ interface ZimletProp { // lib/types/account/index.d.ts:38:9 - (ae-forgotten-export) The symbol "Signature" needs to be exported by the entry point lib.d.ts // lib/types/account/index.d.ts:43:5 - (ae-forgotten-export) The symbol "AccountRights" needs to be exported by the entry point lib.d.ts // lib/types/account/index.d.ts:47:5 - (ae-forgotten-export) The symbol "StringOfLength" needs to be exported by the entry point lib.d.ts -// lib/types/account/index.d.ts:92:5 - (ae-forgotten-export) The symbol "AccountSettingsAttrs" needs to be exported by the entry point lib.d.ts -// lib/types/account/index.d.ts:94:5 - (ae-forgotten-export) The symbol "ZimletProp" needs to be exported by the entry point lib.d.ts -// lib/types/account/index.d.ts:138:5 - (ae-forgotten-export) The symbol "AccountRightTargetEmail" needs to be exported by the entry point lib.d.ts -// lib/types/account/index.d.ts:143:9 - (ae-forgotten-export) The symbol "AccountRightName" needs to be exported by the entry point lib.d.ts -// lib/types/account/index.d.ts:144:9 - (ae-forgotten-export) The symbol "AccountRightTarget" needs to be exported by the entry point lib.d.ts +// lib/types/account/index.d.ts:94:5 - (ae-forgotten-export) The symbol "AccountSettingsAttrs" needs to be exported by the entry point lib.d.ts +// lib/types/account/index.d.ts:96:5 - (ae-forgotten-export) The symbol "ZimletProp" needs to be exported by the entry point lib.d.ts +// lib/types/account/index.d.ts:140:5 - (ae-forgotten-export) The symbol "AccountRightTargetEmail" needs to be exported by the entry point lib.d.ts +// lib/types/account/index.d.ts:145:9 - (ae-forgotten-export) The symbol "AccountRightName" needs to be exported by the entry point lib.d.ts +// lib/types/account/index.d.ts:146:9 - (ae-forgotten-export) The symbol "AccountRightTarget" needs to be exported by the entry point lib.d.ts // lib/types/apps/index.d.ts:68:5 - (ae-forgotten-export) The symbol "PanelMode" needs to be exported by the entry point lib.d.ts // lib/types/misc/index.d.ts:75:9 - (ae-forgotten-export) The symbol "SoapPolicy" needs to be exported by the entry point lib.d.ts // lib/types/misc/index.d.ts:94:5 - (ae-forgotten-export) The symbol "FolderView" needs to be exported by the entry point lib.d.ts diff --git a/src/settings/components/utils.test.ts b/src/settings/components/utils.test.ts index b0e192579..30a31b9dd 100644 --- a/src/settings/components/utils.test.ts +++ b/src/settings/components/utils.test.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { dateToGenTime, genTimeToDate, humanFileSize } from './utils'; +import { dateToGenTime, genTimeToDate, humanFileSize, asArray } from './utils'; import type { GeneralizedTime } from '../../types/account'; describe('dateToGenTime function', () => { @@ -103,3 +103,25 @@ describe('humanFileSize function', () => { expect(() => humanFileSize(1024 ** 9, undefined)).toThrow('Unsupported inputSize'); }); }); + +describe('asArray', () => { + it('should return an array when the value is an array', () => { + const result = asArray(['value1', 'value2']); + expect(result).toEqual(['value1', 'value2']); + }); + + it('should return an array when the value is a single value', () => { + const result = asArray('singleValue'); + expect(result).toEqual(['singleValue']); + }); + + it('should return an empty array when the value is undefined', () => { + const result = asArray(undefined); + expect(result).toEqual([]); + }); + + it('should return array of numbers', () => { + const result = asArray(123); + expect(result).toEqual([123]); + }); +}); diff --git a/src/settings/components/utils.ts b/src/settings/components/utils.ts index 9f0e81bc5..668516049 100644 --- a/src/settings/components/utils.ts +++ b/src/settings/components/utils.ts @@ -7,7 +7,7 @@ import type React from 'react'; import type { TFunction } from 'i18next'; -import { cloneDeep, filter, findIndex, isArray, isBoolean, reduce, uniq } from 'lodash'; +import { cloneDeep, filter, findIndex, isBoolean, reduce, uniq } from 'lodash'; import { BASE_FONT_SIZE, SCALING_LIMIT, SCALING_OPTIONS } from '../../constants'; import type { LocaleDescriptor } from '../../constants/locales'; @@ -496,6 +496,30 @@ export function defaultAsFirstOrderIdentities(identities: Array): Arra return result; } +/** + * Wraps a given value in an array if it is not already an array. + * + * @template T - The type of the input value. + * @param {T | T[] | undefined} value - The value to be transformed. Can be a single value of type `T`, + * an array of `T`, or `undefined`. + * @returns {T[]} - Returns an array of `T`. If `value` is an array, it is returned as-is. If `value` + * is a single item, it is wrapped in an array. If `value` is `undefined`, returns an empty array. + * + * @example + * asArray(5); // returns [5] + * asArray([5, 6]); // returns [5, 6] + * asArray(undefined); // returns [] + */ +export function asArray(value: T | T[] | undefined): T[] { + if (value !== undefined) { + if (Array.isArray(value)) { + return value; + } + return [value]; + } + return []; +} + /** * Compose a unique list of all identities' email addresses * @@ -503,6 +527,7 @@ export function defaultAsFirstOrderIdentities(identities: Array): Arra * - the email address of the current account * - the email addresses of all the shared accounts (taken from the rights infos) * - all the aliases + * - all the email addresses from zimbraAllowFromAddress * * @param account * @param settings @@ -533,14 +558,10 @@ export const getAvailableEmailAddresses = ( }); } - // Adds all the aliases - if (settings.attrs.zimbraMailAlias) { - if (isArray(settings.attrs.zimbraMailAlias)) { - result.push(...(settings.attrs.zimbraMailAlias as string[])); - } else { - result.push(String(settings.attrs.zimbraMailAlias)); - } - } + result.push( + ...asArray(settings.attrs.zimbraMailAlias), + ...asArray(settings.attrs.zimbraAllowFromAddress) + ); return uniq(result); }; diff --git a/src/types/account/index.ts b/src/types/account/index.ts index 2bed07dd3..95d299756 100644 --- a/src/types/account/index.ts +++ b/src/types/account/index.ts @@ -101,6 +101,8 @@ export interface AccountSettingsPrefs { export type AccountSettingsAttrs = { zimbraFeatureOptionsEnabled?: BooleanString; zimbraIdentityMaxNumEntries?: number; + zimbraMailAlias?: string | Array; + zimbraAllowFromAddress?: string | Array; [key: string]: string | number | Array | undefined; };