import React from 'react'; import { FiltersMap, KeyedItem } from '@wix/bex-core'; import { GridCardItemProps } from './GridCardItem'; import { GridBaseState, ItemActionsState } from '../../state'; import { ActionCellProps, TableActionCellPrimaryAction } from '../ActionCell'; import { CardGalleryItemProps, IconButton, PopoverMenu, PopoverMenuItemProps, } from '@wix/design-system'; import { More } from '@wix/wix-ui-icons-common'; import { DisabledActionCellMenu } from '../ActionCell/DisabledActionCellMenu'; import { createPrimaryActionCellPropsWithBI, createSecondaryActionCellPropWithBi, reportOnPopoverMenuShow, } from '../CollectionItemActions/CollectionItemActionsHelper'; import { getDefaultActionCellProps } from '../CollectionItemActions/getDefaultActionCellProps'; import { useExtensionMenuItems } from '../../hooks/useExtensionMenuItems'; interface ActionCell { actionCell: GridCardItemProps['actionCell']; item: T; index: number; gridState: GridBaseState; keyedItem: KeyedItem; itemActionsState: ItemActionsState; } export function evaluateActionCell({ actionCell, item, index, gridState, keyedItem, itemActionsState, }: ActionCell): ActionCellProps { if (!actionCell) { return {}; } if (typeof actionCell === 'function') { return actionCell(item, index, { openConfirmDeleteModal: itemActionsState.openConfirmDeleteModal, openConfirmModal: itemActionsState.openConfirmModal, collection: gridState.collection, keyedItem, }); } return actionCell; } export function useGridItemActions( actionCell: ActionCell & { itemActionsState: ItemActionsState; imagePlacement: string; }, ) { const actions = evaluateActionCell(actionCell); let settingsMenu: CardGalleryItemProps['settingsMenu'] = null; let primaryAction: CardGalleryItemProps['primaryActionProps'] | null = null; const extensionMenuItems = useExtensionMenuItems({ containerId: actions.containerId, }); const secondaryActionCellPropsWithBI = createSecondaryActionCellPropWithBi({ toolbarBIReporter: actionCell.gridState.toolbar.toolbarBIReporter, actionCellProps: actions, keyedItem: actionCell.keyedItem, extensionMenuItems, }); const primaryActionCellPropsWithBI = createPrimaryActionCellPropsWithBI( actionCell.gridState.toolbar.toolbarBIReporter, actions, actionCell.keyedItem, ) as TableActionCellPrimaryAction; if (primaryActionCellPropsWithBI) { primaryAction = { label: primaryActionCellPropsWithBI.text, onClick: primaryActionCellPropsWithBI.onClick, disabled: primaryActionCellPropsWithBI.disabled, disabledMessage: primaryActionCellPropsWithBI.disabledMessage, }; } if (actions.secondaryMenuDisabled) { settingsMenu = ( ); } else if ( secondaryActionCellPropsWithBI && secondaryActionCellPropsWithBI.length > 0 ) { settingsMenu = ( ( { e.stopPropagation(); toggle(); reportOnPopoverMenuShow( actionCell.gridState.toolbar.toolbarBIReporter, actions, actionCell.keyedItem, ); }} onPointerDown={ actionCell.gridState._dnd ? (e) => e.stopPropagation() : undefined } onMouseLeave={close} skin={actionCell.imagePlacement === 'top' ? 'light' : 'standard'} size="small" priority="secondary" > )} children={secondaryActionCellPropsWithBI.map((action, index) => { if (action.divider) { return ; } const { icon: prefixIcon, skin, ...rest } = action; return ( ); })} /> ); } return { primaryAction, settingsMenu, }; }