import { TableActionCell } from '@wix/design-system'; import React, { useEffect } from 'react'; import { CollectionState, FiltersMap } from '@wix/bex-core'; import { observer } from 'mobx-react-lite'; import { ItemActionsState } from '../../state'; import { MessageActionModal } from '../ActionModal'; import { ActionCellProps, TableActionCellMainAction } from '../ActionCell'; import { DisabledActionCellMenu } from '../ActionCell/DisabledActionCellMenu'; import { useToolbarCollectionContext } from '../ToolbarCollectionContext'; import { createPrimaryActionCellPropsWithBI, createSecondaryActionCellPropWithBi, reportOnPopoverMenuShow, } from './CollectionItemActionsHelper'; import { getDefaultActionCellProps } from './getDefaultActionCellProps'; import { useExtensionMenuItems } from '../../hooks/useExtensionMenuItems'; import { useIsMobile } from '../../hooks/useIsMobile'; import { useBmHarmonyTheme } from '../../hooks/useBmHarmonyTheme'; export interface CollectionItemActionsProps< T, F extends FiltersMap = FiltersMap, > { collection: CollectionState; state: ItemActionsState; renderActionCell?: (state: ItemActionsState) => ActionCellProps; } function _CollectionItemActions({ state, renderActionCell, }: CollectionItemActionsProps) { const isMobile = useIsMobile(); const isBmHarmonyTheme = useBmHarmonyTheme(); const { confirmModal } = state; const toolbarState = useToolbarCollectionContext(); const { toolbarBIReporter } = toolbarState; const actionCellProps = renderActionCell?.(state) ?? {}; const { secondaryMenuDisabled, secondaryMenuDisabledTooltip, ...tableActionCellProps } = actionCellProps; const keyedItem = state.actionCellAPI.keyedItem; const existingExtension = actionCellProps.containerId ? toolbarState.menuItemExtensions.get(actionCellProps.containerId) : undefined; const extensionMenuItems = useExtensionMenuItems({ containerId: actionCellProps.containerId, extension: existingExtension, }); useEffect(() => { if (extensionMenuItems && actionCellProps.containerId) { toolbarState.menuItemExtensions.set( actionCellProps.containerId, extensionMenuItems, ); } }, [extensionMenuItems, actionCellProps.containerId]); if (secondaryMenuDisabled) { return ( ); } const secondaryActionCellPropsWithBI = createSecondaryActionCellPropWithBi({ toolbarBIReporter, actionCellProps, keyedItem, extensionMenuItems, }); let primaryActionCellPropsWithBI = !isMobile ? createPrimaryActionCellPropsWithBI( toolbarBIReporter, actionCellProps, keyedItem, ) : []; if ( Array.isArray(primaryActionCellPropsWithBI) && primaryActionCellPropsWithBI.length > 1 ) { primaryActionCellPropsWithBI = primaryActionCellPropsWithBI.map( (action, index) => { return { ...action, priority: index === (primaryActionCellPropsWithBI as TableActionCellMainAction[]) .length! - 1 ? 'primary' : 'secondary', }; }, ); } return ( <> {confirmModal.value && ( )} reportOnPopoverMenuShow( toolbarBIReporter, actionCellProps, state.keyedItem, ), ...actionCellProps.popoverMenuProps, ...getDefaultActionCellProps(), }} primaryAction={primaryActionCellPropsWithBI} secondaryActions={secondaryActionCellPropsWithBI} /> ); } export const CollectionItemActions = observer(_CollectionItemActions);