import { MoreActionItemFactory, MoreActionsPropItem, } from '../../components/MoreActions/MoreActionsBase'; const isGroups = ( items: MoreActionsPropItem[] | MoreActionsPropItem[][] | undefined, ): items is MoreActionsPropItem[][] => items != null && Array.isArray(items[0]); /** * Pushes factory in the list of more actions with the condition that it does not already exist. * Does not modify the original items array. */ export function pushFactoryIfNotExists( items: MoreActionsPropItem[] | undefined, factory: MoreActionItemFactory, ): MoreActionsPropItem[] | undefined { if (!items) { return [factory]; } const idx = items.findIndex((item) => item === factory); if (idx !== -1) { return items; } return [factory, ...items]; } export function pushFactoryIfNotExistsOrAddToTopOfGroup( items: MoreActionsPropItem[] | MoreActionsPropItem[][] | undefined, factory: MoreActionItemFactory | undefined, ): MoreActionsPropItem[] | MoreActionsPropItem[][] | undefined { if (!factory) { return items; } if (!isGroups(items)) { return pushFactoryIfNotExists(items, factory); } const existsInSome = items.some((group) => group.some((item) => item === factory), ); if (existsInSome) { return items; } return [[factory, ...(items[0] || [])], ...items.slice(1)]; }